blob: 685b082a435cfe9924767b861fbdd03a66287763 [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
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000283 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
285 setOperationAction(ISD::VAEND , MVT::Other, Expand);
286 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
287 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
288 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
289 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattner56a752e2006-10-18 01:18:48 +0000290
Chris Lattner6d92cad2006-03-26 10:06:40 +0000291 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000293
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000294 // To handle counter-based loop conditions.
295 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
296
Dale Johannesen53e4e442008-11-07 22:54:33 +0000297 // Comparisons that require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
299 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
300 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
301 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
302 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
303 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
304 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
305 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
306 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
307 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
308 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
309 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000310
Evan Cheng769951f2012-07-02 22:39:56 +0000311 if (Subtarget->has64BitSupport()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000312 // They also have instructions for converting between i64 and fp.
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
314 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
315 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
316 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen4c9369d2009-06-04 20:53:52 +0000317 // This is just the low 32 bits of a (signed) fp->i64 conversion.
318 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000320
Hal Finkel46479192013-04-01 17:52:07 +0000321 if (PPCSubTarget.hasLFIWAX() || Subtarget->isPPC64())
Hal Finkel9ad0f492013-03-31 01:58:02 +0000322 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000323 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000324 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000326 }
327
Hal Finkel46479192013-04-01 17:52:07 +0000328 // With the instructions enabled under FPCVT, we can do everything.
329 if (PPCSubTarget.hasFPCVT()) {
330 if (Subtarget->has64BitSupport()) {
331 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
332 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
333 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
334 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
335 }
336
337 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
338 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
339 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
340 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
341 }
342
Evan Cheng769951f2012-07-02 22:39:56 +0000343 if (Subtarget->use64BitRegs()) {
Chris Lattner26cb2862007-10-19 04:08:28 +0000344 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperc9099502012-04-20 06:31:50 +0000345 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000346 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman9ed06db2008-03-07 20:36:53 +0000348 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
350 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
351 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000352 } else {
Chris Lattner26cb2862007-10-19 04:08:28 +0000353 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
355 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
356 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000357 }
Evan Chengd30bf012006-03-01 01:11:20 +0000358
Evan Cheng769951f2012-07-02 22:39:56 +0000359 if (Subtarget->hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000360 // First set operation action for all vector types to expand. Then we
361 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
363 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
364 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000365
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000366 // add/sub are legal for all supported vector VT's.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000367 setOperationAction(ISD::ADD , VT, Legal);
368 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000369
Chris Lattner7ff7e672006-04-04 17:25:31 +0000370 // We promote all shuffles to v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000371 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000373
374 // We promote all non-typed operations to v4i32.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000375 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000377 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000379 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000381 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000383 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000385 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +0000387
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000388 // No other operations are legal.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000389 setOperationAction(ISD::MUL , VT, Expand);
390 setOperationAction(ISD::SDIV, VT, Expand);
391 setOperationAction(ISD::SREM, VT, Expand);
392 setOperationAction(ISD::UDIV, VT, Expand);
393 setOperationAction(ISD::UREM, VT, Expand);
394 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkelad3b34d2013-07-08 17:30:25 +0000395 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000396 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topper44e394c2012-11-15 08:02:19 +0000397 setOperationAction(ISD::FSQRT, VT, Expand);
398 setOperationAction(ISD::FLOG, VT, Expand);
399 setOperationAction(ISD::FLOG10, VT, Expand);
400 setOperationAction(ISD::FLOG2, VT, Expand);
401 setOperationAction(ISD::FEXP, VT, Expand);
402 setOperationAction(ISD::FEXP2, VT, Expand);
403 setOperationAction(ISD::FSIN, VT, Expand);
404 setOperationAction(ISD::FCOS, VT, Expand);
405 setOperationAction(ISD::FABS, VT, Expand);
406 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topper1ab489a2012-11-14 08:11:25 +0000407 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000408 setOperationAction(ISD::FCEIL, VT, Expand);
409 setOperationAction(ISD::FTRUNC, VT, Expand);
410 setOperationAction(ISD::FRINT, VT, Expand);
411 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000412 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
413 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
414 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
415 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
416 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
417 setOperationAction(ISD::UDIVREM, VT, Expand);
418 setOperationAction(ISD::SDIVREM, VT, Expand);
419 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
420 setOperationAction(ISD::FPOW, VT, Expand);
421 setOperationAction(ISD::CTPOP, VT, Expand);
422 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000423 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000424 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000425 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramer91223a42012-12-19 15:49:14 +0000426 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellacfe09ed2012-11-05 17:15:56 +0000427 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
428
429 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
430 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
431 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
432 setTruncStoreAction(VT, InnerVT, Expand);
433 }
434 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
435 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
436 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000437 }
438
Chris Lattner7ff7e672006-04-04 17:25:31 +0000439 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
440 // with merges, splats, etc.
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000442
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::AND , MVT::v4i32, Legal);
444 setOperationAction(ISD::OR , MVT::v4i32, Legal);
445 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
446 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
447 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
448 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella51aaadb2012-10-08 17:27:24 +0000449 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
450 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
451 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
452 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellae95ed2b2012-11-15 20:56:03 +0000453 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
454 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
455 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
456 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000457
Craig Topperc9099502012-04-20 06:31:50 +0000458 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
459 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
460 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
461 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelfdc40a02009-02-17 22:15:04 +0000462
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel070b8db2012-06-22 00:49:52 +0000464 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel827307b2013-04-03 04:01:11 +0000465
466 if (TM.Options.UnsafeFPMath) {
467 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
468 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
469 }
470
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
472 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
473 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000474
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
476 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000477
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
479 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
480 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
481 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella5f41fd62012-10-30 13:50:19 +0000482
483 // Altivec does not contain unordered floating-point compare instructions
484 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
485 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
486 setCondCodeAction(ISD::SETUGT, MVT::v4f32, Expand);
487 setCondCodeAction(ISD::SETUGE, MVT::v4f32, Expand);
488 setCondCodeAction(ISD::SETULT, MVT::v4f32, Expand);
489 setCondCodeAction(ISD::SETULE, MVT::v4f32, Expand);
Nate Begeman425a9692005-11-29 08:17:20 +0000490 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000491
Hal Finkel8cc34742012-08-04 14:10:46 +0000492 if (Subtarget->has64BitSupport()) {
Hal Finkel19aa2b52012-04-01 20:08:17 +0000493 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel8cc34742012-08-04 14:10:46 +0000494 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
495 }
Hal Finkel19aa2b52012-04-01 20:08:17 +0000496
Eli Friedman4db5aca2011-08-29 18:23:02 +0000497 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
498 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkelcd9ea512012-12-25 17:22:53 +0000499 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
500 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000501
Duncan Sands03228082008-11-23 15:47:28 +0000502 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidtfa799112013-04-23 18:49:44 +0000503 // Altivec instructions set fields to all zeros or all ones.
504 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelfdc40a02009-02-17 22:15:04 +0000505
Evan Cheng769951f2012-07-02 22:39:56 +0000506 if (isPPC64) {
Chris Lattner10da9572006-10-18 01:20:43 +0000507 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000508 setExceptionPointerRegister(PPC::X3);
509 setExceptionSelectorRegister(PPC::X4);
510 } else {
Chris Lattner10da9572006-10-18 01:20:43 +0000511 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000512 setExceptionPointerRegister(PPC::R3);
513 setExceptionSelectorRegister(PPC::R4);
514 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000515
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000516 // We have target-specific dag combine patterns for the following nodes:
517 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkel80d10de2013-05-24 23:00:14 +0000518 setTargetDAGCombine(ISD::LOAD);
Chris Lattner51269842006-03-01 05:50:56 +0000519 setTargetDAGCombine(ISD::STORE);
Chris Lattner90564f22006-04-18 17:59:36 +0000520 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnerd9989382006-07-10 20:56:58 +0000521 setTargetDAGCombine(ISD::BSWAP);
Hal Finkel5a0e6042013-05-25 04:05:05 +0000522 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Scott Michelfdc40a02009-02-17 22:15:04 +0000523
Hal Finkel827307b2013-04-03 04:01:11 +0000524 // Use reciprocal estimates.
525 if (TM.Options.UnsafeFPMath) {
526 setTargetDAGCombine(ISD::FDIV);
527 setTargetDAGCombine(ISD::FSQRT);
528 }
529
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000530 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng769951f2012-07-02 22:39:56 +0000531 if (Subtarget->isDarwin()) {
Duncan Sands007f9842008-01-10 10:28:30 +0000532 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000533 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
534 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands007f9842008-01-10 10:28:30 +0000535 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
536 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000537 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
538 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
539 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
540 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
541 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000542 }
543
Hal Finkelc6129162011-10-17 18:53:03 +0000544 setMinFunctionAlignment(2);
545 if (PPCSubTarget.isDarwin())
546 setPrefFunctionAlignment(4);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000547
Evan Cheng769951f2012-07-02 22:39:56 +0000548 if (isPPC64 && Subtarget->isJITCodeModel())
549 // Temporary workaround for the inability of PPC64 JIT to handle jump
550 // tables.
551 setSupportJumpTables(false);
552
Eli Friedman26689ac2011-08-03 21:06:02 +0000553 setInsertFencesForAtomic(true);
554
Hal Finkel768c65f2011-11-22 16:21:04 +0000555 setSchedulingPreference(Sched::Hybrid);
556
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000557 computeRegisterProperties();
Hal Finkel621b77a2012-08-28 16:12:39 +0000558
559 // The Freescale cores does better with aggressive inlining of memcpy and
560 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
561 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
562 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach3450f802013-02-20 21:13:59 +0000563 MaxStoresPerMemset = 32;
564 MaxStoresPerMemsetOptSize = 16;
565 MaxStoresPerMemcpy = 32;
566 MaxStoresPerMemcpyOptSize = 8;
567 MaxStoresPerMemmove = 32;
568 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel621b77a2012-08-28 16:12:39 +0000569
570 setPrefFunctionAlignment(4);
Hal Finkel621b77a2012-08-28 16:12:39 +0000571 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000572}
573
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000574/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
575/// function arguments in the caller parameter area.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000576unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dan Gohmanf0757b02010-04-21 01:34:56 +0000577 const TargetMachine &TM = getTargetMachine();
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000578 // Darwin passes everything on 4 byte boundary.
579 if (TM.getSubtarget<PPCSubtarget>().isDarwin())
580 return 4;
Roman Divacky466958c2012-04-02 15:49:30 +0000581
582 // 16byte and wider vectors are passed on 16byte boundary.
583 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
584 if (VTy->getBitWidth() >= 128)
585 return 16;
586
587 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
588 if (PPCSubTarget.isPPC64())
589 return 8;
590
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000591 return 4;
592}
593
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000594const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
595 switch (Opcode) {
596 default: return 0;
Evan Cheng53301922008-07-12 02:23:19 +0000597 case PPCISD::FSEL: return "PPCISD::FSEL";
598 case PPCISD::FCFID: return "PPCISD::FCFID";
599 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
600 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel827307b2013-04-03 04:01:11 +0000601 case PPCISD::FRE: return "PPCISD::FRE";
602 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng53301922008-07-12 02:23:19 +0000603 case PPCISD::STFIWX: return "PPCISD::STFIWX";
604 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
605 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
606 case PPCISD::VPERM: return "PPCISD::VPERM";
607 case PPCISD::Hi: return "PPCISD::Hi";
608 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000609 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller3a84dae2009-12-18 13:00:15 +0000610 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
611 case PPCISD::LOAD: return "PPCISD::LOAD";
612 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng53301922008-07-12 02:23:19 +0000613 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
614 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
615 case PPCISD::SRL: return "PPCISD::SRL";
616 case PPCISD::SRA: return "PPCISD::SRA";
617 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigand86765fb2013-03-22 15:24:13 +0000618 case PPCISD::CALL: return "PPCISD::CALL";
619 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng53301922008-07-12 02:23:19 +0000620 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigand86765fb2013-03-22 15:24:13 +0000621 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng53301922008-07-12 02:23:19 +0000622 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel7ee74a62013-03-21 21:37:52 +0000623 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
624 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigand965b20e2013-07-03 17:05:42 +0000625 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng53301922008-07-12 02:23:19 +0000626 case PPCISD::VCMP: return "PPCISD::VCMP";
627 case PPCISD::VCMPo: return "PPCISD::VCMPo";
628 case PPCISD::LBRX: return "PPCISD::LBRX";
629 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng53301922008-07-12 02:23:19 +0000630 case PPCISD::LARX: return "PPCISD::LARX";
631 case PPCISD::STCX: return "PPCISD::STCX";
632 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000633 case PPCISD::BDNZ: return "PPCISD::BDNZ";
634 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng53301922008-07-12 02:23:19 +0000635 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng53301922008-07-12 02:23:19 +0000636 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng53301922008-07-12 02:23:19 +0000637 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel82b38212012-08-28 02:10:27 +0000638 case PPCISD::CR6SET: return "PPCISD::CR6SET";
639 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000640 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
641 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
642 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Bill Schmidtb453e162012-12-14 17:02:38 +0000643 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
644 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtd7802bf2012-12-04 16:18:08 +0000645 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidt57ac1f42012-12-11 20:30:11 +0000646 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
647 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
648 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt349c2782012-12-12 19:29:35 +0000649 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
650 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
651 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
652 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
653 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidtb34c79e2013-02-20 15:50:31 +0000654 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidt5bbdb192013-05-14 19:35:45 +0000655 case PPCISD::SC: return "PPCISD::SC";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000656 }
657}
658
Matt Arsenault225ed702013-05-18 00:21:46 +0000659EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanella1c7d69b2012-10-08 18:59:53 +0000660 if (!VT.isVector())
661 return MVT::i32;
662 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000663}
664
Chris Lattner1a635d62006-04-14 06:01:58 +0000665//===----------------------------------------------------------------------===//
666// Node matching predicates, for use by the tblgen matching code.
667//===----------------------------------------------------------------------===//
668
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000669/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman475871a2008-07-27 21:46:04 +0000670static bool isFloatingPointZero(SDValue Op) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000671 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000672 return CFP->getValueAPF().isZero();
Gabor Greifba36cb52008-08-28 21:40:38 +0000673 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000674 // Maybe this has already been legalized into the constant pool?
675 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohman46510a72010-04-15 01:51:59 +0000676 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000677 return CFP->getValueAPF().isZero();
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000678 }
679 return false;
680}
681
Chris Lattnerddb739e2006-04-06 17:23:16 +0000682/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
683/// true if Op is undef or if it matches the specified value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000684static bool isConstantOrUndef(int Op, int Val) {
685 return Op < 0 || Op == Val;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000686}
687
688/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
689/// VPKUHUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000690bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000691 if (!isUnary) {
692 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000693 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000694 return false;
695 } else {
696 for (unsigned i = 0; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000697 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
698 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000699 return false;
700 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000701 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000702}
703
704/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
705/// VPKUWUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000706bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000707 if (!isUnary) {
708 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000709 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
710 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000711 return false;
712 } else {
713 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000714 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
715 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
716 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
717 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000718 return false;
719 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000720 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000721}
722
Chris Lattnercaad1632006-04-06 22:02:42 +0000723/// isVMerge - Common function, used to match vmrg* shuffles.
724///
Nate Begeman9008ca62009-04-27 18:41:29 +0000725static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnercaad1632006-04-06 22:02:42 +0000726 unsigned LHSStart, unsigned RHSStart) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000727 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman9008ca62009-04-27 18:41:29 +0000728 "PPC only supports shuffles by bytes!");
Chris Lattner116cc482006-04-06 21:11:54 +0000729 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
730 "Unsupported merge size!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000731
Chris Lattner116cc482006-04-06 21:11:54 +0000732 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
733 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman9008ca62009-04-27 18:41:29 +0000734 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000735 LHSStart+j+i*UnitSize) ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000736 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000737 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000738 return false;
739 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000740 return true;
Chris Lattnercaad1632006-04-06 22:02:42 +0000741}
742
743/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
744/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000745bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman9008ca62009-04-27 18:41:29 +0000746 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000747 if (!isUnary)
748 return isVMerge(N, UnitSize, 8, 24);
749 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000750}
751
752/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
753/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000754bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman9008ca62009-04-27 18:41:29 +0000755 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000756 if (!isUnary)
757 return isVMerge(N, UnitSize, 0, 16);
758 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000759}
760
761
Chris Lattnerd0608e12006-04-06 18:26:28 +0000762/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
763/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000764int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman9008ca62009-04-27 18:41:29 +0000766 "PPC only supports shuffles by bytes!");
767
768 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000769
Chris Lattnerd0608e12006-04-06 18:26:28 +0000770 // Find the first non-undef value in the shuffle mask.
771 unsigned i;
Nate Begeman9008ca62009-04-27 18:41:29 +0000772 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattnerd0608e12006-04-06 18:26:28 +0000773 /*search*/;
Scott Michelfdc40a02009-02-17 22:15:04 +0000774
Chris Lattnerd0608e12006-04-06 18:26:28 +0000775 if (i == 16) return -1; // all undef.
Scott Michelfdc40a02009-02-17 22:15:04 +0000776
Nate Begeman9008ca62009-04-27 18:41:29 +0000777 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattnerd0608e12006-04-06 18:26:28 +0000778 // numbered from this value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000779 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattnerd0608e12006-04-06 18:26:28 +0000780 if (ShiftAmt < i) return -1;
781 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000782
Chris Lattnerf24380e2006-04-06 22:28:36 +0000783 if (!isUnary) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000784 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000785 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000786 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000787 return -1;
788 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000789 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000790 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000791 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000792 return -1;
793 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000794 return ShiftAmt;
795}
Chris Lattneref819f82006-03-20 06:33:01 +0000796
797/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
798/// specifies a splat of a single element that is suitable for input to
799/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman9008ca62009-04-27 18:41:29 +0000800bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner7ff7e672006-04-04 17:25:31 +0000802 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelfdc40a02009-02-17 22:15:04 +0000803
Chris Lattner88a99ef2006-03-20 06:37:44 +0000804 // This is a splat operation if each element of the permute is the same, and
805 // if the value doesn't reference the second vector.
Nate Begeman9008ca62009-04-27 18:41:29 +0000806 unsigned ElementBase = N->getMaskElt(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000807
Nate Begeman9008ca62009-04-27 18:41:29 +0000808 // FIXME: Handle UNDEF elements too!
809 if (ElementBase >= 16)
Chris Lattner7ff7e672006-04-04 17:25:31 +0000810 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000811
Nate Begeman9008ca62009-04-27 18:41:29 +0000812 // Check that the indices are consecutive, in the case of a multi-byte element
813 // splatted with a v16i8 mask.
814 for (unsigned i = 1; i != EltSize; ++i)
815 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000816 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000817
Chris Lattner7ff7e672006-04-04 17:25:31 +0000818 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000819 if (N->getMaskElt(i) < 0) continue;
Chris Lattner7ff7e672006-04-04 17:25:31 +0000820 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman9008ca62009-04-27 18:41:29 +0000821 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000822 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000823 }
Chris Lattner7ff7e672006-04-04 17:25:31 +0000824 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000825}
826
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000827/// isAllNegativeZeroVector - Returns true if all elements of build_vector
828/// are -0.0.
829bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000830 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
831
832 APInt APVal, APUndef;
833 unsigned BitSize;
834 bool HasAnyUndefs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000835
Dale Johannesen1e608812009-11-13 01:45:18 +0000836 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman9008ca62009-04-27 18:41:29 +0000837 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000838 return CFP->getValueAPF().isNegZero();
Nate Begeman9008ca62009-04-27 18:41:29 +0000839
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000840 return false;
841}
842
Chris Lattneref819f82006-03-20 06:33:01 +0000843/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
844/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000845unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000846 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
847 assert(isSplatShuffleMask(SVOp, EltSize));
848 return SVOp->getMaskElt(0) / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000849}
850
Chris Lattnere87192a2006-04-12 17:37:20 +0000851/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000852/// by using a vspltis[bhw] instruction of the specified element size, return
853/// the constant being splatted. The ByteSize field indicates the number of
854/// bytes of each element [124] -> [bhw].
Dan Gohman475871a2008-07-27 21:46:04 +0000855SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
856 SDValue OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000857
858 // If ByteSize of the splat is bigger than the element size of the
859 // build_vector, then we have a case where we are checking for a splat where
860 // multiple elements of the buildvector are folded together into a single
861 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
862 unsigned EltSize = 16/N->getNumOperands();
863 if (EltSize < ByteSize) {
864 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman475871a2008-07-27 21:46:04 +0000865 SDValue UniquedVals[4];
Chris Lattner79d9a882006-04-08 07:14:26 +0000866 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelfdc40a02009-02-17 22:15:04 +0000867
Chris Lattner79d9a882006-04-08 07:14:26 +0000868 // See if all of the elements in the buildvector agree across.
869 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
870 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
871 // If the element isn't a constant, bail fully out.
Dan Gohman475871a2008-07-27 21:46:04 +0000872 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000873
Scott Michelfdc40a02009-02-17 22:15:04 +0000874
Gabor Greifba36cb52008-08-28 21:40:38 +0000875 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattner79d9a882006-04-08 07:14:26 +0000876 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
877 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000878 return SDValue(); // no match.
Chris Lattner79d9a882006-04-08 07:14:26 +0000879 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000880
Chris Lattner79d9a882006-04-08 07:14:26 +0000881 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
882 // either constant or undef values that are identical for each chunk. See
883 // if these chunks can form into a larger vspltis*.
Scott Michelfdc40a02009-02-17 22:15:04 +0000884
Chris Lattner79d9a882006-04-08 07:14:26 +0000885 // Check to see if all of the leading entries are either 0 or -1. If
886 // neither, then this won't fit into the immediate field.
887 bool LeadingZero = true;
888 bool LeadingOnes = true;
889 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000890 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelfdc40a02009-02-17 22:15:04 +0000891
Chris Lattner79d9a882006-04-08 07:14:26 +0000892 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
893 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
894 }
895 // Finally, check the least significant entry.
896 if (LeadingZero) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000897 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +0000898 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000899 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000900 if (Val < 16)
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattner79d9a882006-04-08 07:14:26 +0000902 }
903 if (LeadingOnes) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000904 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman7810bfe2008-09-26 21:54:37 +0000906 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000907 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattner79d9a882006-04-08 07:14:26 +0000909 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000910
Dan Gohman475871a2008-07-27 21:46:04 +0000911 return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000912 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000913
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000914 // Check to see if this buildvec has a single non-undef value in its elements.
915 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
916 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greifba36cb52008-08-28 21:40:38 +0000917 if (OpVal.getNode() == 0)
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000918 OpVal = N->getOperand(i);
919 else if (OpVal != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000920 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000921 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000922
Gabor Greifba36cb52008-08-28 21:40:38 +0000923 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelfdc40a02009-02-17 22:15:04 +0000924
Eli Friedman1a8229b2009-05-24 02:03:36 +0000925 unsigned ValSizeInBytes = EltSize;
Nate Begeman98e70cc2006-03-28 04:15:58 +0000926 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000927 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000928 Value = CN->getZExtValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000929 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johanneseneaf08942007-08-31 04:03:46 +0000931 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000932 }
933
934 // If the splat value is larger than the element value, then we can never do
935 // this splat. The only case that we could fit the replicated bits into our
936 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman475871a2008-07-27 21:46:04 +0000937 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +0000938
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000939 // If the element value is larger than the splat value, cut it in half and
940 // check to see if the two halves are equal. Continue doing this until we
941 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
942 while (ValSizeInBytes > ByteSize) {
943 ValSizeInBytes >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000944
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000945 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000946 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
947 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman475871a2008-07-27 21:46:04 +0000948 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000949 }
950
951 // Properly sign extend the value.
Richard Smith1144af32012-08-24 23:29:28 +0000952 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelfdc40a02009-02-17 22:15:04 +0000953
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000954 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman475871a2008-07-27 21:46:04 +0000955 if (MaskVal == 0) return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000956
Chris Lattner140a58f2006-04-08 06:46:53 +0000957 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith1144af32012-08-24 23:29:28 +0000958 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson825b72b2009-08-11 20:47:22 +0000959 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000960 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000961}
962
Chris Lattner1a635d62006-04-14 06:01:58 +0000963//===----------------------------------------------------------------------===//
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000964// Addressing Mode Selection
965//===----------------------------------------------------------------------===//
966
967/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
968/// or 64-bit immediate, and if the value can be accurately represented as a
969/// sign extension from a 16-bit value. If so, this returns true and the
970/// immediate.
971static bool isIntS16Immediate(SDNode *N, short &Imm) {
972 if (N->getOpcode() != ISD::Constant)
973 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000974
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000975 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +0000976 if (N->getValueType(0) == MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000977 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000978 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000979 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000980}
Dan Gohman475871a2008-07-27 21:46:04 +0000981static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000982 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000983}
984
985
986/// SelectAddressRegReg - Given the specified addressed, check to see if it
987/// can be represented as an indexed [r+r] operation. Returns false if it
988/// can be more efficiently represented with [r+imm].
Dan Gohman475871a2008-07-27 21:46:04 +0000989bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
990 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +0000991 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000992 short imm = 0;
993 if (N.getOpcode() == ISD::ADD) {
994 if (isIntS16Immediate(N.getOperand(1), imm))
995 return false; // r+i
996 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
997 return false; // r+i
Scott Michelfdc40a02009-02-17 22:15:04 +0000998
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000999 Base = N.getOperand(0);
1000 Index = N.getOperand(1);
1001 return true;
1002 } else if (N.getOpcode() == ISD::OR) {
1003 if (isIntS16Immediate(N.getOperand(1), imm))
1004 return false; // r+i can fold it if we can.
Scott Michelfdc40a02009-02-17 22:15:04 +00001005
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001006 // If this is an or of disjoint bitfields, we can codegen this as an add
1007 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1008 // disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001009 APInt LHSKnownZero, LHSKnownOne;
1010 APInt RHSKnownZero, RHSKnownOne;
1011 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001012 LHSKnownZero, LHSKnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001013
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001014 if (LHSKnownZero.getBoolValue()) {
1015 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001016 RHSKnownZero, RHSKnownOne);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001017 // If all of the bits are known zero on the LHS or RHS, the add won't
1018 // carry.
Dan Gohmanec59b952008-02-27 21:12:32 +00001019 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001020 Base = N.getOperand(0);
1021 Index = N.getOperand(1);
1022 return true;
1023 }
1024 }
1025 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001026
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001027 return false;
1028}
1029
1030/// Returns true if the address N can be represented by a base register plus
1031/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand347a5072013-05-16 17:58:02 +00001032/// represented as reg+reg. If Aligned is true, only accept displacements
1033/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman475871a2008-07-27 21:46:04 +00001034bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman73e09142009-01-15 16:29:45 +00001035 SDValue &Base,
Ulrich Weigand347a5072013-05-16 17:58:02 +00001036 SelectionDAG &DAG,
1037 bool Aligned) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +00001038 // FIXME dl should come from parent load or store, not from address
Andrew Trickac6d9be2013-05-25 02:42:55 +00001039 SDLoc dl(N);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001040 // If this can be more profitably realized as r+r, fail.
1041 if (SelectAddressRegReg(N, Disp, Base, DAG))
1042 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001043
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001044 if (N.getOpcode() == ISD::ADD) {
1045 short imm = 0;
Ulrich Weigand347a5072013-05-16 17:58:02 +00001046 if (isIntS16Immediate(N.getOperand(1), imm) &&
1047 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigandf0ef8822013-05-16 14:53:05 +00001048 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001049 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1050 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1051 } else {
1052 Base = N.getOperand(0);
1053 }
1054 return true; // [r+i]
1055 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1056 // Match LOAD (ADD (X, Lo(G))).
Gabor Greif413ca0d2012-04-20 11:41:38 +00001057 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001058 && "Cannot handle constant offsets yet!");
1059 Disp = N.getOperand(1).getOperand(0); // The global address.
1060 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackyfd42ed62012-06-04 17:36:38 +00001061 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001062 Disp.getOpcode() == ISD::TargetConstantPool ||
1063 Disp.getOpcode() == ISD::TargetJumpTable);
1064 Base = N.getOperand(0);
1065 return true; // [&g+r]
1066 }
1067 } else if (N.getOpcode() == ISD::OR) {
1068 short imm = 0;
Ulrich Weigand347a5072013-05-16 17:58:02 +00001069 if (isIntS16Immediate(N.getOperand(1), imm) &&
1070 (!Aligned || (imm & 3) == 0)) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001071 // If this is an or of disjoint bitfields, we can codegen this as an add
1072 // (for better address arithmetic) if the LHS and RHS of the OR are
1073 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001074 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001075 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling3e98c302008-03-24 23:16:37 +00001076
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001077 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001078 // If all of the bits are known zero on the LHS or RHS, the add won't
1079 // carry.
1080 Base = N.getOperand(0);
Ulrich Weigandf0ef8822013-05-16 14:53:05 +00001081 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001082 return true;
1083 }
1084 }
1085 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1086 // Loading from a constant address.
Scott Michelfdc40a02009-02-17 22:15:04 +00001087
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001088 // If this address fits entirely in a 16-bit sext immediate field, codegen
1089 // this as "d, 0"
1090 short Imm;
Ulrich Weigand347a5072013-05-16 17:58:02 +00001091 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001092 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkel76973702013-03-21 23:45:03 +00001093 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1094 CN->getValueType(0));
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001095 return true;
1096 }
Chris Lattnerbc681d62007-02-17 06:44:03 +00001097
1098 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand347a5072013-05-16 17:58:02 +00001099 if ((CN->getValueType(0) == MVT::i32 ||
1100 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1101 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001102 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001103
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001104 // Otherwise, break this down into an LIS + disp.
Owen Anderson825b72b2009-08-11 20:47:22 +00001105 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00001106
Owen Anderson825b72b2009-08-11 20:47:22 +00001107 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1108 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman602b0c82009-09-25 18:54:59 +00001109 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001110 return true;
1111 }
1112 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001113
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001114 Disp = DAG.getTargetConstant(0, getPointerTy());
1115 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
1116 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1117 else
1118 Base = N;
1119 return true; // [r+0]
1120}
1121
1122/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1123/// represented as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +00001124bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1125 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +00001126 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001127 // Check to see if we can easily represent this as an [r+r] address. This
1128 // will fail if it thinks that the address is more profitably represented as
1129 // reg+imm, e.g. where imm = 0.
1130 if (SelectAddressRegReg(N, Base, Index, DAG))
1131 return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001132
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001133 // If the operand is an addition, always emit this as [r+r], since this is
1134 // better (for code size, and execution, as the memop does the add for free)
1135 // than emitting an explicit add.
1136 if (N.getOpcode() == ISD::ADD) {
1137 Base = N.getOperand(0);
1138 Index = N.getOperand(1);
1139 return true;
1140 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001141
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001142 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkel76973702013-03-21 23:45:03 +00001143 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1144 N.getValueType());
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001145 Index = N;
1146 return true;
1147}
1148
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001149/// getPreIndexedAddressParts - returns true by value, base pointer and
1150/// offset pointer and addressing mode by reference if the node's address
1151/// can be legally represented as pre-indexed load / store address.
Dan Gohman475871a2008-07-27 21:46:04 +00001152bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1153 SDValue &Offset,
Evan Cheng144d8f02006-11-09 17:55:04 +00001154 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00001155 SelectionDAG &DAG) const {
Hal Finkel77838f92012-06-04 02:21:00 +00001156 if (DisablePPCPreinc) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001157
Ulrich Weigand881a7152013-03-22 14:58:48 +00001158 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00001159 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00001160 EVT VT;
Hal Finkel08a215c2013-03-18 23:00:58 +00001161 unsigned Alignment;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001162 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1163 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001164 VT = LD->getMemoryVT();
Hal Finkel08a215c2013-03-18 23:00:58 +00001165 Alignment = LD->getAlignment();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001166 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001167 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001168 VT = ST->getMemoryVT();
Hal Finkel08a215c2013-03-18 23:00:58 +00001169 Alignment = ST->getAlignment();
Ulrich Weigand881a7152013-03-22 14:58:48 +00001170 isLoad = false;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001171 } else
1172 return false;
1173
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001174 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001175 if (VT.isVector())
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001176 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001177
Ulrich Weigand881a7152013-03-22 14:58:48 +00001178 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1179
1180 // Common code will reject creating a pre-inc form if the base pointer
1181 // is a frame index, or if N is a store and the base pointer is either
1182 // the same as or a predecessor of the value being stored. Check for
1183 // those situations here, and try with swapped Base/Offset instead.
1184 bool Swap = false;
1185
1186 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1187 Swap = true;
1188 else if (!isLoad) {
1189 SDValue Val = cast<StoreSDNode>(N)->getValue();
1190 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1191 Swap = true;
1192 }
1193
1194 if (Swap)
1195 std::swap(Base, Offset);
1196
Hal Finkel0fcdd8b2012-06-20 15:43:03 +00001197 AM = ISD::PRE_INC;
1198 return true;
Hal Finkelac81cc32012-06-19 02:34:32 +00001199 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001200
Ulrich Weigand347a5072013-05-16 17:58:02 +00001201 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson825b72b2009-08-11 20:47:22 +00001202 if (VT != MVT::i64) {
Ulrich Weigand347a5072013-05-16 17:58:02 +00001203 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner0851b4f2006-11-15 19:55:13 +00001204 return false;
1205 } else {
Hal Finkel08a215c2013-03-18 23:00:58 +00001206 // LDU/STU need an address with at least 4-byte alignment.
1207 if (Alignment < 4)
1208 return false;
1209
Ulrich Weigand347a5072013-05-16 17:58:02 +00001210 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner0851b4f2006-11-15 19:55:13 +00001211 return false;
1212 }
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001213
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001214 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001215 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1216 // sext i32 to i64 when addr mode is r+i.
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001218 LD->getExtensionType() == ISD::SEXTLOAD &&
1219 isa<ConstantSDNode>(Offset))
1220 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001221 }
1222
Chris Lattner4eab7142006-11-10 02:08:47 +00001223 AM = ISD::PRE_INC;
1224 return true;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001225}
1226
1227//===----------------------------------------------------------------------===//
Chris Lattner1a635d62006-04-14 06:01:58 +00001228// LowerOperation implementation
1229//===----------------------------------------------------------------------===//
1230
Chris Lattner1e61e692010-11-15 02:46:57 +00001231/// GetLabelAccessInfo - Return true if we should reference labels using a
1232/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1233static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Chris Lattner6d2ff122010-11-15 03:13:19 +00001234 unsigned &LoOpFlags, const GlobalValue *GV = 0) {
Ulrich Weigand92cfa612013-06-21 14:42:20 +00001235 HiOpFlags = PPCII::MO_HA;
1236 LoOpFlags = PPCII::MO_LO;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001237
Chris Lattner1e61e692010-11-15 02:46:57 +00001238 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1239 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001240 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattner1e61e692010-11-15 02:46:57 +00001241 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattner6d2ff122010-11-15 03:13:19 +00001242 if (isPIC) {
1243 HiOpFlags |= PPCII::MO_PIC_FLAG;
1244 LoOpFlags |= PPCII::MO_PIC_FLAG;
1245 }
1246
1247 // If this is a reference to a global value that requires a non-lazy-ptr, make
1248 // sure that instruction lowering adds it.
1249 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1250 HiOpFlags |= PPCII::MO_NLP_FLAG;
1251 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001252
Chris Lattner6d2ff122010-11-15 03:13:19 +00001253 if (GV->hasHiddenVisibility()) {
1254 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1255 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1256 }
1257 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001258
Chris Lattner1e61e692010-11-15 02:46:57 +00001259 return isPIC;
1260}
1261
1262static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1263 SelectionDAG &DAG) {
1264 EVT PtrVT = HiPart.getValueType();
1265 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001266 SDLoc DL(HiPart);
Chris Lattner1e61e692010-11-15 02:46:57 +00001267
1268 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1269 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001270
Chris Lattner1e61e692010-11-15 02:46:57 +00001271 // With PIC, the first instruction is actually "GR+hi(&G)".
1272 if (isPIC)
1273 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1274 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001275
Chris Lattner1e61e692010-11-15 02:46:57 +00001276 // Generate non-pic code that has direct accesses to the constant pool.
1277 // The address of the global is just (hi(&g)+lo(&g)).
1278 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1279}
1280
Scott Michelfdc40a02009-02-17 22:15:04 +00001281SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001282 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001283 EVT PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001284 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001285 const Constant *C = CP->getConstVal();
Chris Lattner1a635d62006-04-14 06:01:58 +00001286
Roman Divacky9fb8b492012-08-24 16:26:02 +00001287 // 64-bit SVR4 ABI code is always position-independent.
1288 // The actual address of the GlobalValue is stored in the TOC.
1289 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1290 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001291 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divacky9fb8b492012-08-24 16:26:02 +00001292 DAG.getRegister(PPC::X2, MVT::i64));
1293 }
1294
Chris Lattner1e61e692010-11-15 02:46:57 +00001295 unsigned MOHiFlag, MOLoFlag;
1296 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1297 SDValue CPIHi =
1298 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1299 SDValue CPILo =
1300 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1301 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00001302}
1303
Dan Gohmand858e902010-04-17 15:26:15 +00001304SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001305 EVT PtrVT = Op.getValueType();
Nate Begeman37efe672006-04-22 18:53:45 +00001306 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001307
Roman Divacky9fb8b492012-08-24 16:26:02 +00001308 // 64-bit SVR4 ABI code is always position-independent.
1309 // The actual address of the GlobalValue is stored in the TOC.
1310 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1311 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001312 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divacky9fb8b492012-08-24 16:26:02 +00001313 DAG.getRegister(PPC::X2, MVT::i64));
1314 }
1315
Chris Lattner1e61e692010-11-15 02:46:57 +00001316 unsigned MOHiFlag, MOLoFlag;
1317 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1318 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1319 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1320 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001321}
1322
Dan Gohmand858e902010-04-17 15:26:15 +00001323SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1324 SelectionDAG &DAG) const {
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001325 EVT PtrVT = Op.getValueType();
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001326
Dan Gohman46510a72010-04-15 01:51:59 +00001327 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001328
Chris Lattner1e61e692010-11-15 02:46:57 +00001329 unsigned MOHiFlag, MOLoFlag;
1330 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001331 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1332 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattner1e61e692010-11-15 02:46:57 +00001333 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1334}
1335
Roman Divackyfd42ed62012-06-04 17:36:38 +00001336SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1337 SelectionDAG &DAG) const {
1338
1339 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001340 SDLoc dl(GA);
Roman Divackyfd42ed62012-06-04 17:36:38 +00001341 const GlobalValue *GV = GA->getGlobal();
1342 EVT PtrVT = getPointerTy();
1343 bool is64bit = PPCSubTarget.isPPC64();
1344
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001345 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackyfd42ed62012-06-04 17:36:38 +00001346
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001347 if (Model == TLSModel::LocalExec) {
1348 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigand92cfa612013-06-21 14:42:20 +00001349 PPCII::MO_TPREL_HA);
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001350 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigand92cfa612013-06-21 14:42:20 +00001351 PPCII::MO_TPREL_LO);
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001352 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1353 is64bit ? MVT::i64 : MVT::i32);
1354 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1355 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1356 }
Roman Divackyfd42ed62012-06-04 17:36:38 +00001357
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001358 if (!is64bit)
1359 llvm_unreachable("only local-exec is currently supported for ppc32");
1360
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001361 if (Model == TLSModel::InitialExec) {
Bill Schmidtdfebc4c2012-12-13 18:45:54 +00001362 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand23a72c82013-07-05 12:22:36 +00001363 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1364 PPCII::MO_TLS);
Bill Schmidtdfebc4c2012-12-13 18:45:54 +00001365 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
Bill Schmidtb453e162012-12-14 17:02:38 +00001366 SDValue TPOffsetHi = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1367 PtrVT, GOTReg, TGA);
1368 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
1369 PtrVT, TGA, TPOffsetHi);
Ulrich Weigand23a72c82013-07-05 12:22:36 +00001370 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001371 }
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001372
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001373 if (Model == TLSModel::GeneralDynamic) {
1374 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1375 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1376 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1377 GOTReg, TGA);
1378 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1379 GOTEntryHi, TGA);
1380
1381 // We need a chain node, and don't have one handy. The underlying
1382 // call has no side effects, so using the function entry node
1383 // suffices.
1384 SDValue Chain = DAG.getEntryNode();
1385 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1386 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1387 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1388 PtrVT, ParmReg, TGA);
Bill Schmidt349c2782012-12-12 19:29:35 +00001389 // The return value from GET_TLS_ADDR really is in X3 already, but
1390 // some hacks are needed here to tie everything together. The extra
1391 // copies dissolve during subsequent transforms.
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001392 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1393 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1394 }
1395
Bill Schmidt349c2782012-12-12 19:29:35 +00001396 if (Model == TLSModel::LocalDynamic) {
1397 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1398 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1399 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1400 GOTReg, TGA);
1401 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1402 GOTEntryHi, TGA);
1403
1404 // We need a chain node, and don't have one handy. The underlying
1405 // call has no side effects, so using the function entry node
1406 // suffices.
1407 SDValue Chain = DAG.getEntryNode();
1408 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1409 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1410 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1411 PtrVT, ParmReg, TGA);
1412 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1413 // some hacks are needed here to tie everything together. The extra
1414 // copies dissolve during subsequent transforms.
1415 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1416 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt1e18b862012-12-13 20:57:10 +00001417 Chain, ParmReg, TGA);
Bill Schmidt349c2782012-12-12 19:29:35 +00001418 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1419 }
1420
1421 llvm_unreachable("Unknown TLS model!");
Roman Divackyfd42ed62012-06-04 17:36:38 +00001422}
1423
Chris Lattner1e61e692010-11-15 02:46:57 +00001424SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1425 SelectionDAG &DAG) const {
1426 EVT PtrVT = Op.getValueType();
1427 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001428 SDLoc DL(GSDN);
Chris Lattner1e61e692010-11-15 02:46:57 +00001429 const GlobalValue *GV = GSDN->getGlobal();
1430
Chris Lattner1e61e692010-11-15 02:46:57 +00001431 // 64-bit SVR4 ABI code is always position-independent.
1432 // The actual address of the GlobalValue is stored in the TOC.
1433 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1434 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1435 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1436 DAG.getRegister(PPC::X2, MVT::i64));
1437 }
1438
Chris Lattner6d2ff122010-11-15 03:13:19 +00001439 unsigned MOHiFlag, MOLoFlag;
1440 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattner1e61e692010-11-15 02:46:57 +00001441
Chris Lattner6d2ff122010-11-15 03:13:19 +00001442 SDValue GAHi =
1443 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1444 SDValue GALo =
1445 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001446
Chris Lattner6d2ff122010-11-15 03:13:19 +00001447 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001448
Chris Lattner6d2ff122010-11-15 03:13:19 +00001449 // If the global reference is actually to a non-lazy-pointer, we have to do an
1450 // extra load to get the address of the global.
1451 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1452 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001453 false, false, false, 0);
Chris Lattner6d2ff122010-11-15 03:13:19 +00001454 return Ptr;
Chris Lattner1a635d62006-04-14 06:01:58 +00001455}
1456
Dan Gohmand858e902010-04-17 15:26:15 +00001457SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner1a635d62006-04-14 06:01:58 +00001458 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001459 SDLoc dl(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00001460
Chris Lattner1a635d62006-04-14 06:01:58 +00001461 // If we're comparing for equality to zero, expose the fact that this is
1462 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1463 // fold the new nodes.
1464 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1465 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Andersone50ed302009-08-10 22:56:29 +00001466 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001467 SDValue Zext = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001468 if (VT.bitsLT(MVT::i32)) {
1469 VT = MVT::i32;
Dale Johannesenf5d97892009-02-04 01:48:28 +00001470 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001471 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00001472 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001473 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1474 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson825b72b2009-08-11 20:47:22 +00001475 DAG.getConstant(Log2b, MVT::i32));
1476 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner1a635d62006-04-14 06:01:58 +00001477 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001478 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner1a635d62006-04-14 06:01:58 +00001479 // optimized. FIXME: revisit this when we can custom lower all setcc
1480 // optimizations.
1481 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman475871a2008-07-27 21:46:04 +00001482 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001483 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001484
Chris Lattner1a635d62006-04-14 06:01:58 +00001485 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattnerac011bc2006-11-14 05:28:08 +00001486 // by xor'ing the rhs with the lhs, which is faster than setting a
1487 // condition register, reading it back out, and masking the correct bit. The
1488 // normal approach here uses sub to do this instead of xor. Using xor exposes
1489 // the result to other bit-twiddling opportunities.
Owen Andersone50ed302009-08-10 22:56:29 +00001490 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001491 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001492 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001493 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner1a635d62006-04-14 06:01:58 +00001494 Op.getOperand(1));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001495 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner1a635d62006-04-14 06:01:58 +00001496 }
Dan Gohman475871a2008-07-27 21:46:04 +00001497 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001498}
1499
Dan Gohman475871a2008-07-27 21:46:04 +00001500SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001501 const PPCSubtarget &Subtarget) const {
Roman Divackybdb226e2011-06-28 15:30:42 +00001502 SDNode *Node = Op.getNode();
1503 EVT VT = Node->getValueType(0);
1504 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1505 SDValue InChain = Node->getOperand(0);
1506 SDValue VAListPtr = Node->getOperand(1);
1507 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001508 SDLoc dl(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001509
Roman Divackybdb226e2011-06-28 15:30:42 +00001510 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1511
1512 // gpr_index
1513 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1514 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1515 false, false, 0);
1516 InChain = GprIndex.getValue(1);
1517
1518 if (VT == MVT::i64) {
1519 // Check if GprIndex is even
1520 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1521 DAG.getConstant(1, MVT::i32));
1522 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1523 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1524 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1525 DAG.getConstant(1, MVT::i32));
1526 // Align GprIndex to be even if it isn't
1527 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1528 GprIndex);
1529 }
1530
1531 // fpr index is 1 byte after gpr
1532 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1533 DAG.getConstant(1, MVT::i32));
1534
1535 // fpr
1536 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1537 FprPtr, MachinePointerInfo(SV), MVT::i8,
1538 false, false, 0);
1539 InChain = FprIndex.getValue(1);
1540
1541 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1542 DAG.getConstant(8, MVT::i32));
1543
1544 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1545 DAG.getConstant(4, MVT::i32));
1546
1547 // areas
1548 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001549 MachinePointerInfo(), false, false,
1550 false, 0);
Roman Divackybdb226e2011-06-28 15:30:42 +00001551 InChain = OverflowArea.getValue(1);
1552
1553 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001554 MachinePointerInfo(), false, false,
1555 false, 0);
Roman Divackybdb226e2011-06-28 15:30:42 +00001556 InChain = RegSaveArea.getValue(1);
1557
1558 // select overflow_area if index > 8
1559 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1560 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1561
Roman Divackybdb226e2011-06-28 15:30:42 +00001562 // adjustment constant gpr_index * 4/8
1563 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1564 VT.isInteger() ? GprIndex : FprIndex,
1565 DAG.getConstant(VT.isInteger() ? 4 : 8,
1566 MVT::i32));
1567
1568 // OurReg = RegSaveArea + RegConstant
1569 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1570 RegConstant);
1571
1572 // Floating types are 32 bytes into RegSaveArea
1573 if (VT.isFloatingPoint())
1574 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1575 DAG.getConstant(32, MVT::i32));
1576
1577 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1578 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1579 VT.isInteger() ? GprIndex : FprIndex,
1580 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1581 MVT::i32));
1582
1583 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1584 VT.isInteger() ? VAListPtr : FprPtr,
1585 MachinePointerInfo(SV),
1586 MVT::i8, false, false, 0);
1587
1588 // determine if we should load from reg_save_area or overflow_area
1589 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1590
1591 // increase overflow_area by 4/8 if gpr/fpr > 8
1592 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1593 DAG.getConstant(VT.isInteger() ? 4 : 8,
1594 MVT::i32));
1595
1596 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1597 OverflowAreaPlusN);
1598
1599 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1600 OverflowAreaPtr,
1601 MachinePointerInfo(),
1602 MVT::i32, false, false, 0);
1603
NAKAMURA Takumi25f6b5a2012-08-30 15:52:23 +00001604 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001605 false, false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001606}
1607
Duncan Sands4a544a72011-09-06 13:37:06 +00001608SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1609 SelectionDAG &DAG) const {
1610 return Op.getOperand(0);
1611}
1612
1613SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1614 SelectionDAG &DAG) const {
Bill Wendling77959322008-09-17 00:30:57 +00001615 SDValue Chain = Op.getOperand(0);
1616 SDValue Trmp = Op.getOperand(1); // trampoline
1617 SDValue FPtr = Op.getOperand(2); // nested function
1618 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickac6d9be2013-05-25 02:42:55 +00001619 SDLoc dl(Op);
Bill Wendling77959322008-09-17 00:30:57 +00001620
Owen Andersone50ed302009-08-10 22:56:29 +00001621 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00001622 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001623 Type *IntPtrTy =
Micah Villmow3574eca2012-10-08 16:38:25 +00001624 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruthece6c6b2012-11-01 08:07:29 +00001625 *DAG.getContext());
Bill Wendling77959322008-09-17 00:30:57 +00001626
Scott Michelfdc40a02009-02-17 22:15:04 +00001627 TargetLowering::ArgListTy Args;
Bill Wendling77959322008-09-17 00:30:57 +00001628 TargetLowering::ArgListEntry Entry;
1629
1630 Entry.Ty = IntPtrTy;
1631 Entry.Node = Trmp; Args.push_back(Entry);
1632
1633 // TrampSize == (isPPC64 ? 48 : 40);
1634 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson825b72b2009-08-11 20:47:22 +00001635 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling77959322008-09-17 00:30:57 +00001636 Args.push_back(Entry);
1637
1638 Entry.Node = FPtr; Args.push_back(Entry);
1639 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelfdc40a02009-02-17 22:15:04 +00001640
Bill Wendling77959322008-09-17 00:30:57 +00001641 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001642 TargetLowering::CallLoweringInfo CLI(Chain,
1643 Type::getVoidTy(*DAG.getContext()),
1644 false, false, false, false, 0,
1645 CallingConv::C,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001646 /*isTailCall=*/false,
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001647 /*doesNotRet=*/false,
1648 /*isReturnValueUsed=*/true,
Bill Wendling77959322008-09-17 00:30:57 +00001649 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Bill Wendling46ada192010-03-02 01:55:18 +00001650 Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001651 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bill Wendling77959322008-09-17 00:30:57 +00001652
Duncan Sands4a544a72011-09-06 13:37:06 +00001653 return CallResult.second;
Bill Wendling77959322008-09-17 00:30:57 +00001654}
1655
Dan Gohman475871a2008-07-27 21:46:04 +00001656SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001657 const PPCSubtarget &Subtarget) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001658 MachineFunction &MF = DAG.getMachineFunction();
1659 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1660
Andrew Trickac6d9be2013-05-25 02:42:55 +00001661 SDLoc dl(Op);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001662
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001663 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray01119992007-04-03 13:59:52 +00001664 // vastart just stores the address of the VarArgsFrameIndex slot into the
1665 // memory location argument.
Owen Andersone50ed302009-08-10 22:56:29 +00001666 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001667 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001668 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner6229d0a2010-09-21 18:41:36 +00001669 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1670 MachinePointerInfo(SV),
David Greene534502d12010-02-15 16:56:53 +00001671 false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001672 }
1673
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001674 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray01119992007-04-03 13:59:52 +00001675 // We suppose the given va_list is already allocated.
1676 //
1677 // typedef struct {
1678 // char gpr; /* index into the array of 8 GPRs
1679 // * stored in the register save area
1680 // * gpr=0 corresponds to r3,
1681 // * gpr=1 to r4, etc.
1682 // */
1683 // char fpr; /* index into the array of 8 FPRs
1684 // * stored in the register save area
1685 // * fpr=0 corresponds to f1,
1686 // * fpr=1 to f2, etc.
1687 // */
1688 // char *overflow_arg_area;
1689 // /* location on stack that holds
1690 // * the next overflow argument
1691 // */
1692 // char *reg_save_area;
1693 // /* where r3:r10 and f1:f8 (if saved)
1694 // * are stored
1695 // */
1696 // } va_list[1];
1697
1698
Dan Gohman1e93df62010-04-17 14:41:14 +00001699 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1700 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00001701
Nicolas Geoffray01119992007-04-03 13:59:52 +00001702
Owen Andersone50ed302009-08-10 22:56:29 +00001703 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfdc40a02009-02-17 22:15:04 +00001704
Dan Gohman1e93df62010-04-17 14:41:14 +00001705 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1706 PtrVT);
1707 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1708 PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001709
Duncan Sands83ec4b62008-06-06 12:08:01 +00001710 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman475871a2008-07-27 21:46:04 +00001711 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001712
Duncan Sands83ec4b62008-06-06 12:08:01 +00001713 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001714 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001715
1716 uint64_t FPROffset = 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001717 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001718
Dan Gohman69de1932008-02-06 22:27:42 +00001719 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001720
Nicolas Geoffray01119992007-04-03 13:59:52 +00001721 // Store first byte : number of int regs
Tilmann Schellerffd02002009-07-03 06:45:56 +00001722 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001723 Op.getOperand(1),
1724 MachinePointerInfo(SV),
1725 MVT::i8, false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001726 uint64_t nextOffset = FPROffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001727 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray01119992007-04-03 13:59:52 +00001728 ConstFPROffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001729
Nicolas Geoffray01119992007-04-03 13:59:52 +00001730 // Store second byte : number of float regs
Dan Gohman475871a2008-07-27 21:46:04 +00001731 SDValue secondStore =
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001732 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1733 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene534502d12010-02-15 16:56:53 +00001734 false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001735 nextOffset += StackOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001736 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001737
Nicolas Geoffray01119992007-04-03 13:59:52 +00001738 // Store second word : arguments given on stack
Dan Gohman475871a2008-07-27 21:46:04 +00001739 SDValue thirdStore =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001740 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1741 MachinePointerInfo(SV, nextOffset),
David Greene534502d12010-02-15 16:56:53 +00001742 false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001743 nextOffset += FrameOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001744 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001745
1746 // Store third word : arguments given in registers
Chris Lattner6229d0a2010-09-21 18:41:36 +00001747 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1748 MachinePointerInfo(SV, nextOffset),
David Greene534502d12010-02-15 16:56:53 +00001749 false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001750
Chris Lattner1a635d62006-04-14 06:01:58 +00001751}
1752
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00001753#include "PPCGenCallingConv.inc"
1754
Bill Schmidtd3f77662013-06-12 16:39:22 +00001755bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1756 CCValAssign::LocInfo &LocInfo,
1757 ISD::ArgFlagsTy &ArgFlags,
1758 CCState &State) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001759 return true;
1760}
1761
Bill Schmidtd3f77662013-06-12 16:39:22 +00001762bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
1763 MVT &LocVT,
1764 CCValAssign::LocInfo &LocInfo,
1765 ISD::ArgFlagsTy &ArgFlags,
1766 CCState &State) {
Craig Topperc5eaae42012-03-11 07:57:25 +00001767 static const uint16_t ArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001768 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1769 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1770 };
1771 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001772
Tilmann Schellerffd02002009-07-03 06:45:56 +00001773 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1774
1775 // Skip one register if the first unallocated register has an even register
1776 // number and there are still argument registers available which have not been
1777 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1778 // need to skip a register if RegNum is odd.
1779 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1780 State.AllocateReg(ArgRegs[RegNum]);
1781 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001782
Tilmann Schellerffd02002009-07-03 06:45:56 +00001783 // Always return false here, as this function only makes sure that the first
1784 // unallocated register has an odd register number and does not actually
1785 // allocate a register for the current argument.
1786 return false;
1787}
1788
Bill Schmidtd3f77662013-06-12 16:39:22 +00001789bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
1790 MVT &LocVT,
1791 CCValAssign::LocInfo &LocInfo,
1792 ISD::ArgFlagsTy &ArgFlags,
1793 CCState &State) {
Craig Topperc5eaae42012-03-11 07:57:25 +00001794 static const uint16_t ArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001795 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1796 PPC::F8
1797 };
1798
1799 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001800
Tilmann Schellerffd02002009-07-03 06:45:56 +00001801 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1802
1803 // If there is only one Floating-point register left we need to put both f64
1804 // values of a split ppc_fp128 value on the stack.
1805 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1806 State.AllocateReg(ArgRegs[RegNum]);
1807 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001808
Tilmann Schellerffd02002009-07-03 06:45:56 +00001809 // Always return false here, as this function only makes sure that the two f64
1810 // values a ppc_fp128 value is split into are both passed in registers or both
1811 // passed on the stack and does not actually allocate a register for the
1812 // current argument.
1813 return false;
1814}
1815
Chris Lattner9f0bc652007-02-25 05:34:32 +00001816/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001817/// on Darwin.
Craig Topperb78ca422012-03-11 07:16:55 +00001818static const uint16_t *GetFPR() {
1819 static const uint16_t FPR[] = {
Chris Lattner9f0bc652007-02-25 05:34:32 +00001820 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001821 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner9f0bc652007-02-25 05:34:32 +00001822 };
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001823
Chris Lattner9f0bc652007-02-25 05:34:32 +00001824 return FPR;
1825}
1826
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001827/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1828/// the stack.
Owen Andersone50ed302009-08-10 22:56:29 +00001829static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001830 unsigned PtrByteSize) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001831 unsigned ArgSize = ArgVT.getSizeInBits()/8;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001832 if (Flags.isByVal())
1833 ArgSize = Flags.getByValSize();
1834 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1835
1836 return ArgSize;
1837}
1838
Dan Gohman475871a2008-07-27 21:46:04 +00001839SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001840PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001841 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001842 const SmallVectorImpl<ISD::InputArg>
1843 &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001844 SDLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001845 SmallVectorImpl<SDValue> &InVals)
1846 const {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00001847 if (PPCSubTarget.isSVR4ABI()) {
1848 if (PPCSubTarget.isPPC64())
1849 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
1850 dl, DAG, InVals);
1851 else
1852 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
1853 dl, DAG, InVals);
Bill Schmidt419f3762012-09-19 15:42:13 +00001854 } else {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00001855 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
1856 dl, DAG, InVals);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001857 }
1858}
1859
1860SDValue
Bill Schmidt419f3762012-09-19 15:42:13 +00001861PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohman98ca4f22009-08-05 01:29:28 +00001862 SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001863 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001864 const SmallVectorImpl<ISD::InputArg>
1865 &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001866 SDLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001867 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001868
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001869 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001870 // +-----------------------------------+
1871 // +--> | Back chain |
1872 // | +-----------------------------------+
1873 // | | Floating-point register save area |
1874 // | +-----------------------------------+
1875 // | | General register save area |
1876 // | +-----------------------------------+
1877 // | | CR save word |
1878 // | +-----------------------------------+
1879 // | | VRSAVE save word |
1880 // | +-----------------------------------+
1881 // | | Alignment padding |
1882 // | +-----------------------------------+
1883 // | | Vector register save area |
1884 // | +-----------------------------------+
1885 // | | Local variable space |
1886 // | +-----------------------------------+
1887 // | | Parameter list area |
1888 // | +-----------------------------------+
1889 // | | LR save word |
1890 // | +-----------------------------------+
1891 // SP--> +--- | Back chain |
1892 // +-----------------------------------+
1893 //
1894 // Specifications:
1895 // System V Application Binary Interface PowerPC Processor Supplement
1896 // AltiVec Technology Programming Interface Manual
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001897
Tilmann Schellerffd02002009-07-03 06:45:56 +00001898 MachineFunction &MF = DAG.getMachineFunction();
1899 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +00001900 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerffd02002009-07-03 06:45:56 +00001901
Owen Andersone50ed302009-08-10 22:56:29 +00001902 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerffd02002009-07-03 06:45:56 +00001903 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001904 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
1905 (CallConv == CallingConv::Fast));
Tilmann Schellerffd02002009-07-03 06:45:56 +00001906 unsigned PtrByteSize = 4;
1907
1908 // Assign locations to all of the incoming arguments.
1909 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001910 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00001911 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00001912
1913 // Reserve space for the linkage area on the stack.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001914 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001915
Bill Schmidt212af6a2013-02-06 17:33:58 +00001916 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001917
Tilmann Schellerffd02002009-07-03 06:45:56 +00001918 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1919 CCValAssign &VA = ArgLocs[i];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001920
Tilmann Schellerffd02002009-07-03 06:45:56 +00001921 // Arguments stored in registers.
1922 if (VA.isRegLoc()) {
Craig Topper44d23822012-02-22 05:59:10 +00001923 const TargetRegisterClass *RC;
Owen Andersone50ed302009-08-10 22:56:29 +00001924 EVT ValVT = VA.getValVT();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001925
Owen Anderson825b72b2009-08-11 20:47:22 +00001926 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001927 default:
Dan Gohman98ca4f22009-08-05 01:29:28 +00001928 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Owen Anderson825b72b2009-08-11 20:47:22 +00001929 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +00001930 RC = &PPC::GPRCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001931 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001932 case MVT::f32:
Craig Topperc9099502012-04-20 06:31:50 +00001933 RC = &PPC::F4RCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001934 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001935 case MVT::f64:
Craig Topperc9099502012-04-20 06:31:50 +00001936 RC = &PPC::F8RCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001937 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001938 case MVT::v16i8:
1939 case MVT::v8i16:
1940 case MVT::v4i32:
1941 case MVT::v4f32:
Craig Topperc9099502012-04-20 06:31:50 +00001942 RC = &PPC::VRRCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001943 break;
1944 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001945
Tilmann Schellerffd02002009-07-03 06:45:56 +00001946 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00001947 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001948 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, ValVT);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001949
Dan Gohman98ca4f22009-08-05 01:29:28 +00001950 InVals.push_back(ArgValue);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001951 } else {
1952 // Argument stored in memory.
1953 assert(VA.isMemLoc());
1954
1955 unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8;
1956 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Chenged2ae132010-07-03 00:40:23 +00001957 isImmutable);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001958
1959 // Create load nodes to retrieve arguments from the stack.
1960 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001961 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1962 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001963 false, false, false, 0));
Tilmann Schellerffd02002009-07-03 06:45:56 +00001964 }
1965 }
1966
1967 // Assign locations to all of the incoming aggregate by value arguments.
1968 // Aggregates passed by value are stored in the local variable space of the
1969 // caller's stack frame, right above the parameter list area.
1970 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001971 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00001972 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00001973
1974 // Reserve stack space for the allocations in CCInfo.
1975 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
1976
Bill Schmidt212af6a2013-02-06 17:33:58 +00001977 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001978
1979 // Area that is at least reserved in the caller of this function.
1980 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001981
Tilmann Schellerffd02002009-07-03 06:45:56 +00001982 // Set the size that is at least reserved in caller of this function. Tail
1983 // call optimized function's reserved stack space needs to be aligned so that
1984 // taking the difference between two stack areas will result in an aligned
1985 // stack.
1986 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1987
1988 MinReservedArea =
1989 std::max(MinReservedArea,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001990 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001991
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001992 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerffd02002009-07-03 06:45:56 +00001993 getStackAlignment();
1994 unsigned AlignMask = TargetAlign-1;
1995 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001996
Tilmann Schellerffd02002009-07-03 06:45:56 +00001997 FI->setMinReservedArea(MinReservedArea);
1998
1999 SmallVector<SDValue, 8> MemOps;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002000
Tilmann Schellerffd02002009-07-03 06:45:56 +00002001 // If the function takes variable number of arguments, make a frame index for
2002 // the start of the first vararg value... for expansion of llvm.va_start.
2003 if (isVarArg) {
Craig Topperc5eaae42012-03-11 07:57:25 +00002004 static const uint16_t GPArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002005 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2006 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2007 };
2008 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2009
Craig Topperc5eaae42012-03-11 07:57:25 +00002010 static const uint16_t FPArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002011 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2012 PPC::F8
2013 };
2014 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2015
Dan Gohman1e93df62010-04-17 14:41:14 +00002016 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2017 NumGPArgRegs));
2018 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2019 NumFPArgRegs));
Tilmann Schellerffd02002009-07-03 06:45:56 +00002020
2021 // Make room for NumGPArgRegs and NumFPArgRegs.
2022 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson825b72b2009-08-11 20:47:22 +00002023 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002024
Dan Gohman1e93df62010-04-17 14:41:14 +00002025 FuncInfo->setVarArgsStackOffset(
2026 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00002027 CCInfo.getNextStackOffset(), true));
Tilmann Schellerffd02002009-07-03 06:45:56 +00002028
Dan Gohman1e93df62010-04-17 14:41:14 +00002029 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2030 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002031
Jakob Stoklund Olesen4f9af2e2010-10-11 20:43:09 +00002032 // The fixed integer arguments of a variadic function are stored to the
2033 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2034 // the result of va_next.
2035 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2036 // Get an existing live-in vreg, or add a new one.
2037 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2038 if (!VReg)
Devang Patel68e6bee2011-02-21 23:21:26 +00002039 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002040
Dan Gohman98ca4f22009-08-05 01:29:28 +00002041 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002042 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2043 MachinePointerInfo(), false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002044 MemOps.push_back(Store);
2045 // Increment the address by four for the next argument to store
2046 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2047 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2048 }
2049
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002050 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2051 // is set.
Tilmann Schellerffd02002009-07-03 06:45:56 +00002052 // The double arguments are stored to the VarArgsFrameIndex
2053 // on the stack.
Jakob Stoklund Olesen4f9af2e2010-10-11 20:43:09 +00002054 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2055 // Get an existing live-in vreg, or add a new one.
2056 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2057 if (!VReg)
Devang Patel68e6bee2011-02-21 23:21:26 +00002058 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002059
Owen Anderson825b72b2009-08-11 20:47:22 +00002060 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002061 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2062 MachinePointerInfo(), false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002063 MemOps.push_back(Store);
2064 // Increment the address by eight for the next argument to store
Owen Anderson825b72b2009-08-11 20:47:22 +00002065 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerffd02002009-07-03 06:45:56 +00002066 PtrVT);
2067 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2068 }
2069 }
2070
2071 if (!MemOps.empty())
Dan Gohman98ca4f22009-08-05 01:29:28 +00002072 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002073 MVT::Other, &MemOps[0], MemOps.size());
Tilmann Schellerffd02002009-07-03 06:45:56 +00002074
Dan Gohman98ca4f22009-08-05 01:29:28 +00002075 return Chain;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002076}
2077
Bill Schmidt726c2372012-10-23 15:51:16 +00002078// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2079// value to MVT::i64 and then truncate to the correct register size.
2080SDValue
2081PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2082 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002083 SDLoc dl) const {
Bill Schmidt726c2372012-10-23 15:51:16 +00002084 if (Flags.isSExt())
2085 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2086 DAG.getValueType(ObjectVT));
2087 else if (Flags.isZExt())
2088 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2089 DAG.getValueType(ObjectVT));
Matt Arsenault225ed702013-05-18 00:21:46 +00002090
Bill Schmidt726c2372012-10-23 15:51:16 +00002091 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
2092}
2093
2094// Set the size that is at least reserved in caller of this function. Tail
2095// call optimized functions' reserved stack space needs to be aligned so that
2096// taking the difference between two stack areas will result in an aligned
2097// stack.
2098void
2099PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2100 unsigned nAltivecParamsAtEnd,
2101 unsigned MinReservedArea,
2102 bool isPPC64) const {
2103 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2104 // Add the Altivec parameters at the end, if needed.
2105 if (nAltivecParamsAtEnd) {
2106 MinReservedArea = ((MinReservedArea+15)/16)*16;
2107 MinReservedArea += 16*nAltivecParamsAtEnd;
2108 }
2109 MinReservedArea =
2110 std::max(MinReservedArea,
2111 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2112 unsigned TargetAlign
2113 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2114 getStackAlignment();
2115 unsigned AlignMask = TargetAlign-1;
2116 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2117 FI->setMinReservedArea(MinReservedArea);
2118}
2119
Tilmann Schellerffd02002009-07-03 06:45:56 +00002120SDValue
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002121PPCTargetLowering::LowerFormalArguments_64SVR4(
2122 SDValue Chain,
2123 CallingConv::ID CallConv, bool isVarArg,
2124 const SmallVectorImpl<ISD::InputArg>
2125 &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002126 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002127 SmallVectorImpl<SDValue> &InVals) const {
2128 // TODO: add description of PPC stack frame format, or at least some docs.
2129 //
2130 MachineFunction &MF = DAG.getMachineFunction();
2131 MachineFrameInfo *MFI = MF.getFrameInfo();
2132 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2133
2134 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2135 // Potential tail calls could cause overwriting of argument stack slots.
2136 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2137 (CallConv == CallingConv::Fast));
2138 unsigned PtrByteSize = 8;
2139
2140 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2141 // Area that is at least reserved in caller of this function.
2142 unsigned MinReservedArea = ArgOffset;
2143
2144 static const uint16_t GPR[] = {
2145 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2146 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2147 };
2148
2149 static const uint16_t *FPR = GetFPR();
2150
2151 static const uint16_t VR[] = {
2152 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2153 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2154 };
2155
2156 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2157 const unsigned Num_FPR_Regs = 13;
2158 const unsigned Num_VR_Regs = array_lengthof(VR);
2159
2160 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2161
2162 // Add DAG nodes to load the arguments or copy them out of registers. On
2163 // entry to a function on PPC, the arguments start after the linkage area,
2164 // although the first ones are often in registers.
2165
2166 SmallVector<SDValue, 8> MemOps;
2167 unsigned nAltivecParamsAtEnd = 0;
2168 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt49deebb2013-02-20 17:31:41 +00002169 unsigned CurArgIdx = 0;
2170 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002171 SDValue ArgVal;
2172 bool needsLoad = false;
2173 EVT ObjectVT = Ins[ArgNo].VT;
2174 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
2175 unsigned ArgSize = ObjSize;
2176 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt49deebb2013-02-20 17:31:41 +00002177 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2178 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002179
2180 unsigned CurArgOffset = ArgOffset;
2181
2182 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2183 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2184 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
2185 if (isVarArg) {
2186 MinReservedArea = ((MinReservedArea+15)/16)*16;
2187 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2188 Flags,
2189 PtrByteSize);
2190 } else
2191 nAltivecParamsAtEnd++;
2192 } else
2193 // Calculate min reserved area.
2194 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2195 Flags,
2196 PtrByteSize);
2197
2198 // FIXME the codegen can be much improved in some cases.
2199 // We do not have to keep everything in memory.
2200 if (Flags.isByVal()) {
2201 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2202 ObjSize = Flags.getByValSize();
2203 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt42d43352012-10-31 01:15:05 +00002204 // Empty aggregate parameters do not take up registers. Examples:
2205 // struct { } a;
2206 // union { } b;
2207 // int c[0];
2208 // etc. However, we have to provide a place-holder in InVals, so
2209 // pretend we have an 8-byte item at the current address for that
2210 // purpose.
2211 if (!ObjSize) {
2212 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2213 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2214 InVals.push_back(FIN);
2215 continue;
2216 }
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002217 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt7a6cb152012-10-16 13:30:53 +00002218 if (ObjSize < PtrByteSize)
2219 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002220 // The value of the object is its address.
2221 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2222 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2223 InVals.push_back(FIN);
Bill Schmidt37900c52012-10-25 13:38:09 +00002224
2225 if (ObjSize < 8) {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002226 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt37900c52012-10-25 13:38:09 +00002227 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002228 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt37900c52012-10-25 13:38:09 +00002229 SDValue Store;
2230
2231 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2232 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2233 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2234 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
2235 MachinePointerInfo(FuncArg, CurArgOffset),
2236 ObjType, false, false, 0);
2237 } else {
2238 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2239 // store the whole register as-is to the parameter save area
2240 // slot. The address of the parameter was already calculated
2241 // above (InVals.push_back(FIN)) to be the right-justified
2242 // offset within the slot. For this store, we need a new
2243 // frame index that points at the beginning of the slot.
2244 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2245 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2246 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2247 MachinePointerInfo(FuncArg, ArgOffset),
2248 false, false, 0);
2249 }
2250
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002251 MemOps.push_back(Store);
2252 ++GPR_idx;
2253 }
Bill Schmidt37900c52012-10-25 13:38:09 +00002254 // Whether we copied from a register or not, advance the offset
2255 // into the parameter save area by a full doubleword.
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002256 ArgOffset += PtrByteSize;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002257 continue;
2258 }
Bill Schmidt37900c52012-10-25 13:38:09 +00002259
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002260 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2261 // Store whatever pieces of the object are in registers
2262 // to memory. ArgOffset will be the address of the beginning
2263 // of the object.
2264 if (GPR_idx != Num_GPR_Regs) {
2265 unsigned VReg;
2266 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2267 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2268 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2269 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt37900c52012-10-25 13:38:09 +00002270 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002271 MachinePointerInfo(FuncArg, ArgOffset),
2272 false, false, 0);
2273 MemOps.push_back(Store);
2274 ++GPR_idx;
2275 ArgOffset += PtrByteSize;
2276 } else {
Bill Schmidt7a6cb152012-10-16 13:30:53 +00002277 ArgOffset += ArgSize - j;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002278 break;
2279 }
2280 }
2281 continue;
2282 }
2283
2284 switch (ObjectVT.getSimpleVT().SimpleTy) {
2285 default: llvm_unreachable("Unhandled argument type!");
2286 case MVT::i32:
2287 case MVT::i64:
2288 if (GPR_idx != Num_GPR_Regs) {
2289 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2290 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2291
Bill Schmidt726c2372012-10-23 15:51:16 +00002292 if (ObjectVT == MVT::i32)
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002293 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2294 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt726c2372012-10-23 15:51:16 +00002295 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002296
2297 ++GPR_idx;
2298 } else {
2299 needsLoad = true;
2300 ArgSize = PtrByteSize;
2301 }
2302 ArgOffset += 8;
2303 break;
2304
2305 case MVT::f32:
2306 case MVT::f64:
2307 // Every 8 bytes of argument space consumes one of the GPRs available for
2308 // argument passing.
2309 if (GPR_idx != Num_GPR_Regs) {
2310 ++GPR_idx;
2311 }
2312 if (FPR_idx != Num_FPR_Regs) {
2313 unsigned VReg;
2314
2315 if (ObjectVT == MVT::f32)
2316 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2317 else
2318 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
2319
2320 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2321 ++FPR_idx;
2322 } else {
2323 needsLoad = true;
Bill Schmidta867f372012-10-11 15:38:20 +00002324 ArgSize = PtrByteSize;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002325 }
2326
2327 ArgOffset += 8;
2328 break;
2329 case MVT::v4f32:
2330 case MVT::v4i32:
2331 case MVT::v8i16:
2332 case MVT::v16i8:
2333 // Note that vector arguments in registers don't reserve stack space,
2334 // except in varargs functions.
2335 if (VR_idx != Num_VR_Regs) {
2336 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
2337 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2338 if (isVarArg) {
2339 while ((ArgOffset % 16) != 0) {
2340 ArgOffset += PtrByteSize;
2341 if (GPR_idx != Num_GPR_Regs)
2342 GPR_idx++;
2343 }
2344 ArgOffset += 16;
2345 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2346 }
2347 ++VR_idx;
2348 } else {
2349 // Vectors are aligned.
2350 ArgOffset = ((ArgOffset+15)/16)*16;
2351 CurArgOffset = ArgOffset;
2352 ArgOffset += 16;
2353 needsLoad = true;
2354 }
2355 break;
2356 }
2357
2358 // We need to load the argument to a virtual register if we determined
2359 // above that we ran out of physical registers of the appropriate type.
2360 if (needsLoad) {
2361 int FI = MFI->CreateFixedObject(ObjSize,
2362 CurArgOffset + (ArgSize - ObjSize),
2363 isImmutable);
2364 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2365 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2366 false, false, false, 0);
2367 }
2368
2369 InVals.push_back(ArgVal);
2370 }
2371
2372 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt726c2372012-10-23 15:51:16 +00002373 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002374 // taking the difference between two stack areas will result in an aligned
2375 // stack.
Bill Schmidt726c2372012-10-23 15:51:16 +00002376 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002377
2378 // If the function takes variable number of arguments, make a frame index for
2379 // the start of the first vararg value... for expansion of llvm.va_start.
2380 if (isVarArg) {
2381 int Depth = ArgOffset;
2382
2383 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt726c2372012-10-23 15:51:16 +00002384 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002385 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2386
2387 // If this function is vararg, store any remaining integer argument regs
2388 // to their spots on the stack so that they may be loaded by deferencing the
2389 // result of va_next.
2390 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2391 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2392 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2393 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2394 MachinePointerInfo(), false, false, 0);
2395 MemOps.push_back(Store);
2396 // Increment the address by four for the next argument to store
Bill Schmidt726c2372012-10-23 15:51:16 +00002397 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002398 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2399 }
2400 }
2401
2402 if (!MemOps.empty())
2403 Chain = DAG.getNode(ISD::TokenFactor, dl,
2404 MVT::Other, &MemOps[0], MemOps.size());
2405
2406 return Chain;
2407}
2408
2409SDValue
2410PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002411 SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002412 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002413 const SmallVectorImpl<ISD::InputArg>
2414 &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002415 SDLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002416 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002417 // TODO: add description of PPC stack frame format, or at least some docs.
2418 //
2419 MachineFunction &MF = DAG.getMachineFunction();
2420 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +00002421 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00002422
Owen Andersone50ed302009-08-10 22:56:29 +00002423 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00002424 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002425 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002426 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2427 (CallConv == CallingConv::Fast));
Jim Laskeye9bd7b22006-11-28 14:53:52 +00002428 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey2f616bf2006-11-16 22:43:37 +00002429
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002430 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002431 // Area that is at least reserved in caller of this function.
2432 unsigned MinReservedArea = ArgOffset;
2433
Craig Topperb78ca422012-03-11 07:16:55 +00002434 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002435 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2436 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2437 };
Craig Topperb78ca422012-03-11 07:16:55 +00002438 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerc91a4752006-06-26 22:48:35 +00002439 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2440 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2441 };
Scott Michelfdc40a02009-02-17 22:15:04 +00002442
Craig Topperb78ca422012-03-11 07:16:55 +00002443 static const uint16_t *FPR = GetFPR();
Scott Michelfdc40a02009-02-17 22:15:04 +00002444
Craig Topperb78ca422012-03-11 07:16:55 +00002445 static const uint16_t VR[] = {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002446 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2447 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2448 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00002449
Owen Anderson718cb662007-09-07 04:06:50 +00002450 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002451 const unsigned Num_FPR_Regs = 13;
Owen Anderson718cb662007-09-07 04:06:50 +00002452 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey2f616bf2006-11-16 22:43:37 +00002453
2454 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002455
Craig Topperb78ca422012-03-11 07:16:55 +00002456 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelfdc40a02009-02-17 22:15:04 +00002457
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002458 // In 32-bit non-varargs functions, the stack space for vectors is after the
2459 // stack space for non-vectors. We do not use this space unless we have
2460 // too many vectors to fit in registers, something that only occurs in
Scott Michelfdc40a02009-02-17 22:15:04 +00002461 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002462 // that out...for the pathological case, compute VecArgOffset as the
2463 // start of the vector parameter area. Computing VecArgOffset is the
2464 // entire point of the following loop.
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002465 unsigned VecArgOffset = ArgOffset;
2466 if (!isVarArg && !isPPC64) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002467 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002468 ++ArgNo) {
Owen Andersone50ed302009-08-10 22:56:29 +00002469 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002470 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002471
Duncan Sands276dcbd2008-03-21 09:14:45 +00002472 if (Flags.isByVal()) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002473 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer263109d2012-01-20 14:42:32 +00002474 unsigned ObjSize = Flags.getByValSize();
Scott Michelfdc40a02009-02-17 22:15:04 +00002475 unsigned ArgSize =
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002476 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2477 VecArgOffset += ArgSize;
2478 continue;
2479 }
2480
Owen Anderson825b72b2009-08-11 20:47:22 +00002481 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002482 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002483 case MVT::i32:
2484 case MVT::f32:
Bill Schmidt419f3762012-09-19 15:42:13 +00002485 VecArgOffset += 4;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002486 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002487 case MVT::i64: // PPC64
2488 case MVT::f64:
Bill Schmidt419f3762012-09-19 15:42:13 +00002489 // FIXME: We are guaranteed to be !isPPC64 at this point.
2490 // Does MVT::i64 apply?
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002491 VecArgOffset += 8;
2492 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002493 case MVT::v4f32:
2494 case MVT::v4i32:
2495 case MVT::v8i16:
2496 case MVT::v16i8:
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002497 // Nothing to do, we're only looking at Nonvector args here.
2498 break;
2499 }
2500 }
2501 }
2502 // We've found where the vector parameter area in memory is. Skip the
2503 // first 12 parameters; these don't use that memory.
2504 VecArgOffset = ((VecArgOffset+15)/16)*16;
2505 VecArgOffset += 12*16;
2506
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002507 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey2f616bf2006-11-16 22:43:37 +00002508 // entry to a function on PPC, the arguments start after the linkage area,
2509 // although the first ones are often in registers.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002510
Dan Gohman475871a2008-07-27 21:46:04 +00002511 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002512 unsigned nAltivecParamsAtEnd = 0;
Roman Divacky5236ab32012-09-24 20:47:19 +00002513 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidta7f2ce82013-05-08 17:22:33 +00002514 unsigned CurArgIdx = 0;
2515 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002516 SDValue ArgVal;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002517 bool needsLoad = false;
Owen Andersone50ed302009-08-10 22:56:29 +00002518 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002519 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey619965d2006-11-29 13:37:09 +00002520 unsigned ArgSize = ObjSize;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002521 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidta7f2ce82013-05-08 17:22:33 +00002522 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2523 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002524
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002525 unsigned CurArgOffset = ArgOffset;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002526
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002527 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson825b72b2009-08-11 20:47:22 +00002528 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2529 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002530 if (isVarArg || isPPC64) {
2531 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002532 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohman095cc292008-09-13 01:54:27 +00002533 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002534 PtrByteSize);
2535 } else nAltivecParamsAtEnd++;
2536 } else
2537 // Calculate min reserved area.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002538 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohman095cc292008-09-13 01:54:27 +00002539 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002540 PtrByteSize);
2541
Dale Johannesen8419dd62008-03-07 20:27:40 +00002542 // FIXME the codegen can be much improved in some cases.
2543 // We do not have to keep everything in memory.
Duncan Sands276dcbd2008-03-21 09:14:45 +00002544 if (Flags.isByVal()) {
Dale Johannesen8419dd62008-03-07 20:27:40 +00002545 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sands276dcbd2008-03-21 09:14:45 +00002546 ObjSize = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00002547 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002548 // Objects of size 1 and 2 are right justified, everything else is
2549 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen7f96f392008-03-08 01:41:42 +00002550 if (ObjSize==1 || ObjSize==2) {
2551 CurArgOffset = CurArgOffset + (4 - ObjSize);
2552 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002553 // The value of the object is its address.
Evan Chenged2ae132010-07-03 00:40:23 +00002554 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman475871a2008-07-27 21:46:04 +00002555 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002556 InVals.push_back(FIN);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002557 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen7f96f392008-03-08 01:41:42 +00002558 if (GPR_idx != Num_GPR_Regs) {
Roman Divacky951cd022011-06-17 15:21:10 +00002559 unsigned VReg;
2560 if (isPPC64)
2561 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2562 else
2563 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002564 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt726c2372012-10-23 15:51:16 +00002565 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelfdc40a02009-02-17 22:15:04 +00002566 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Roman Divacky5236ab32012-09-24 20:47:19 +00002567 MachinePointerInfo(FuncArg,
2568 CurArgOffset),
Bill Schmidt419f3762012-09-19 15:42:13 +00002569 ObjType, false, false, 0);
Dale Johannesen7f96f392008-03-08 01:41:42 +00002570 MemOps.push_back(Store);
2571 ++GPR_idx;
Dale Johannesen7f96f392008-03-08 01:41:42 +00002572 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002573
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002574 ArgOffset += PtrByteSize;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002575
Dale Johannesen7f96f392008-03-08 01:41:42 +00002576 continue;
2577 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002578 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2579 // Store whatever pieces of the object are in registers
Bill Schmidt419f3762012-09-19 15:42:13 +00002580 // to memory. ArgOffset will be the address of the beginning
2581 // of the object.
Dale Johannesen8419dd62008-03-07 20:27:40 +00002582 if (GPR_idx != Num_GPR_Regs) {
Roman Divacky951cd022011-06-17 15:21:10 +00002583 unsigned VReg;
2584 if (isPPC64)
2585 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2586 else
2587 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Chenged2ae132010-07-03 00:40:23 +00002588 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman475871a2008-07-27 21:46:04 +00002589 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002590 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002591 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Roman Divacky5236ab32012-09-24 20:47:19 +00002592 MachinePointerInfo(FuncArg, ArgOffset),
David Greene534502d12010-02-15 16:56:53 +00002593 false, false, 0);
Dale Johannesen8419dd62008-03-07 20:27:40 +00002594 MemOps.push_back(Store);
2595 ++GPR_idx;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002596 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002597 } else {
2598 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2599 break;
2600 }
2601 }
2602 continue;
2603 }
2604
Owen Anderson825b72b2009-08-11 20:47:22 +00002605 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002606 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002607 case MVT::i32:
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002608 if (!isPPC64) {
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002609 if (GPR_idx != Num_GPR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002610 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002611 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002612 ++GPR_idx;
2613 } else {
2614 needsLoad = true;
2615 ArgSize = PtrByteSize;
2616 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002617 // All int arguments reserve stack space in the Darwin ABI.
2618 ArgOffset += PtrByteSize;
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002619 break;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002620 }
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002621 // FALLTHROUGH
Owen Anderson825b72b2009-08-11 20:47:22 +00002622 case MVT::i64: // PPC64
Chris Lattnerc91a4752006-06-26 22:48:35 +00002623 if (GPR_idx != Num_GPR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002624 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002625 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002626
Bill Schmidt726c2372012-10-23 15:51:16 +00002627 if (ObjectVT == MVT::i32)
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002628 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson825b72b2009-08-11 20:47:22 +00002629 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt726c2372012-10-23 15:51:16 +00002630 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002631
Chris Lattnerc91a4752006-06-26 22:48:35 +00002632 ++GPR_idx;
2633 } else {
2634 needsLoad = true;
Evan Cheng982a0592008-07-24 08:17:07 +00002635 ArgSize = PtrByteSize;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002636 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002637 // All int arguments reserve stack space in the Darwin ABI.
2638 ArgOffset += 8;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002639 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002640
Owen Anderson825b72b2009-08-11 20:47:22 +00002641 case MVT::f32:
2642 case MVT::f64:
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002643 // Every 4 bytes of argument space consumes one of the GPRs available for
2644 // argument passing.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002645 if (GPR_idx != Num_GPR_Regs) {
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002646 ++GPR_idx;
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002647 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002648 ++GPR_idx;
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002649 }
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002650 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002651 unsigned VReg;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002652
Owen Anderson825b72b2009-08-11 20:47:22 +00002653 if (ObjectVT == MVT::f32)
Devang Patel68e6bee2011-02-21 23:21:26 +00002654 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002655 else
Devang Patel68e6bee2011-02-21 23:21:26 +00002656 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002657
Dan Gohman98ca4f22009-08-05 01:29:28 +00002658 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002659 ++FPR_idx;
2660 } else {
2661 needsLoad = true;
2662 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002663
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002664 // All FP arguments reserve stack space in the Darwin ABI.
2665 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002666 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002667 case MVT::v4f32:
2668 case MVT::v4i32:
2669 case MVT::v8i16:
2670 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00002671 // Note that vector arguments in registers don't reserve stack space,
2672 // except in varargs functions.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002673 if (VR_idx != Num_VR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002674 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002675 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesen75092de2008-03-12 00:22:17 +00002676 if (isVarArg) {
2677 while ((ArgOffset % 16) != 0) {
2678 ArgOffset += PtrByteSize;
2679 if (GPR_idx != Num_GPR_Regs)
2680 GPR_idx++;
2681 }
2682 ArgOffset += 16;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002683 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesen75092de2008-03-12 00:22:17 +00002684 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002685 ++VR_idx;
2686 } else {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002687 if (!isVarArg && !isPPC64) {
2688 // Vectors go after all the nonvectors.
2689 CurArgOffset = VecArgOffset;
2690 VecArgOffset += 16;
2691 } else {
2692 // Vectors are aligned.
2693 ArgOffset = ((ArgOffset+15)/16)*16;
2694 CurArgOffset = ArgOffset;
2695 ArgOffset += 16;
Dale Johannesen404d9902008-03-12 00:49:20 +00002696 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002697 needsLoad = true;
2698 }
2699 break;
2700 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002701
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002702 // We need to load the argument to a virtual register if we determined above
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002703 // that we ran out of physical registers of the appropriate type.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002704 if (needsLoad) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002705 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002706 CurArgOffset + (ArgSize - ObjSize),
Evan Chenged2ae132010-07-03 00:40:23 +00002707 isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00002708 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002709 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002710 false, false, false, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002711 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002712
Dan Gohman98ca4f22009-08-05 01:29:28 +00002713 InVals.push_back(ArgVal);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002714 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002715
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002716 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt726c2372012-10-23 15:51:16 +00002717 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002718 // taking the difference between two stack areas will result in an aligned
2719 // stack.
Bill Schmidt726c2372012-10-23 15:51:16 +00002720 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002721
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002722 // If the function takes variable number of arguments, make a frame index for
2723 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002724 if (isVarArg) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002725 int Depth = ArgOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00002726
Dan Gohman1e93df62010-04-17 14:41:14 +00002727 FuncInfo->setVarArgsFrameIndex(
2728 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00002729 Depth, true));
Dan Gohman1e93df62010-04-17 14:41:14 +00002730 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002731
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002732 // If this function is vararg, store any remaining integer argument regs
2733 // to their spots on the stack so that they may be loaded by deferencing the
2734 // result of va_next.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002735 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002736 unsigned VReg;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002737
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002738 if (isPPC64)
Devang Patel68e6bee2011-02-21 23:21:26 +00002739 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002740 else
Devang Patel68e6bee2011-02-21 23:21:26 +00002741 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002742
Dan Gohman98ca4f22009-08-05 01:29:28 +00002743 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002744 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2745 MachinePointerInfo(), false, false, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002746 MemOps.push_back(Store);
2747 // Increment the address by four for the next argument to store
Dan Gohman475871a2008-07-27 21:46:04 +00002748 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002749 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002750 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002751 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002752
Dale Johannesen8419dd62008-03-07 20:27:40 +00002753 if (!MemOps.empty())
Dan Gohman98ca4f22009-08-05 01:29:28 +00002754 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002755 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesen8419dd62008-03-07 20:27:40 +00002756
Dan Gohman98ca4f22009-08-05 01:29:28 +00002757 return Chain;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002758}
2759
Bill Schmidt419f3762012-09-19 15:42:13 +00002760/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
2761/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002762static unsigned
2763CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2764 bool isPPC64,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002765 bool isVarArg,
2766 unsigned CC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002767 const SmallVectorImpl<ISD::OutputArg>
2768 &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002769 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002770 unsigned &nAltivecParamsAtEnd) {
2771 // Count how many bytes are to be pushed on the stack, including the linkage
2772 // area, and parameter passing area. We start with 24/48 bytes, which is
2773 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002774 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002775 unsigned NumOps = Outs.size();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002776 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2777
2778 // Add up all the space actually used.
2779 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2780 // they all go in registers, but we must reserve stack space for them for
2781 // possible use by the caller. In varargs or 64-bit calls, parameters are
2782 // assigned stack space in order, with padding so Altivec parameters are
2783 // 16-byte aligned.
2784 nAltivecParamsAtEnd = 0;
2785 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002786 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanc9403652010-07-07 15:54:55 +00002787 EVT ArgVT = Outs[i].VT;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002788 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson825b72b2009-08-11 20:47:22 +00002789 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2790 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002791 if (!isVarArg && !isPPC64) {
2792 // Non-varargs Altivec parameters go after all the non-Altivec
2793 // parameters; handle those later so we know how much padding we need.
2794 nAltivecParamsAtEnd++;
2795 continue;
2796 }
2797 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2798 NumBytes = ((NumBytes+15)/16)*16;
2799 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002800 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002801 }
2802
2803 // Allow for Altivec parameters at the end, if needed.
2804 if (nAltivecParamsAtEnd) {
2805 NumBytes = ((NumBytes+15)/16)*16;
2806 NumBytes += 16*nAltivecParamsAtEnd;
2807 }
2808
2809 // The prolog code of the callee may store up to 8 GPR argument registers to
2810 // the stack, allowing va_start to index over them in memory if its varargs.
2811 // Because we cannot tell if this is needed on the caller side, we have to
2812 // conservatively assume that it is needed. As such, make sure we have at
2813 // least enough stack space for the caller to store the 8 GPRs.
2814 NumBytes = std::max(NumBytes,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002815 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002816
2817 // Tail call needs the stack to be aligned.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002818 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
2819 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
2820 getFrameLowering()->getStackAlignment();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002821 unsigned AlignMask = TargetAlign-1;
2822 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2823 }
2824
2825 return NumBytes;
2826}
2827
2828/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002829/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesenb60d5192009-11-24 01:09:07 +00002830static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002831 unsigned ParamSize) {
2832
Dale Johannesenb60d5192009-11-24 01:09:07 +00002833 if (!isTailCall) return 0;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002834
2835 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
2836 unsigned CallerMinReservedArea = FI->getMinReservedArea();
2837 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
2838 // Remember only if the new adjustement is bigger.
2839 if (SPDiff < FI->getTailCallSPDelta())
2840 FI->setTailCallSPDelta(SPDiff);
2841
2842 return SPDiff;
2843}
2844
Dan Gohman98ca4f22009-08-05 01:29:28 +00002845/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2846/// for tail call optimization. Targets which want to do tail call
2847/// optimization should implement this function.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002848bool
Dan Gohman98ca4f22009-08-05 01:29:28 +00002849PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002850 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002851 bool isVarArg,
2852 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002853 SelectionDAG& DAG) const {
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002854 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng6c2e8a92010-01-29 23:05:56 +00002855 return false;
2856
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002857 // Variable argument functions are not supported.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002858 if (isVarArg)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002859 return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002860
Dan Gohman98ca4f22009-08-05 01:29:28 +00002861 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002862 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002863 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
2864 // Functions containing by val parameters are not supported.
2865 for (unsigned i = 0; i != Ins.size(); i++) {
2866 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2867 if (Flags.isByVal()) return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002868 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002869
2870 // Non PIC/GOT tail calls are supported.
2871 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
2872 return true;
2873
2874 // At the moment we can only do local tail calls (in same module, hidden
2875 // or protected) if we are generating PIC.
2876 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2877 return G->getGlobal()->hasHiddenVisibility()
2878 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002879 }
2880
2881 return false;
2882}
2883
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002884/// isCallCompatibleAddress - Return the immediate to use if the specified
2885/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00002886static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002887 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2888 if (!C) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002889
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002890 int Addr = C->getZExtValue();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002891 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith1144af32012-08-24 23:29:28 +00002892 SignExtend32<26>(Addr) != Addr)
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002893 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002894
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002895 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greifba36cb52008-08-28 21:40:38 +00002896 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002897}
2898
Dan Gohman844731a2008-05-13 00:00:25 +00002899namespace {
2900
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002901struct TailCallArgumentInfo {
Dan Gohman475871a2008-07-27 21:46:04 +00002902 SDValue Arg;
2903 SDValue FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002904 int FrameIdx;
2905
2906 TailCallArgumentInfo() : FrameIdx(0) {}
2907};
2908
Dan Gohman844731a2008-05-13 00:00:25 +00002909}
2910
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002911/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
2912static void
2913StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Chengff89dcb2009-10-18 18:16:27 +00002914 SDValue Chain,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002915 const SmallVector<TailCallArgumentInfo, 8> &TailCallArgs,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002916 SmallVector<SDValue, 8> &MemOpChains,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002917 SDLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002918 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002919 SDValue Arg = TailCallArgs[i].Arg;
2920 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002921 int FI = TailCallArgs[i].FrameIdx;
2922 // Store relative to framepointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002923 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002924 MachinePointerInfo::getFixedStack(FI),
2925 false, false, 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002926 }
2927}
2928
2929/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
2930/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman475871a2008-07-27 21:46:04 +00002931static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002932 MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002933 SDValue Chain,
2934 SDValue OldRetAddr,
2935 SDValue OldFP,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002936 int SPDiff,
2937 bool isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002938 bool isDarwinABI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002939 SDLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002940 if (SPDiff) {
2941 // Calculate the new stack slot for the return address.
2942 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002943 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002944 isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002945 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002946 NewRetAddrLoc, true);
Owen Anderson825b72b2009-08-11 20:47:22 +00002947 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002948 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002949 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002950 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene534502d12010-02-15 16:56:53 +00002951 false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002952
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002953 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
2954 // slot as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002955 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002956 int NewFPLoc =
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002957 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene3f2bf852009-11-12 20:49:22 +00002958 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Chenged2ae132010-07-03 00:40:23 +00002959 true);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002960 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
2961 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002962 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene534502d12010-02-15 16:56:53 +00002963 false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002964 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002965 }
2966 return Chain;
2967}
2968
2969/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
2970/// the position of the argument.
2971static void
2972CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman475871a2008-07-27 21:46:04 +00002973 SDValue Arg, int SPDiff, unsigned ArgOffset,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002974 SmallVector<TailCallArgumentInfo, 8>& TailCallArguments) {
2975 int Offset = ArgOffset + SPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002976 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002977 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson825b72b2009-08-11 20:47:22 +00002978 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002979 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002980 TailCallArgumentInfo Info;
2981 Info.Arg = Arg;
2982 Info.FrameIdxOp = FIN;
2983 Info.FrameIdx = FI;
2984 TailCallArguments.push_back(Info);
2985}
2986
2987/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
2988/// stack slot. Returns the chain as result and the loaded frame pointers in
2989/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman475871a2008-07-27 21:46:04 +00002990SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002991 int SPDiff,
2992 SDValue Chain,
2993 SDValue &LROpOut,
2994 SDValue &FPOpOut,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002995 bool isDarwinABI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002996 SDLoc dl) const {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002997 if (SPDiff) {
2998 // Load the LR and FP stack slot for later adjusting.
Owen Anderson825b72b2009-08-11 20:47:22 +00002999 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003000 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003001 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003002 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003003 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003004
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003005 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3006 // slot as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003007 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00003008 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003009 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003010 false, false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003011 Chain = SDValue(FPOpOut.getNode(), 1);
3012 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003013 }
3014 return Chain;
3015}
3016
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003017/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelfdc40a02009-02-17 22:15:04 +00003018/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003019/// specified by the specific parameter attribute. The copy will be passed as
3020/// a byval function parameter.
3021/// Sometimes what we are copying is the end of a larger object, the part that
3022/// does not fit in registers.
Scott Michelfdc40a02009-02-17 22:15:04 +00003023static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003024CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00003025 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003026 SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003027 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen8ad9b432009-02-04 01:17:06 +00003028 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Chris Lattnere72f2022010-09-21 05:40:29 +00003029 false, false, MachinePointerInfo(0),
3030 MachinePointerInfo(0));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003031}
Chris Lattner9f0bc652007-02-25 05:34:32 +00003032
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003033/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3034/// tail calls.
3035static void
Dan Gohman475871a2008-07-27 21:46:04 +00003036LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3037 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003038 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Dan Gohman475871a2008-07-27 21:46:04 +00003039 bool isVector, SmallVector<SDValue, 8> &MemOpChains,
Chris Lattner6229d0a2010-09-21 18:41:36 +00003040 SmallVector<TailCallArgumentInfo, 8> &TailCallArguments,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003041 SDLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00003042 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003043 if (!isTailCall) {
3044 if (isVector) {
Dan Gohman475871a2008-07-27 21:46:04 +00003045 SDValue StackPtr;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003046 if (isPPC64)
Owen Anderson825b72b2009-08-11 20:47:22 +00003047 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003048 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003049 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003050 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003051 DAG.getConstant(ArgOffset, PtrVT));
3052 }
Chris Lattner6229d0a2010-09-21 18:41:36 +00003053 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3054 MachinePointerInfo(), false, false, 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003055 // Calculate and remember argument location.
3056 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3057 TailCallArguments);
3058}
3059
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003060static
3061void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003062 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003063 SDValue LROp, SDValue FPOp, bool isDarwinABI,
3064 SmallVector<TailCallArgumentInfo, 8> &TailCallArguments) {
3065 MachineFunction &MF = DAG.getMachineFunction();
3066
3067 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3068 // might overwrite each other in case of tail call optimization.
3069 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00003070 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003071 InFlag = SDValue();
3072 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3073 MemOpChains2, dl);
3074 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00003075 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003076 &MemOpChains2[0], MemOpChains2.size());
3077
3078 // Store the return address to the appropriate stack slot.
3079 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3080 isPPC64, isDarwinABI, dl);
3081
3082 // Emit callseq_end just before tailcall node.
3083 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trick6e0b2a02013-05-29 22:03:55 +00003084 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003085 InFlag = Chain.getValue(1);
3086}
3087
3088static
3089unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003090 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003091 SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass,
Owen Andersone50ed302009-08-10 22:56:29 +00003092 SmallVector<SDValue, 8> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerb9082582010-11-14 23:42:06 +00003093 const PPCSubtarget &PPCSubTarget) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003094
Chris Lattnerb9082582010-11-14 23:42:06 +00003095 bool isPPC64 = PPCSubTarget.isPPC64();
3096 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3097
Owen Andersone50ed302009-08-10 22:56:29 +00003098 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00003099 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003100 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003101
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003102 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003103
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003104 bool needIndirectCall = true;
3105 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003106 // If this is an absolute destination address, use the munged value.
3107 Callee = SDValue(Dest, 0);
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003108 needIndirectCall = false;
3109 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003110
Chris Lattnerb9082582010-11-14 23:42:06 +00003111 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3112 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3113 // Use indirect calls for ALL functions calls in JIT mode, since the
3114 // far-call stubs may be outside relocation limits for a BL instruction.
3115 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3116 unsigned OpFlags = 0;
3117 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyd5601cc2011-07-24 08:22:56 +00003118 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00003119 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerb9082582010-11-14 23:42:06 +00003120 (G->getGlobal()->isDeclaration() ||
3121 G->getGlobal()->isWeakForLinker())) {
3122 // PC-relative references to external symbols should go through $stub,
3123 // unless we're building with the leopard linker or later, which
3124 // automatically synthesizes these stubs.
3125 OpFlags = PPCII::MO_DARWIN_STUB;
3126 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003127
Chris Lattnerb9082582010-11-14 23:42:06 +00003128 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3129 // every direct call is) turn it into a TargetGlobalAddress /
3130 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003131 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerb9082582010-11-14 23:42:06 +00003132 Callee.getValueType(),
3133 0, OpFlags);
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003134 needIndirectCall = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003135 }
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003136 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003137
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003138 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerb9082582010-11-14 23:42:06 +00003139 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003140
Chris Lattnerb9082582010-11-14 23:42:06 +00003141 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyd5601cc2011-07-24 08:22:56 +00003142 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00003143 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerb9082582010-11-14 23:42:06 +00003144 // PC-relative references to external symbols should go through $stub,
3145 // unless we're building with the leopard linker or later, which
3146 // automatically synthesizes these stubs.
3147 OpFlags = PPCII::MO_DARWIN_STUB;
3148 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003149
Chris Lattnerb9082582010-11-14 23:42:06 +00003150 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3151 OpFlags);
3152 needIndirectCall = false;
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003153 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003154
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003155 if (needIndirectCall) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003156 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3157 // to do the call, we can't use PPCISD::CALL.
3158 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003159
3160 if (isSVR4ABI && isPPC64) {
3161 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3162 // entry point, but to the function descriptor (the function entry point
3163 // address is part of the function descriptor though).
3164 // The function descriptor is a three doubleword structure with the
3165 // following fields: function entry point, TOC base address and
3166 // environment pointer.
3167 // Thus for a call through a function pointer, the following actions need
3168 // to be performed:
3169 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt726c2372012-10-23 15:51:16 +00003170 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003171 // 2. Load the address of the function entry point from the function
3172 // descriptor.
3173 // 3. Load the TOC of the callee from the function descriptor into r2.
3174 // 4. Load the environment pointer from the function descriptor into
3175 // r11.
3176 // 5. Branch to the function entry point address.
3177 // 6. On return of the callee, the TOC of the caller needs to be
3178 // restored (this is done in FinishCall()).
3179 //
3180 // All those operations are flagged together to ensure that no other
3181 // operations can be scheduled in between. E.g. without flagging the
3182 // operations together, a TOC access in the caller could be scheduled
3183 // between the load of the callee TOC and the branch to the callee, which
3184 // results in the TOC access going through the TOC of the callee instead
3185 // of going through the TOC of the caller, which leads to incorrect code.
3186
3187 // Load the address of the function entry point from the function
3188 // descriptor.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003189 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003190 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, MTCTROps,
3191 InFlag.getNode() ? 3 : 2);
3192 Chain = LoadFuncPtr.getValue(1);
3193 InFlag = LoadFuncPtr.getValue(2);
3194
3195 // Load environment pointer into r11.
3196 // Offset of the environment pointer within the function descriptor.
3197 SDValue PtrOff = DAG.getIntPtrConstant(16);
3198
3199 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3200 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3201 InFlag);
3202 Chain = LoadEnvPtr.getValue(1);
3203 InFlag = LoadEnvPtr.getValue(2);
3204
3205 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3206 InFlag);
3207 Chain = EnvVal.getValue(0);
3208 InFlag = EnvVal.getValue(1);
3209
3210 // Load TOC of the callee into r2. We are using a target-specific load
3211 // with r2 hard coded, because the result of a target-independent load
3212 // would never go directly into r2, since r2 is a reserved register (which
3213 // prevents the register allocator from allocating it), resulting in an
3214 // additional register being allocated and an unnecessary move instruction
3215 // being generated.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003216 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003217 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3218 Callee, InFlag);
3219 Chain = LoadTOCPtr.getValue(0);
3220 InFlag = LoadTOCPtr.getValue(1);
3221
3222 MTCTROps[0] = Chain;
3223 MTCTROps[1] = LoadFuncPtr;
3224 MTCTROps[2] = InFlag;
3225 }
3226
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003227 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
3228 2 + (InFlag.getNode() != 0));
3229 InFlag = Chain.getValue(1);
3230
3231 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00003232 NodeTys.push_back(MVT::Other);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003233 NodeTys.push_back(MVT::Glue);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003234 Ops.push_back(Chain);
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003235 CallOpc = PPCISD::BCTRL;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003236 Callee.setNode(0);
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003237 // Add use of X11 (holding environment pointer)
3238 if (isSVR4ABI && isPPC64)
3239 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003240 // Add CTR register as callee so a bctr can be emitted later.
3241 if (isTailCall)
Roman Divacky0c9b5592011-06-03 15:47:49 +00003242 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003243 }
3244
3245 // If this is a direct call, pass the chain and the callee.
3246 if (Callee.getNode()) {
3247 Ops.push_back(Chain);
3248 Ops.push_back(Callee);
3249 }
3250 // If this is a tail call add stack pointer delta.
3251 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00003252 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003253
3254 // Add argument registers to the end of the list so that they are known live
3255 // into the call.
3256 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3257 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3258 RegsToPass[i].second.getValueType()));
3259
3260 return CallOpc;
3261}
3262
Roman Divackyeb8b7dc2012-09-18 16:47:58 +00003263static
3264bool isLocalCall(const SDValue &Callee)
3265{
3266 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky6fc3ea22012-09-18 18:27:49 +00003267 return !G->getGlobal()->isDeclaration() &&
3268 !G->getGlobal()->isWeakForLinker();
Roman Divackyeb8b7dc2012-09-18 16:47:58 +00003269 return false;
3270}
3271
Dan Gohman98ca4f22009-08-05 01:29:28 +00003272SDValue
3273PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003274 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003275 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003276 SDLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00003277 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003278
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003279 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003280 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00003281 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00003282 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003283
3284 // Copy all of the result registers out of their specified physreg.
3285 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3286 CCValAssign &VA = RVLocs[i];
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003287 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand86aef0a2012-11-05 19:39:45 +00003288
3289 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3290 VA.getLocReg(), VA.getLocVT(), InFlag);
3291 Chain = Val.getValue(1);
3292 InFlag = Val.getValue(2);
3293
3294 switch (VA.getLocInfo()) {
3295 default: llvm_unreachable("Unknown loc info!");
3296 case CCValAssign::Full: break;
3297 case CCValAssign::AExt:
3298 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3299 break;
3300 case CCValAssign::ZExt:
3301 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3302 DAG.getValueType(VA.getValVT()));
3303 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3304 break;
3305 case CCValAssign::SExt:
3306 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3307 DAG.getValueType(VA.getValVT()));
3308 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3309 break;
3310 }
3311
3312 InVals.push_back(Val);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003313 }
3314
Dan Gohman98ca4f22009-08-05 01:29:28 +00003315 return Chain;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003316}
3317
Dan Gohman98ca4f22009-08-05 01:29:28 +00003318SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00003319PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003320 bool isTailCall, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003321 SelectionDAG &DAG,
3322 SmallVector<std::pair<unsigned, SDValue>, 8>
3323 &RegsToPass,
3324 SDValue InFlag, SDValue Chain,
3325 SDValue &Callee,
3326 int SPDiff, unsigned NumBytes,
3327 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohmand858e902010-04-17 15:26:15 +00003328 SmallVectorImpl<SDValue> &InVals) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003329 std::vector<EVT> NodeTys;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003330 SmallVector<SDValue, 8> Ops;
3331 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3332 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerb9082582010-11-14 23:42:06 +00003333 PPCSubTarget);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003334
Hal Finkel82b38212012-08-28 02:10:27 +00003335 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3336 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3337 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3338
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003339 // When performing tail call optimization the callee pops its arguments off
3340 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky700ed802013-02-21 20:05:00 +00003341 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003342 int BytesCalleePops =
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003343 (CallConv == CallingConv::Fast &&
3344 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003345
Roman Divackye46137f2012-03-06 16:41:49 +00003346 // Add a register mask operand representing the call-preserved registers.
3347 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3348 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3349 assert(Mask && "Missing call preserved mask for calling convention");
3350 Ops.push_back(DAG.getRegisterMask(Mask));
3351
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003352 if (InFlag.getNode())
3353 Ops.push_back(InFlag);
3354
3355 // Emit tail call.
3356 if (isTailCall) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003357 assert(((Callee.getOpcode() == ISD::Register &&
3358 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3359 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3360 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3361 isa<ConstantSDNode>(Callee)) &&
3362 "Expecting an global address, external symbol, absolute value or register");
3363
Owen Anderson825b72b2009-08-11 20:47:22 +00003364 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003365 }
3366
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003367 // Add a NOP immediately after the branch instruction when using the 64-bit
3368 // SVR4 ABI. At link time, if caller and callee are in a different module and
3369 // thus have a different TOC, the call will be replaced with a call to a stub
3370 // function which saves the current TOC, loads the TOC of the callee and
3371 // branches to the callee. The NOP will be replaced with a load instruction
3372 // which restores the TOC of the caller from the TOC save slot of the current
3373 // stack frame. If caller and callee belong to the same module (and have the
3374 // same TOC), the NOP will remain unchanged.
Hal Finkel5b00cea2012-03-31 14:45:15 +00003375
3376 bool needsTOCRestore = false;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003377 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003378 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003379 // This is a call through a function pointer.
3380 // Restore the caller TOC from the save area into R2.
3381 // See PrepareCall() for more information about calls through function
3382 // pointers in the 64-bit SVR4 ABI.
3383 // We are using a target-specific load with r2 hard coded, because the
3384 // result of a target-independent load would never go directly into r2,
3385 // since r2 is a reserved register (which prevents the register allocator
3386 // from allocating it), resulting in an additional register being
3387 // allocated and an unnecessary move instruction being generated.
Hal Finkel5b00cea2012-03-31 14:45:15 +00003388 needsTOCRestore = true;
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003389 } else if ((CallOpc == PPCISD::CALL) && !isLocalCall(Callee)) {
Roman Divackyeb8b7dc2012-09-18 16:47:58 +00003390 // Otherwise insert NOP for non-local calls.
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003391 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003392 }
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003393 }
3394
Hal Finkel5b00cea2012-03-31 14:45:15 +00003395 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
3396 InFlag = Chain.getValue(1);
3397
3398 if (needsTOCRestore) {
3399 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3400 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3401 InFlag = Chain.getValue(1);
3402 }
3403
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003404 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3405 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trick6e0b2a02013-05-29 22:03:55 +00003406 InFlag, dl);
Dan Gohman98ca4f22009-08-05 01:29:28 +00003407 if (!Ins.empty())
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003408 InFlag = Chain.getValue(1);
3409
Dan Gohman98ca4f22009-08-05 01:29:28 +00003410 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3411 Ins, dl, DAG, InVals);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003412}
3413
Dan Gohman98ca4f22009-08-05 01:29:28 +00003414SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003415PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00003416 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003417 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003418 SDLoc &dl = CLI.DL;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003419 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
3420 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
3421 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
3422 SDValue Chain = CLI.Chain;
3423 SDValue Callee = CLI.Callee;
3424 bool &isTailCall = CLI.IsTailCall;
3425 CallingConv::ID CallConv = CLI.CallConv;
3426 bool isVarArg = CLI.IsVarArg;
3427
Evan Cheng0c439eb2010-01-27 00:07:07 +00003428 if (isTailCall)
3429 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3430 Ins, DAG);
3431
Bill Schmidt726c2372012-10-23 15:51:16 +00003432 if (PPCSubTarget.isSVR4ABI()) {
3433 if (PPCSubTarget.isPPC64())
3434 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3435 isTailCall, Outs, OutVals, Ins,
3436 dl, DAG, InVals);
3437 else
3438 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3439 isTailCall, Outs, OutVals, Ins,
3440 dl, DAG, InVals);
3441 }
Chris Lattnerb9082582010-11-14 23:42:06 +00003442
Bill Schmidt726c2372012-10-23 15:51:16 +00003443 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3444 isTailCall, Outs, OutVals, Ins,
3445 dl, DAG, InVals);
Dan Gohman98ca4f22009-08-05 01:29:28 +00003446}
3447
3448SDValue
Bill Schmidt419f3762012-09-19 15:42:13 +00003449PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3450 CallingConv::ID CallConv, bool isVarArg,
3451 bool isTailCall,
3452 const SmallVectorImpl<ISD::OutputArg> &Outs,
3453 const SmallVectorImpl<SDValue> &OutVals,
3454 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003455 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt419f3762012-09-19 15:42:13 +00003456 SmallVectorImpl<SDValue> &InVals) const {
3457 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003458 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003459
Dan Gohman98ca4f22009-08-05 01:29:28 +00003460 assert((CallConv == CallingConv::C ||
3461 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerffd02002009-07-03 06:45:56 +00003462
Tilmann Schellerffd02002009-07-03 06:45:56 +00003463 unsigned PtrByteSize = 4;
3464
3465 MachineFunction &MF = DAG.getMachineFunction();
3466
3467 // Mark this function as potentially containing a function that contains a
3468 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3469 // and restoring the callers stack pointer in this functions epilog. This is
3470 // done because by tail calling the called function might overwrite the value
3471 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003472 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3473 CallConv == CallingConv::Fast)
Tilmann Schellerffd02002009-07-03 06:45:56 +00003474 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003475
Tilmann Schellerffd02002009-07-03 06:45:56 +00003476 // Count how many bytes are to be pushed on the stack, including the linkage
3477 // area, parameter list area and the part of the local variable space which
3478 // contains copies of aggregates which are passed by value.
3479
3480 // Assign locations to all of the outgoing arguments.
3481 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003482 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00003483 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00003484
3485 // Reserve space for the linkage area on the stack.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003486 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003487
3488 if (isVarArg) {
3489 // Handle fixed and variable vector arguments differently.
3490 // Fixed vector arguments go into registers as long as registers are
3491 // available. Variable vector arguments always go into memory.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003492 unsigned NumArgs = Outs.size();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003493
Tilmann Schellerffd02002009-07-03 06:45:56 +00003494 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00003495 MVT ArgVT = Outs[i].VT;
Dan Gohman98ca4f22009-08-05 01:29:28 +00003496 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerffd02002009-07-03 06:45:56 +00003497 bool Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003498
Dan Gohman98ca4f22009-08-05 01:29:28 +00003499 if (Outs[i].IsFixed) {
Bill Schmidt212af6a2013-02-06 17:33:58 +00003500 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3501 CCInfo);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003502 } else {
Bill Schmidt212af6a2013-02-06 17:33:58 +00003503 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3504 ArgFlags, CCInfo);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003505 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003506
Tilmann Schellerffd02002009-07-03 06:45:56 +00003507 if (Result) {
Torok Edwindac237e2009-07-08 20:53:28 +00003508#ifndef NDEBUG
Chris Lattner45cfe542009-08-23 06:03:38 +00003509 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sands1440e8b2010-11-03 11:35:31 +00003510 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +00003511#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00003512 llvm_unreachable(0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003513 }
3514 }
3515 } else {
3516 // All arguments are treated the same.
Bill Schmidt212af6a2013-02-06 17:33:58 +00003517 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003518 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003519
Tilmann Schellerffd02002009-07-03 06:45:56 +00003520 // Assign locations to all of the outgoing aggregate by value arguments.
3521 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003522 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00003523 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00003524
3525 // Reserve stack space for the allocations in CCInfo.
3526 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3527
Bill Schmidt212af6a2013-02-06 17:33:58 +00003528 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003529
3530 // Size of the linkage area, parameter list area and the part of the local
3531 // space variable where copies of aggregates which are passed by value are
3532 // stored.
3533 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003534
Tilmann Schellerffd02002009-07-03 06:45:56 +00003535 // Calculate by how many bytes the stack has to be adjusted in case of tail
3536 // call optimization.
3537 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3538
3539 // Adjust the stack pointer for the new arguments...
3540 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trick6e0b2a02013-05-29 22:03:55 +00003541 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3542 dl);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003543 SDValue CallSeqStart = Chain;
3544
3545 // Load the return address and frame pointer so it can be moved somewhere else
3546 // later.
3547 SDValue LROp, FPOp;
3548 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3549 dl);
3550
3551 // Set up a copy of the stack pointer for use loading and storing any
3552 // arguments that may not fit in the registers available for argument
3553 // passing.
Owen Anderson825b72b2009-08-11 20:47:22 +00003554 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003555
Tilmann Schellerffd02002009-07-03 06:45:56 +00003556 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3557 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3558 SmallVector<SDValue, 8> MemOpChains;
3559
Roman Divacky0aaa9192011-08-30 17:04:16 +00003560 bool seenFloatArg = false;
Tilmann Schellerffd02002009-07-03 06:45:56 +00003561 // Walk the register/memloc assignments, inserting copies/loads.
3562 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3563 i != e;
3564 ++i) {
3565 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00003566 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00003567 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003568
Tilmann Schellerffd02002009-07-03 06:45:56 +00003569 if (Flags.isByVal()) {
3570 // Argument is an aggregate which is passed by value, thus we need to
3571 // create a copy of it in the local variable space of the current stack
3572 // frame (which is the stack frame of the caller) and pass the address of
3573 // this copy to the callee.
3574 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3575 CCValAssign &ByValVA = ByValArgLocs[j++];
3576 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003577
Tilmann Schellerffd02002009-07-03 06:45:56 +00003578 // Memory reserved in the local variable space of the callers stack frame.
3579 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003580
Tilmann Schellerffd02002009-07-03 06:45:56 +00003581 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3582 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003583
Tilmann Schellerffd02002009-07-03 06:45:56 +00003584 // Create a copy of the argument in the local area of the current
3585 // stack frame.
3586 SDValue MemcpyCall =
3587 CreateCopyOfByValArgument(Arg, PtrOff,
3588 CallSeqStart.getNode()->getOperand(0),
3589 Flags, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003590
Tilmann Schellerffd02002009-07-03 06:45:56 +00003591 // This must go outside the CALLSEQ_START..END.
3592 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00003593 CallSeqStart.getNode()->getOperand(1),
3594 SDLoc(MemcpyCall));
Tilmann Schellerffd02002009-07-03 06:45:56 +00003595 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3596 NewCallSeqStart.getNode());
3597 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003598
Tilmann Schellerffd02002009-07-03 06:45:56 +00003599 // Pass the address of the aggregate copy on the stack either in a
3600 // physical register or in the parameter list area of the current stack
3601 // frame to the callee.
3602 Arg = PtrOff;
3603 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003604
Tilmann Schellerffd02002009-07-03 06:45:56 +00003605 if (VA.isRegLoc()) {
Roman Divacky0aaa9192011-08-30 17:04:16 +00003606 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerffd02002009-07-03 06:45:56 +00003607 // Put argument in a physical register.
3608 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3609 } else {
3610 // Put argument in the parameter list area of the current stack frame.
3611 assert(VA.isMemLoc());
3612 unsigned LocMemOffset = VA.getLocMemOffset();
3613
3614 if (!isTailCall) {
3615 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3616 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3617
3618 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner6229d0a2010-09-21 18:41:36 +00003619 MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00003620 false, false, 0));
Tilmann Schellerffd02002009-07-03 06:45:56 +00003621 } else {
3622 // Calculate and remember argument location.
3623 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3624 TailCallArguments);
3625 }
3626 }
3627 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003628
Tilmann Schellerffd02002009-07-03 06:45:56 +00003629 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00003630 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Schellerffd02002009-07-03 06:45:56 +00003631 &MemOpChains[0], MemOpChains.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003632
Tilmann Schellerffd02002009-07-03 06:45:56 +00003633 // Build a sequence of copy-to-reg nodes chained together with token chain
3634 // and flag operands which copy the outgoing args into the appropriate regs.
3635 SDValue InFlag;
3636 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3637 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3638 RegsToPass[i].second, InFlag);
3639 InFlag = Chain.getValue(1);
3640 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003641
Hal Finkel82b38212012-08-28 02:10:27 +00003642 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3643 // registers.
3644 if (isVarArg) {
NAKAMURA Takumid2a35f22012-08-30 15:52:29 +00003645 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3646 SDValue Ops[] = { Chain, InFlag };
3647
Hal Finkel82b38212012-08-28 02:10:27 +00003648 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
NAKAMURA Takumid2a35f22012-08-30 15:52:29 +00003649 dl, VTs, Ops, InFlag.getNode() ? 2 : 1);
3650
Hal Finkel82b38212012-08-28 02:10:27 +00003651 InFlag = Chain.getValue(1);
3652 }
3653
Chris Lattnerb9082582010-11-14 23:42:06 +00003654 if (isTailCall)
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003655 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3656 false, TailCallArguments);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003657
Dan Gohman98ca4f22009-08-05 01:29:28 +00003658 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3659 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3660 Ins, InVals);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003661}
3662
Bill Schmidt726c2372012-10-23 15:51:16 +00003663// Copy an argument into memory, being careful to do this outside the
3664// call sequence for the call to which the argument belongs.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003665SDValue
Bill Schmidt726c2372012-10-23 15:51:16 +00003666PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3667 SDValue CallSeqStart,
3668 ISD::ArgFlagsTy Flags,
3669 SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003670 SDLoc dl) const {
Bill Schmidt726c2372012-10-23 15:51:16 +00003671 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3672 CallSeqStart.getNode()->getOperand(0),
3673 Flags, DAG, dl);
3674 // The MEMCPY must go outside the CALLSEQ_START..END.
3675 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00003676 CallSeqStart.getNode()->getOperand(1),
3677 SDLoc(MemcpyCall));
Bill Schmidt726c2372012-10-23 15:51:16 +00003678 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3679 NewCallSeqStart.getNode());
3680 return NewCallSeqStart;
3681}
3682
3683SDValue
3684PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003685 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003686 bool isTailCall,
3687 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00003688 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003689 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003690 SDLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00003691 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003692
Bill Schmidt726c2372012-10-23 15:51:16 +00003693 unsigned NumOps = Outs.size();
Bill Schmidt419f3762012-09-19 15:42:13 +00003694
Bill Schmidt726c2372012-10-23 15:51:16 +00003695 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3696 unsigned PtrByteSize = 8;
3697
3698 MachineFunction &MF = DAG.getMachineFunction();
3699
3700 // Mark this function as potentially containing a function that contains a
3701 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3702 // and restoring the callers stack pointer in this functions epilog. This is
3703 // done because by tail calling the called function might overwrite the value
3704 // in this function's (MF) stack pointer stack slot 0(SP).
3705 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3706 CallConv == CallingConv::Fast)
3707 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3708
3709 unsigned nAltivecParamsAtEnd = 0;
3710
3711 // Count how many bytes are to be pushed on the stack, including the linkage
3712 // area, and parameter passing area. We start with at least 48 bytes, which
3713 // is reserved space for [SP][CR][LR][3 x unused].
3714 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
3715 // of this call.
3716 unsigned NumBytes =
3717 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
3718 Outs, OutVals, nAltivecParamsAtEnd);
3719
3720 // Calculate by how many bytes the stack has to be adjusted in case of tail
3721 // call optimization.
3722 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3723
3724 // To protect arguments on the stack from being clobbered in a tail call,
3725 // force all the loads to happen before doing any other lowering.
3726 if (isTailCall)
3727 Chain = DAG.getStackArgumentTokenFactor(Chain);
3728
3729 // Adjust the stack pointer for the new arguments...
3730 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trick6e0b2a02013-05-29 22:03:55 +00003731 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3732 dl);
Bill Schmidt726c2372012-10-23 15:51:16 +00003733 SDValue CallSeqStart = Chain;
3734
3735 // Load the return address and frame pointer so it can be move somewhere else
3736 // later.
3737 SDValue LROp, FPOp;
3738 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
3739 dl);
3740
3741 // Set up a copy of the stack pointer for use loading and storing any
3742 // arguments that may not fit in the registers available for argument
3743 // passing.
3744 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
3745
3746 // Figure out which arguments are going to go in registers, and which in
3747 // memory. Also, if this is a vararg function, floating point operations
3748 // must be stored to our stack, and loaded into integer regs as well, if
3749 // any integer regs are available for argument passing.
3750 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
3751 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
3752
3753 static const uint16_t GPR[] = {
3754 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3755 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3756 };
3757 static const uint16_t *FPR = GetFPR();
3758
3759 static const uint16_t VR[] = {
3760 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3761 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3762 };
3763 const unsigned NumGPRs = array_lengthof(GPR);
3764 const unsigned NumFPRs = 13;
3765 const unsigned NumVRs = array_lengthof(VR);
3766
3767 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3768 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3769
3770 SmallVector<SDValue, 8> MemOpChains;
3771 for (unsigned i = 0; i != NumOps; ++i) {
3772 SDValue Arg = OutVals[i];
3773 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3774
3775 // PtrOff will be used to store the current argument to the stack if a
3776 // register cannot be found for it.
3777 SDValue PtrOff;
3778
3779 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
3780
3781 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
3782
3783 // Promote integers to 64-bit values.
3784 if (Arg.getValueType() == MVT::i32) {
3785 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
3786 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3787 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
3788 }
3789
3790 // FIXME memcpy is used way more than necessary. Correctness first.
3791 // Note: "by value" is code for passing a structure by value, not
3792 // basic types.
3793 if (Flags.isByVal()) {
3794 // Note: Size includes alignment padding, so
3795 // struct x { short a; char b; }
3796 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
3797 // These are the proper values we need for right-justifying the
3798 // aggregate in a parameter register.
3799 unsigned Size = Flags.getByValSize();
Bill Schmidt42d43352012-10-31 01:15:05 +00003800
3801 // An empty aggregate parameter takes up no storage and no
3802 // registers.
3803 if (Size == 0)
3804 continue;
3805
Bill Schmidt726c2372012-10-23 15:51:16 +00003806 // All aggregates smaller than 8 bytes must be passed right-justified.
3807 if (Size==1 || Size==2 || Size==4) {
3808 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
3809 if (GPR_idx != NumGPRs) {
3810 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
3811 MachinePointerInfo(), VT,
3812 false, false, 0);
3813 MemOpChains.push_back(Load.getValue(1));
3814 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3815
3816 ArgOffset += PtrByteSize;
3817 continue;
3818 }
3819 }
3820
3821 if (GPR_idx == NumGPRs && Size < 8) {
3822 SDValue Const = DAG.getConstant(PtrByteSize - Size,
3823 PtrOff.getValueType());
3824 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3825 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3826 CallSeqStart,
3827 Flags, DAG, dl);
3828 ArgOffset += PtrByteSize;
3829 continue;
3830 }
3831 // Copy entire object into memory. There are cases where gcc-generated
3832 // code assumes it is there, even if it could be put entirely into
3833 // registers. (This is not what the doc says.)
3834
3835 // FIXME: The above statement is likely due to a misunderstanding of the
3836 // documents. All arguments must be copied into the parameter area BY
3837 // THE CALLEE in the event that the callee takes the address of any
3838 // formal argument. That has not yet been implemented. However, it is
3839 // reasonable to use the stack area as a staging area for the register
3840 // load.
3841
3842 // Skip this for small aggregates, as we will use the same slot for a
3843 // right-justified copy, below.
3844 if (Size >= 8)
3845 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
3846 CallSeqStart,
3847 Flags, DAG, dl);
3848
3849 // When a register is available, pass a small aggregate right-justified.
3850 if (Size < 8 && GPR_idx != NumGPRs) {
3851 // The easiest way to get this right-justified in a register
3852 // is to copy the structure into the rightmost portion of a
3853 // local variable slot, then load the whole slot into the
3854 // register.
3855 // FIXME: The memcpy seems to produce pretty awful code for
3856 // small aggregates, particularly for packed ones.
Matt Arsenault225ed702013-05-18 00:21:46 +00003857 // FIXME: It would be preferable to use the slot in the
Bill Schmidt726c2372012-10-23 15:51:16 +00003858 // parameter save area instead of a new local variable.
3859 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
3860 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3861 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3862 CallSeqStart,
3863 Flags, DAG, dl);
3864
3865 // Load the slot into the register.
3866 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
3867 MachinePointerInfo(),
3868 false, false, false, 0);
3869 MemOpChains.push_back(Load.getValue(1));
3870 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3871
3872 // Done with this argument.
3873 ArgOffset += PtrByteSize;
3874 continue;
3875 }
3876
3877 // For aggregates larger than PtrByteSize, copy the pieces of the
3878 // object that fit into registers from the parameter save area.
3879 for (unsigned j=0; j<Size; j+=PtrByteSize) {
3880 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
3881 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
3882 if (GPR_idx != NumGPRs) {
3883 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
3884 MachinePointerInfo(),
3885 false, false, false, 0);
3886 MemOpChains.push_back(Load.getValue(1));
3887 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3888 ArgOffset += PtrByteSize;
3889 } else {
3890 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
3891 break;
3892 }
3893 }
3894 continue;
3895 }
3896
3897 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
3898 default: llvm_unreachable("Unexpected ValueType for argument!");
3899 case MVT::i32:
3900 case MVT::i64:
3901 if (GPR_idx != NumGPRs) {
3902 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
3903 } else {
3904 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3905 true, isTailCall, false, MemOpChains,
3906 TailCallArguments, dl);
3907 }
3908 ArgOffset += PtrByteSize;
3909 break;
3910 case MVT::f32:
3911 case MVT::f64:
3912 if (FPR_idx != NumFPRs) {
3913 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
3914
3915 if (isVarArg) {
Bill Schmidte6c56432012-10-29 21:18:16 +00003916 // A single float or an aggregate containing only a single float
3917 // must be passed right-justified in the stack doubleword, and
3918 // in the GPR, if one is available.
3919 SDValue StoreOff;
3920 if (Arg.getValueType().getSimpleVT().SimpleTy == MVT::f32) {
3921 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
3922 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
3923 } else
3924 StoreOff = PtrOff;
3925
3926 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt726c2372012-10-23 15:51:16 +00003927 MachinePointerInfo(), false, false, 0);
3928 MemOpChains.push_back(Store);
3929
3930 // Float varargs are always shadowed in available integer registers
3931 if (GPR_idx != NumGPRs) {
3932 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
3933 MachinePointerInfo(), false, false,
3934 false, 0);
3935 MemOpChains.push_back(Load.getValue(1));
3936 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3937 }
3938 } else if (GPR_idx != NumGPRs)
3939 // If we have any FPRs remaining, we may also have GPRs remaining.
3940 ++GPR_idx;
3941 } else {
3942 // Single-precision floating-point values are mapped to the
3943 // second (rightmost) word of the stack doubleword.
3944 if (Arg.getValueType() == MVT::f32) {
3945 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
3946 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
3947 }
3948
3949 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3950 true, isTailCall, false, MemOpChains,
3951 TailCallArguments, dl);
3952 }
3953 ArgOffset += 8;
3954 break;
3955 case MVT::v4f32:
3956 case MVT::v4i32:
3957 case MVT::v8i16:
3958 case MVT::v16i8:
3959 if (isVarArg) {
3960 // These go aligned on the stack, or in the corresponding R registers
3961 // when within range. The Darwin PPC ABI doc claims they also go in
3962 // V registers; in fact gcc does this only for arguments that are
3963 // prototyped, not for those that match the ... We do it for all
3964 // arguments, seems to work.
3965 while (ArgOffset % 16 !=0) {
3966 ArgOffset += PtrByteSize;
3967 if (GPR_idx != NumGPRs)
3968 GPR_idx++;
3969 }
3970 // We could elide this store in the case where the object fits
3971 // entirely in R registers. Maybe later.
3972 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
3973 DAG.getConstant(ArgOffset, PtrVT));
3974 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
3975 MachinePointerInfo(), false, false, 0);
3976 MemOpChains.push_back(Store);
3977 if (VR_idx != NumVRs) {
3978 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
3979 MachinePointerInfo(),
3980 false, false, false, 0);
3981 MemOpChains.push_back(Load.getValue(1));
3982 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
3983 }
3984 ArgOffset += 16;
3985 for (unsigned i=0; i<16; i+=PtrByteSize) {
3986 if (GPR_idx == NumGPRs)
3987 break;
3988 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
3989 DAG.getConstant(i, PtrVT));
3990 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
3991 false, false, false, 0);
3992 MemOpChains.push_back(Load.getValue(1));
3993 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3994 }
3995 break;
3996 }
3997
3998 // Non-varargs Altivec params generally go in registers, but have
3999 // stack space allocated at the end.
4000 if (VR_idx != NumVRs) {
4001 // Doesn't have GPR space allocated.
4002 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4003 } else {
4004 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4005 true, isTailCall, true, MemOpChains,
4006 TailCallArguments, dl);
4007 ArgOffset += 16;
4008 }
4009 break;
4010 }
4011 }
4012
4013 if (!MemOpChains.empty())
4014 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4015 &MemOpChains[0], MemOpChains.size());
4016
4017 // Check if this is an indirect call (MTCTR/BCTRL).
4018 // See PrepareCall() for more information about calls through function
4019 // pointers in the 64-bit SVR4 ABI.
4020 if (!isTailCall &&
4021 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4022 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4023 !isBLACompatibleAddress(Callee, DAG)) {
4024 // Load r2 into a virtual register and store it to the TOC save area.
4025 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4026 // TOC save area offset.
4027 SDValue PtrOff = DAG.getIntPtrConstant(40);
4028 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4029 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4030 false, false, 0);
4031 // R12 must contain the address of an indirect callee. This does not
4032 // mean the MTCTR instruction must use R12; it's easier to model this
4033 // as an extra parameter, so do that.
4034 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4035 }
4036
4037 // Build a sequence of copy-to-reg nodes chained together with token chain
4038 // and flag operands which copy the outgoing args into the appropriate regs.
4039 SDValue InFlag;
4040 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4041 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4042 RegsToPass[i].second, InFlag);
4043 InFlag = Chain.getValue(1);
4044 }
4045
4046 if (isTailCall)
4047 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4048 FPOp, true, TailCallArguments);
4049
4050 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4051 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4052 Ins, InVals);
4053}
4054
4055SDValue
4056PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4057 CallingConv::ID CallConv, bool isVarArg,
4058 bool isTailCall,
4059 const SmallVectorImpl<ISD::OutputArg> &Outs,
4060 const SmallVectorImpl<SDValue> &OutVals,
4061 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004062 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt726c2372012-10-23 15:51:16 +00004063 SmallVectorImpl<SDValue> &InVals) const {
4064
4065 unsigned NumOps = Outs.size();
Scott Michelfdc40a02009-02-17 22:15:04 +00004066
Owen Andersone50ed302009-08-10 22:56:29 +00004067 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00004068 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerc91a4752006-06-26 22:48:35 +00004069 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004070
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004071 MachineFunction &MF = DAG.getMachineFunction();
4072
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004073 // Mark this function as potentially containing a function that contains a
4074 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4075 // and restoring the callers stack pointer in this functions epilog. This is
4076 // done because by tail calling the called function might overwrite the value
4077 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky8a8d4792011-12-02 22:16:29 +00004078 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4079 CallConv == CallingConv::Fast)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004080 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4081
4082 unsigned nAltivecParamsAtEnd = 0;
4083
Chris Lattnerabde4602006-05-16 22:56:08 +00004084 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerc91a4752006-06-26 22:48:35 +00004085 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004086 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004087 unsigned NumBytes =
Dan Gohman98ca4f22009-08-05 01:29:28 +00004088 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanc9403652010-07-07 15:54:55 +00004089 Outs, OutVals,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004090 nAltivecParamsAtEnd);
Dale Johannesen75092de2008-03-12 00:22:17 +00004091
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004092 // Calculate by how many bytes the stack has to be adjusted in case of tail
4093 // call optimization.
4094 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelfdc40a02009-02-17 22:15:04 +00004095
Dan Gohman98ca4f22009-08-05 01:29:28 +00004096 // To protect arguments on the stack from being clobbered in a tail call,
4097 // force all the loads to happen before doing any other lowering.
4098 if (isTailCall)
4099 Chain = DAG.getStackArgumentTokenFactor(Chain);
4100
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004101 // Adjust the stack pointer for the new arguments...
4102 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trick6e0b2a02013-05-29 22:03:55 +00004103 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4104 dl);
Dan Gohman475871a2008-07-27 21:46:04 +00004105 SDValue CallSeqStart = Chain;
Scott Michelfdc40a02009-02-17 22:15:04 +00004106
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004107 // Load the return address and frame pointer so it can be move somewhere else
4108 // later.
Dan Gohman475871a2008-07-27 21:46:04 +00004109 SDValue LROp, FPOp;
Tilmann Schellerffd02002009-07-03 06:45:56 +00004110 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4111 dl);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004112
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004113 // Set up a copy of the stack pointer for use loading and storing any
4114 // arguments that may not fit in the registers available for argument
4115 // passing.
Dan Gohman475871a2008-07-27 21:46:04 +00004116 SDValue StackPtr;
Chris Lattnerc91a4752006-06-26 22:48:35 +00004117 if (isPPC64)
Owen Anderson825b72b2009-08-11 20:47:22 +00004118 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerc91a4752006-06-26 22:48:35 +00004119 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004120 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00004121
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004122 // Figure out which arguments are going to go in registers, and which in
4123 // memory. Also, if this is a vararg function, floating point operations
4124 // must be stored to our stack, and loaded into integer regs as well, if
4125 // any integer regs are available for argument passing.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00004126 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004127 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004128
Craig Topperb78ca422012-03-11 07:16:55 +00004129 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner9a2a4972006-05-17 06:01:33 +00004130 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4131 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4132 };
Craig Topperb78ca422012-03-11 07:16:55 +00004133 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerc91a4752006-06-26 22:48:35 +00004134 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4135 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4136 };
Craig Topperb78ca422012-03-11 07:16:55 +00004137 static const uint16_t *FPR = GetFPR();
Scott Michelfdc40a02009-02-17 22:15:04 +00004138
Craig Topperb78ca422012-03-11 07:16:55 +00004139 static const uint16_t VR[] = {
Chris Lattner9a2a4972006-05-17 06:01:33 +00004140 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4141 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4142 };
Owen Anderson718cb662007-09-07 04:06:50 +00004143 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004144 const unsigned NumFPRs = 13;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00004145 const unsigned NumVRs = array_lengthof(VR);
Scott Michelfdc40a02009-02-17 22:15:04 +00004146
Craig Topperb78ca422012-03-11 07:16:55 +00004147 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerc91a4752006-06-26 22:48:35 +00004148
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004149 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004150 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4151
Dan Gohman475871a2008-07-27 21:46:04 +00004152 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng4360bdc2006-05-25 00:57:32 +00004153 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00004154 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00004155 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00004156
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004157 // PtrOff will be used to store the current argument to the stack if a
4158 // register cannot be found for it.
Dan Gohman475871a2008-07-27 21:46:04 +00004159 SDValue PtrOff;
Scott Michelfdc40a02009-02-17 22:15:04 +00004160
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004161 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00004162
Dale Johannesen39355f92009-02-04 02:34:38 +00004163 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerc91a4752006-06-26 22:48:35 +00004164
4165 // On PPC64, promote integers to 64-bit values.
Owen Anderson825b72b2009-08-11 20:47:22 +00004166 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00004167 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4168 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson825b72b2009-08-11 20:47:22 +00004169 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00004170 }
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004171
Dale Johannesen8419dd62008-03-07 20:27:40 +00004172 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt419f3762012-09-19 15:42:13 +00004173 // Note: "by value" is code for passing a structure by value, not
4174 // basic types.
Duncan Sands276dcbd2008-03-21 09:14:45 +00004175 if (Flags.isByVal()) {
4176 unsigned Size = Flags.getByValSize();
Bill Schmidt726c2372012-10-23 15:51:16 +00004177 // Very small objects are passed right-justified. Everything else is
4178 // passed left-justified.
4179 if (Size==1 || Size==2) {
4180 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesen8419dd62008-03-07 20:27:40 +00004181 if (GPR_idx != NumGPRs) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004182 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004183 MachinePointerInfo(), VT,
4184 false, false, 0);
Dale Johannesen8419dd62008-03-07 20:27:40 +00004185 MemOpChains.push_back(Load.getValue(1));
4186 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004187
4188 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00004189 } else {
Bill Schmidt7a6cb152012-10-16 13:30:53 +00004190 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4191 PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00004192 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt726c2372012-10-23 15:51:16 +00004193 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4194 CallSeqStart,
4195 Flags, DAG, dl);
Dale Johannesen8419dd62008-03-07 20:27:40 +00004196 ArgOffset += PtrByteSize;
4197 }
4198 continue;
4199 }
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00004200 // Copy entire object into memory. There are cases where gcc-generated
4201 // code assumes it is there, even if it could be put entirely into
4202 // registers. (This is not what the doc says.)
Bill Schmidt726c2372012-10-23 15:51:16 +00004203 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4204 CallSeqStart,
4205 Flags, DAG, dl);
Bill Schmidt419f3762012-09-19 15:42:13 +00004206
4207 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4208 // copy the pieces of the object that fit into registers from the
4209 // parameter save area.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004210 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00004211 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00004212 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004213 if (GPR_idx != NumGPRs) {
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004214 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4215 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004216 false, false, false, 0);
Dale Johannesen1f797a32008-03-05 23:31:27 +00004217 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004218 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004219 ArgOffset += PtrByteSize;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004220 } else {
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00004221 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00004222 break;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004223 }
4224 }
4225 continue;
4226 }
4227
Owen Anderson825b72b2009-08-11 20:47:22 +00004228 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004229 default: llvm_unreachable("Unexpected ValueType for argument!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004230 case MVT::i32:
4231 case MVT::i64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00004232 if (GPR_idx != NumGPRs) {
4233 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004234 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004235 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4236 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004237 TailCallArguments, dl);
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004238 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004239 ArgOffset += PtrByteSize;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004240 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004241 case MVT::f32:
4242 case MVT::f64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00004243 if (FPR_idx != NumFPRs) {
4244 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4245
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004246 if (isVarArg) {
Chris Lattner6229d0a2010-09-21 18:41:36 +00004247 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4248 MachinePointerInfo(), false, false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004249 MemOpChains.push_back(Store);
4250
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004251 // Float varargs are always shadowed in available integer registers
Chris Lattner9a2a4972006-05-17 06:01:33 +00004252 if (GPR_idx != NumGPRs) {
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004253 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004254 MachinePointerInfo(), false, false,
4255 false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004256 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004257 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004258 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004259 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman475871a2008-07-27 21:46:04 +00004260 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00004261 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004262 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4263 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004264 false, false, false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004265 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004266 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerabde4602006-05-16 22:56:08 +00004267 }
4268 } else {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004269 // If we have any FPRs remaining, we may also have GPRs remaining.
4270 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4271 // GPRs.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004272 if (GPR_idx != NumGPRs)
4273 ++GPR_idx;
Owen Anderson825b72b2009-08-11 20:47:22 +00004274 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004275 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4276 ++GPR_idx;
Chris Lattnerabde4602006-05-16 22:56:08 +00004277 }
Bill Schmidt726c2372012-10-23 15:51:16 +00004278 } else
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004279 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4280 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004281 TailCallArguments, dl);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004282 if (isPPC64)
4283 ArgOffset += 8;
4284 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004285 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004286 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004287 case MVT::v4f32:
4288 case MVT::v4i32:
4289 case MVT::v8i16:
4290 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00004291 if (isVarArg) {
4292 // These go aligned on the stack, or in the corresponding R registers
Scott Michelfdc40a02009-02-17 22:15:04 +00004293 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesen75092de2008-03-12 00:22:17 +00004294 // V registers; in fact gcc does this only for arguments that are
4295 // prototyped, not for those that match the ... We do it for all
4296 // arguments, seems to work.
4297 while (ArgOffset % 16 !=0) {
4298 ArgOffset += PtrByteSize;
4299 if (GPR_idx != NumGPRs)
4300 GPR_idx++;
4301 }
4302 // We could elide this store in the case where the object fits
4303 // entirely in R registers. Maybe later.
Scott Michelfdc40a02009-02-17 22:15:04 +00004304 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesen75092de2008-03-12 00:22:17 +00004305 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner6229d0a2010-09-21 18:41:36 +00004306 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4307 MachinePointerInfo(), false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00004308 MemOpChains.push_back(Store);
4309 if (VR_idx != NumVRs) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004310 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004311 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004312 false, false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00004313 MemOpChains.push_back(Load.getValue(1));
4314 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4315 }
4316 ArgOffset += 16;
4317 for (unsigned i=0; i<16; i+=PtrByteSize) {
4318 if (GPR_idx == NumGPRs)
4319 break;
Dale Johannesen39355f92009-02-04 02:34:38 +00004320 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesen75092de2008-03-12 00:22:17 +00004321 DAG.getConstant(i, PtrVT));
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004322 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004323 false, false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00004324 MemOpChains.push_back(Load.getValue(1));
4325 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4326 }
4327 break;
4328 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004329
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004330 // Non-varargs Altivec params generally go in registers, but have
4331 // stack space allocated at the end.
4332 if (VR_idx != NumVRs) {
4333 // Doesn't have GPR space allocated.
4334 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4335 } else if (nAltivecParamsAtEnd==0) {
4336 // We are emitting Altivec params in order.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004337 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4338 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004339 TailCallArguments, dl);
Dale Johannesen75092de2008-03-12 00:22:17 +00004340 ArgOffset += 16;
Dale Johannesen75092de2008-03-12 00:22:17 +00004341 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004342 break;
Chris Lattnerabde4602006-05-16 22:56:08 +00004343 }
Chris Lattnerabde4602006-05-16 22:56:08 +00004344 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004345 // If all Altivec parameters fit in registers, as they usually do,
4346 // they get stack space following the non-Altivec parameters. We
4347 // don't track this here because nobody below needs it.
4348 // If there are more Altivec parameters than fit in registers emit
4349 // the stores here.
4350 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4351 unsigned j = 0;
4352 // Offset is aligned; skip 1st 12 params which go in V registers.
4353 ArgOffset = ((ArgOffset+15)/16)*16;
4354 ArgOffset += 12*16;
4355 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00004356 SDValue Arg = OutVals[i];
4357 EVT ArgType = Outs[i].VT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004358 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4359 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004360 if (++j > NumVRs) {
Dan Gohman475871a2008-07-27 21:46:04 +00004361 SDValue PtrOff;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004362 // We are emitting Altivec params in order.
4363 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4364 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004365 TailCallArguments, dl);
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004366 ArgOffset += 16;
4367 }
4368 }
4369 }
4370 }
4371
Chris Lattner9a2a4972006-05-17 06:01:33 +00004372 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00004373 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnere2199452006-08-11 17:38:39 +00004374 &MemOpChains[0], MemOpChains.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00004375
Dale Johannesenf7b73042010-03-09 20:15:42 +00004376 // On Darwin, R12 must contain the address of an indirect callee. This does
4377 // not mean the MTCTR instruction must use R12; it's easier to model this as
4378 // an extra parameter, so do that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004379 if (!isTailCall &&
Dale Johannesenf7b73042010-03-09 20:15:42 +00004380 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4381 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4382 !isBLACompatibleAddress(Callee, DAG))
4383 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4384 PPC::R12), Callee));
4385
Chris Lattner9a2a4972006-05-17 06:01:33 +00004386 // Build a sequence of copy-to-reg nodes chained together with token chain
4387 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00004388 SDValue InFlag;
Chris Lattner9a2a4972006-05-17 06:01:33 +00004389 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004390 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen39355f92009-02-04 02:34:38 +00004391 RegsToPass[i].second, InFlag);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004392 InFlag = Chain.getValue(1);
4393 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004394
Chris Lattnerb9082582010-11-14 23:42:06 +00004395 if (isTailCall)
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004396 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4397 FPOp, true, TailCallArguments);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004398
Dan Gohman98ca4f22009-08-05 01:29:28 +00004399 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4400 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4401 Ins, InVals);
Chris Lattnerabde4602006-05-16 22:56:08 +00004402}
4403
Hal Finkeld712f932011-10-14 19:51:36 +00004404bool
4405PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4406 MachineFunction &MF, bool isVarArg,
4407 const SmallVectorImpl<ISD::OutputArg> &Outs,
4408 LLVMContext &Context) const {
4409 SmallVector<CCValAssign, 16> RVLocs;
4410 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4411 RVLocs, Context);
4412 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4413}
4414
Dan Gohman98ca4f22009-08-05 01:29:28 +00004415SDValue
4416PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00004417 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004418 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00004419 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004420 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00004421
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004422 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00004423 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00004424 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004425 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelfdc40a02009-02-17 22:15:04 +00004426
Dan Gohman475871a2008-07-27 21:46:04 +00004427 SDValue Flag;
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004428 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00004429
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004430 // Copy the result values into the output registers.
4431 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4432 CCValAssign &VA = RVLocs[i];
4433 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand86aef0a2012-11-05 19:39:45 +00004434
4435 SDValue Arg = OutVals[i];
4436
4437 switch (VA.getLocInfo()) {
4438 default: llvm_unreachable("Unknown loc info!");
4439 case CCValAssign::Full: break;
4440 case CCValAssign::AExt:
4441 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4442 break;
4443 case CCValAssign::ZExt:
4444 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4445 break;
4446 case CCValAssign::SExt:
4447 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4448 break;
4449 }
4450
4451 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004452 Flag = Chain.getValue(1);
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004453 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004454 }
4455
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004456 RetOps[0] = Chain; // Update chain.
4457
4458 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00004459 if (Flag.getNode())
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004460 RetOps.push_back(Flag);
4461
4462 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other,
4463 &RetOps[0], RetOps.size());
Chris Lattner1a635d62006-04-14 06:01:58 +00004464}
4465
Dan Gohman475871a2008-07-27 21:46:04 +00004466SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004467 const PPCSubtarget &Subtarget) const {
Jim Laskeyefc7e522006-12-04 22:04:42 +00004468 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004469 SDLoc dl(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004470
Jim Laskeyefc7e522006-12-04 22:04:42 +00004471 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00004472 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeyefc7e522006-12-04 22:04:42 +00004473
4474 // Construct the stack pointer operand.
Dale Johannesenb60d5192009-11-24 01:09:07 +00004475 bool isPPC64 = Subtarget.isPPC64();
4476 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman475871a2008-07-27 21:46:04 +00004477 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeyefc7e522006-12-04 22:04:42 +00004478
4479 // Get the operands for the STACKRESTORE.
Dan Gohman475871a2008-07-27 21:46:04 +00004480 SDValue Chain = Op.getOperand(0);
4481 SDValue SaveSP = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004482
Jim Laskeyefc7e522006-12-04 22:04:42 +00004483 // Load the old link SP.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004484 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4485 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004486 false, false, false, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004487
Jim Laskeyefc7e522006-12-04 22:04:42 +00004488 // Restore the stack pointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004489 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelfdc40a02009-02-17 22:15:04 +00004490
Jim Laskeyefc7e522006-12-04 22:04:42 +00004491 // Store the old link SP.
Chris Lattner6229d0a2010-09-21 18:41:36 +00004492 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00004493 false, false, 0);
Jim Laskeyefc7e522006-12-04 22:04:42 +00004494}
4495
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004496
4497
Dan Gohman475871a2008-07-27 21:46:04 +00004498SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004499PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00004500 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesenb60d5192009-11-24 01:09:07 +00004501 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004502 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00004503 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004504
4505 // Get current frame pointer save index. The users of this index will be
4506 // primarily DYNALLOC instructions.
4507 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4508 int RASI = FI->getReturnAddrSaveIndex();
4509
4510 // If the frame pointer save index hasn't been defined yet.
4511 if (!RASI) {
4512 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00004513 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004514 // Allocate the frame index for frame pointer save area.
Evan Chenged2ae132010-07-03 00:40:23 +00004515 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004516 // Save the result.
4517 FI->setReturnAddrSaveIndex(RASI);
4518 }
4519 return DAG.getFrameIndex(RASI, PtrVT);
4520}
4521
Dan Gohman475871a2008-07-27 21:46:04 +00004522SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004523PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4524 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesenb60d5192009-11-24 01:09:07 +00004525 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004526 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00004527 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00004528
4529 // Get current frame pointer save index. The users of this index will be
4530 // primarily DYNALLOC instructions.
4531 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4532 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004533
Jim Laskey2f616bf2006-11-16 22:43:37 +00004534 // If the frame pointer save index hasn't been defined yet.
4535 if (!FPSI) {
4536 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00004537 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004538 isDarwinABI);
Scott Michelfdc40a02009-02-17 22:15:04 +00004539
Jim Laskey2f616bf2006-11-16 22:43:37 +00004540 // Allocate the frame index for frame pointer save area.
Evan Chenged2ae132010-07-03 00:40:23 +00004541 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004542 // Save the result.
Scott Michelfdc40a02009-02-17 22:15:04 +00004543 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004544 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004545 return DAG.getFrameIndex(FPSI, PtrVT);
4546}
Jim Laskey2f616bf2006-11-16 22:43:37 +00004547
Dan Gohman475871a2008-07-27 21:46:04 +00004548SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004549 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004550 const PPCSubtarget &Subtarget) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00004551 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004552 SDValue Chain = Op.getOperand(0);
4553 SDValue Size = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004554 SDLoc dl(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004555
Jim Laskey2f616bf2006-11-16 22:43:37 +00004556 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00004557 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00004558 // Negate the size.
Dale Johannesende064702009-02-06 21:50:26 +00004559 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey2f616bf2006-11-16 22:43:37 +00004560 DAG.getConstant(0, PtrVT), Size);
4561 // Construct a node for the frame pointer save index.
Dan Gohman475871a2008-07-27 21:46:04 +00004562 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004563 // Build a DYNALLOC node.
Dan Gohman475871a2008-07-27 21:46:04 +00004564 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson825b72b2009-08-11 20:47:22 +00004565 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesende064702009-02-06 21:50:26 +00004566 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004567}
4568
Hal Finkel7ee74a62013-03-21 21:37:52 +00004569SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
4570 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004571 SDLoc DL(Op);
Hal Finkel7ee74a62013-03-21 21:37:52 +00004572 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
4573 DAG.getVTList(MVT::i32, MVT::Other),
4574 Op.getOperand(0), Op.getOperand(1));
4575}
4576
4577SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
4578 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004579 SDLoc DL(Op);
Hal Finkel7ee74a62013-03-21 21:37:52 +00004580 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
4581 Op.getOperand(0), Op.getOperand(1));
4582}
4583
Chris Lattner1a635d62006-04-14 06:01:58 +00004584/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4585/// possible.
Dan Gohmand858e902010-04-17 15:26:15 +00004586SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner1a635d62006-04-14 06:01:58 +00004587 // Not FP? Not a fsel.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004588 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4589 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedmanc06441e2009-05-28 04:31:08 +00004590 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004591
Hal Finkel59889f72013-04-07 22:11:09 +00004592 // We might be able to do better than this under some circumstances, but in
4593 // general, fsel-based lowering of select is a finite-math-only optimization.
4594 // For more information, see section F.3 of the 2.06 ISA specification.
4595 if (!DAG.getTarget().Options.NoInfsFPMath ||
4596 !DAG.getTarget().Options.NoNaNsFPMath)
4597 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004598
Hal Finkel59889f72013-04-07 22:11:09 +00004599 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004600
Owen Andersone50ed302009-08-10 22:56:29 +00004601 EVT ResVT = Op.getValueType();
4602 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004603 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4604 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004605 SDLoc dl(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004606
Chris Lattner1a635d62006-04-14 06:01:58 +00004607 // If the RHS of the comparison is a 0.0, we don't need to do the
4608 // subtraction at all.
Hal Finkel59889f72013-04-07 22:11:09 +00004609 SDValue Sel1;
Chris Lattner1a635d62006-04-14 06:01:58 +00004610 if (isFloatingPointZero(RHS))
4611 switch (CC) {
4612 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel59889f72013-04-07 22:11:09 +00004613 case ISD::SETNE:
4614 std::swap(TV, FV);
4615 case ISD::SETEQ:
4616 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4617 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
4618 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
4619 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4620 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4621 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4622 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004623 case ISD::SETULT:
4624 case ISD::SETLT:
4625 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00004626 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004627 case ISD::SETGE:
Owen Anderson825b72b2009-08-11 20:47:22 +00004628 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4629 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00004630 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004631 case ISD::SETUGT:
4632 case ISD::SETGT:
4633 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00004634 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004635 case ISD::SETLE:
Owen Anderson825b72b2009-08-11 20:47:22 +00004636 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4637 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00004638 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00004639 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004640 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004641
Dan Gohman475871a2008-07-27 21:46:04 +00004642 SDValue Cmp;
Chris Lattner1a635d62006-04-14 06:01:58 +00004643 switch (CC) {
4644 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel59889f72013-04-07 22:11:09 +00004645 case ISD::SETNE:
4646 std::swap(TV, FV);
4647 case ISD::SETEQ:
4648 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
4649 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4650 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
4651 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
4652 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4653 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4654 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4655 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004656 case ISD::SETULT:
4657 case ISD::SETLT:
Dale Johannesende064702009-02-06 21:50:26 +00004658 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004659 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4660 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel59889f72013-04-07 22:11:09 +00004661 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00004662 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004663 case ISD::SETGE:
Dale Johannesende064702009-02-06 21:50:26 +00004664 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004665 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4666 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel59889f72013-04-07 22:11:09 +00004667 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004668 case ISD::SETUGT:
4669 case ISD::SETGT:
Dale Johannesende064702009-02-06 21:50:26 +00004670 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004671 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4672 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel59889f72013-04-07 22:11:09 +00004673 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00004674 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004675 case ISD::SETLE:
Dale Johannesende064702009-02-06 21:50:26 +00004676 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004677 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4678 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel59889f72013-04-07 22:11:09 +00004679 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004680 }
Eli Friedmanc06441e2009-05-28 04:31:08 +00004681 return Op;
Chris Lattner1a635d62006-04-14 06:01:58 +00004682}
4683
Chris Lattner1f873002007-11-28 18:44:47 +00004684// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004685SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004686 SDLoc dl) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004687 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman475871a2008-07-27 21:46:04 +00004688 SDValue Src = Op.getOperand(0);
Bill Schmidt12ae7fd2013-07-08 14:22:45 +00004689
4690 // If we have a long double here, it must be that we have an undef of
4691 // that type. In this case return an undef of the target type.
4692 if (Src.getValueType() == MVT::ppcf128) {
4693 assert(Src.getOpcode() == ISD::UNDEF && "Unhandled ppcf128!");
4694 return DAG.getNode(ISD::UNDEF, dl,
4695 Op.getValueType().getSimpleVT().SimpleTy);
4696 }
4697
Owen Anderson825b72b2009-08-11 20:47:22 +00004698 if (Src.getValueType() == MVT::f32)
4699 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sandsa7360f02008-07-19 16:26:02 +00004700
Dan Gohman475871a2008-07-27 21:46:04 +00004701 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00004702 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004703 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004704 case MVT::i32:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004705 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Hal Finkel46479192013-04-01 17:52:07 +00004706 (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ :
4707 PPCISD::FCTIDZ),
Owen Anderson825b72b2009-08-11 20:47:22 +00004708 dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00004709 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004710 case MVT::i64:
Hal Finkela1646ce2013-04-01 18:42:58 +00004711 assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) &&
4712 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkel46479192013-04-01 17:52:07 +00004713 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
4714 PPCISD::FCTIDUZ,
4715 dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00004716 break;
4717 }
Duncan Sandsa7360f02008-07-19 16:26:02 +00004718
Chris Lattner1a635d62006-04-14 06:01:58 +00004719 // Convert the FP value to an int value through memory.
Hal Finkel46479192013-04-01 17:52:07 +00004720 bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() &&
4721 (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT());
4722 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
4723 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
4724 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sandsa7360f02008-07-19 16:26:02 +00004725
Chris Lattner1de7c1d2007-10-15 20:14:52 +00004726 // Emit a store to the stack slot.
Hal Finkel46479192013-04-01 17:52:07 +00004727 SDValue Chain;
4728 if (i32Stack) {
4729 MachineFunction &MF = DAG.getMachineFunction();
4730 MachineMemOperand *MMO =
4731 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
4732 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
4733 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
4734 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
4735 MVT::i32, MMO);
4736 } else
4737 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
4738 MPI, false, false, 0);
Chris Lattner1de7c1d2007-10-15 20:14:52 +00004739
4740 // Result is a load from the stack slot. If loading 4 bytes, make sure to
4741 // add in a bias.
Hal Finkel46479192013-04-01 17:52:07 +00004742 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004743 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner1de7c1d2007-10-15 20:14:52 +00004744 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkel46479192013-04-01 17:52:07 +00004745 MPI = MachinePointerInfo();
4746 }
4747
4748 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004749 false, false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004750}
4751
Hal Finkel46479192013-04-01 17:52:07 +00004752SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004753 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004754 SDLoc dl(Op);
Dan Gohman034f60e2008-03-11 01:59:03 +00004755 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson825b72b2009-08-11 20:47:22 +00004756 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman475871a2008-07-27 21:46:04 +00004757 return SDValue();
Dan Gohman034f60e2008-03-11 01:59:03 +00004758
Hal Finkel46479192013-04-01 17:52:07 +00004759 assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) &&
4760 "UINT_TO_FP is supported only with FPCVT");
4761
4762 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel2a401952013-04-02 03:29:51 +00004763 // Otherwise, convert to double-precision and then round.
Hal Finkel46479192013-04-01 17:52:07 +00004764 unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4765 (Op.getOpcode() == ISD::UINT_TO_FP ?
4766 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
4767 (Op.getOpcode() == ISD::UINT_TO_FP ?
4768 PPCISD::FCFIDU : PPCISD::FCFID);
4769 MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4770 MVT::f32 : MVT::f64;
4771
Owen Anderson825b72b2009-08-11 20:47:22 +00004772 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigand6c28a7e2012-10-18 13:16:11 +00004773 SDValue SINT = Op.getOperand(0);
4774 // When converting to single-precision, we actually need to convert
4775 // to double-precision first and then round to single-precision.
4776 // To avoid double-rounding effects during that operation, we have
4777 // to prepare the input operand. Bits that might be truncated when
4778 // converting to double-precision are replaced by a bit that won't
4779 // be lost at this stage, but is below the single-precision rounding
4780 // position.
4781 //
4782 // However, if -enable-unsafe-fp-math is in effect, accept double
4783 // rounding to avoid the extra overhead.
4784 if (Op.getValueType() == MVT::f32 &&
Hal Finkel46479192013-04-01 17:52:07 +00004785 !PPCSubTarget.hasFPCVT() &&
Ulrich Weigand6c28a7e2012-10-18 13:16:11 +00004786 !DAG.getTarget().Options.UnsafeFPMath) {
4787
4788 // Twiddle input to make sure the low 11 bits are zero. (If this
4789 // is the case, we are guaranteed the value will fit into the 53 bit
4790 // mantissa of an IEEE double-precision value without rounding.)
4791 // If any of those low 11 bits were not zero originally, make sure
4792 // bit 12 (value 2048) is set instead, so that the final rounding
4793 // to single-precision gets the correct result.
4794 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4795 SINT, DAG.getConstant(2047, MVT::i64));
4796 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
4797 Round, DAG.getConstant(2047, MVT::i64));
4798 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
4799 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4800 Round, DAG.getConstant(-2048, MVT::i64));
4801
4802 // However, we cannot use that value unconditionally: if the magnitude
4803 // of the input value is small, the bit-twiddling we did above might
4804 // end up visibly changing the output. Fortunately, in that case, we
4805 // don't need to twiddle bits since the original input will convert
4806 // exactly to double-precision floating-point already. Therefore,
4807 // construct a conditional to use the original value if the top 11
4808 // bits are all sign-bit copies, and use the rounded value computed
4809 // above otherwise.
4810 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
4811 SINT, DAG.getConstant(53, MVT::i32));
4812 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
4813 Cond, DAG.getConstant(1, MVT::i64));
4814 Cond = DAG.getSetCC(dl, MVT::i32,
4815 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
4816
4817 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
4818 }
Hal Finkel46479192013-04-01 17:52:07 +00004819
Ulrich Weigand6c28a7e2012-10-18 13:16:11 +00004820 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkel46479192013-04-01 17:52:07 +00004821 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
4822
4823 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Scott Michelfdc40a02009-02-17 22:15:04 +00004824 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004825 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00004826 return FP;
4827 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004828
Owen Anderson825b72b2009-08-11 20:47:22 +00004829 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkel46479192013-04-01 17:52:07 +00004830 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner1a635d62006-04-14 06:01:58 +00004831 // Since we only generate this in 64-bit mode, we can take advantage of
4832 // 64-bit registers. In particular, sign extend the input value into the
4833 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
4834 // then lfd it and fcfid it.
Dan Gohmanc76909a2009-09-25 20:36:54 +00004835 MachineFunction &MF = DAG.getMachineFunction();
4836 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004837 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfdc40a02009-02-17 22:15:04 +00004838
Hal Finkel8049ab12013-03-31 10:12:51 +00004839 SDValue Ld;
Hal Finkel46479192013-04-01 17:52:07 +00004840 if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) {
Hal Finkel8049ab12013-03-31 10:12:51 +00004841 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
4842 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004843
Hal Finkel8049ab12013-03-31 10:12:51 +00004844 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
4845 MachinePointerInfo::getFixedStack(FrameIdx),
4846 false, false, 0);
Hal Finkel9ad0f492013-03-31 01:58:02 +00004847
Hal Finkel8049ab12013-03-31 10:12:51 +00004848 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
4849 "Expected an i32 store");
4850 MachineMemOperand *MMO =
4851 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
4852 MachineMemOperand::MOLoad, 4, 4);
4853 SDValue Ops[] = { Store, FIdx };
Hal Finkel46479192013-04-01 17:52:07 +00004854 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
4855 PPCISD::LFIWZX : PPCISD::LFIWAX,
4856 dl, DAG.getVTList(MVT::f64, MVT::Other),
4857 Ops, 2, MVT::i32, MMO);
Hal Finkel8049ab12013-03-31 10:12:51 +00004858 } else {
Hal Finkel46479192013-04-01 17:52:07 +00004859 assert(PPCSubTarget.isPPC64() &&
4860 "i32->FP without LFIWAX supported only on PPC64");
4861
Hal Finkel8049ab12013-03-31 10:12:51 +00004862 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
4863 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
4864
4865 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
4866 Op.getOperand(0));
4867
4868 // STD the extended value into the stack slot.
4869 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
4870 MachinePointerInfo::getFixedStack(FrameIdx),
4871 false, false, 0);
4872
4873 // Load the value as a double.
4874 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
4875 MachinePointerInfo::getFixedStack(FrameIdx),
4876 false, false, false, 0);
4877 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004878
Chris Lattner1a635d62006-04-14 06:01:58 +00004879 // FCFID it and return it.
Hal Finkel46479192013-04-01 17:52:07 +00004880 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
4881 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Owen Anderson825b72b2009-08-11 20:47:22 +00004882 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00004883 return FP;
4884}
4885
Dan Gohmand858e902010-04-17 15:26:15 +00004886SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
4887 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004888 SDLoc dl(Op);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004889 /*
4890 The rounding mode is in bits 30:31 of FPSR, and has the following
4891 settings:
4892 00 Round to nearest
4893 01 Round to 0
4894 10 Round to +inf
4895 11 Round to -inf
4896
4897 FLT_ROUNDS, on the other hand, expects the following:
4898 -1 Undefined
4899 0 Round to 0
4900 1 Round to nearest
4901 2 Round to +inf
4902 3 Round to -inf
4903
4904 To perform the conversion, we do:
4905 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
4906 */
4907
4908 MachineFunction &MF = DAG.getMachineFunction();
Owen Andersone50ed302009-08-10 22:56:29 +00004909 EVT VT = Op.getValueType();
4910 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00004911 SDValue MFFSreg, InFlag;
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004912
4913 // Save FP Control Word to register
Benjamin Kramer3853f742013-03-07 20:33:29 +00004914 EVT NodeTys[] = {
4915 MVT::f64, // return register
4916 MVT::Glue // unused in this context
4917 };
Dale Johannesen33c960f2009-02-04 20:06:27 +00004918 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004919
4920 // Save FP register to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00004921 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00004922 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00004923 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner6229d0a2010-09-21 18:41:36 +00004924 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004925
4926 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00004927 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00004928 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004929 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004930 false, false, false, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004931
4932 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00004933 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00004934 DAG.getNode(ISD::AND, dl, MVT::i32,
4935 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00004936 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00004937 DAG.getNode(ISD::SRL, dl, MVT::i32,
4938 DAG.getNode(ISD::AND, dl, MVT::i32,
4939 DAG.getNode(ISD::XOR, dl, MVT::i32,
4940 CWD, DAG.getConstant(3, MVT::i32)),
4941 DAG.getConstant(3, MVT::i32)),
4942 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004943
Dan Gohman475871a2008-07-27 21:46:04 +00004944 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00004945 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004946
Duncan Sands83ec4b62008-06-06 12:08:01 +00004947 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen33c960f2009-02-04 20:06:27 +00004948 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004949}
4950
Dan Gohmand858e902010-04-17 15:26:15 +00004951SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004952 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004953 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004954 SDLoc dl(Op);
Dan Gohman9ed06db2008-03-07 20:36:53 +00004955 assert(Op.getNumOperands() == 3 &&
4956 VT == Op.getOperand(1).getValueType() &&
4957 "Unexpected SHL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004958
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00004959 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00004960 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00004961 SDValue Lo = Op.getOperand(0);
4962 SDValue Hi = Op.getOperand(1);
4963 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00004964 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00004965
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004966 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00004967 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004968 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
4969 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
4970 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
4971 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00004972 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004973 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
4974 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
4975 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00004976 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004977 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00004978}
4979
Dan Gohmand858e902010-04-17 15:26:15 +00004980SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004981 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004982 SDLoc dl(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004983 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00004984 assert(Op.getNumOperands() == 3 &&
4985 VT == Op.getOperand(1).getValueType() &&
4986 "Unexpected SRL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004987
Dan Gohman9ed06db2008-03-07 20:36:53 +00004988 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00004989 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00004990 SDValue Lo = Op.getOperand(0);
4991 SDValue Hi = Op.getOperand(1);
4992 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00004993 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00004994
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004995 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00004996 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00004997 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
4998 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
4999 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5000 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005001 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005002 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5003 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5004 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00005005 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005006 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00005007}
5008
Dan Gohmand858e902010-04-17 15:26:15 +00005009SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005010 SDLoc dl(Op);
Owen Andersone50ed302009-08-10 22:56:29 +00005011 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005012 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00005013 assert(Op.getNumOperands() == 3 &&
5014 VT == Op.getOperand(1).getValueType() &&
5015 "Unexpected SRA!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005016
Dan Gohman9ed06db2008-03-07 20:36:53 +00005017 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman475871a2008-07-27 21:46:04 +00005018 SDValue Lo = Op.getOperand(0);
5019 SDValue Hi = Op.getOperand(1);
5020 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005021 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005022
Dale Johannesenf5d97892009-02-04 01:48:28 +00005023 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005024 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf5d97892009-02-04 01:48:28 +00005025 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5026 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5027 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5028 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005029 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf5d97892009-02-04 01:48:28 +00005030 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5031 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5032 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005033 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman475871a2008-07-27 21:46:04 +00005034 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005035 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00005036}
5037
5038//===----------------------------------------------------------------------===//
5039// Vector related lowering.
5040//
5041
Chris Lattner4a998b92006-04-17 06:00:21 +00005042/// BuildSplatI - Build a canonical splati of Val with an element size of
5043/// SplatSize. Cast the result to VT.
Owen Andersone50ed302009-08-10 22:56:29 +00005044static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005045 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner4a998b92006-04-17 06:00:21 +00005046 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner70fa4932006-12-01 01:45:39 +00005047
Owen Andersone50ed302009-08-10 22:56:29 +00005048 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson825b72b2009-08-11 20:47:22 +00005049 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner4a998b92006-04-17 06:00:21 +00005050 };
Chris Lattner70fa4932006-12-01 01:45:39 +00005051
Owen Anderson825b72b2009-08-11 20:47:22 +00005052 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00005053
Chris Lattner70fa4932006-12-01 01:45:39 +00005054 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5055 if (Val == -1)
5056 SplatSize = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00005057
Owen Andersone50ed302009-08-10 22:56:29 +00005058 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00005059
Chris Lattner4a998b92006-04-17 06:00:21 +00005060 // Build a canonical splat for this value.
Owen Anderson825b72b2009-08-11 20:47:22 +00005061 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00005062 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005063 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga87008d2009-02-25 22:49:59 +00005064 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
5065 &Ops[0], Ops.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005066 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00005067}
5068
Hal Finkel80d10de2013-05-24 23:00:14 +00005069/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5070/// specified intrinsic ID.
5071static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005072 SelectionDAG &DAG, SDLoc dl,
Hal Finkel80d10de2013-05-24 23:00:14 +00005073 EVT DestVT = MVT::Other) {
5074 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5075 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5076 DAG.getConstant(IID, MVT::i32), Op);
5077}
5078
Chris Lattnere7c768e2006-04-18 03:24:30 +00005079/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00005080/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00005081static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005082 SelectionDAG &DAG, SDLoc dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005083 EVT DestVT = MVT::Other) {
5084 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00005085 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005086 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner6876e662006-04-17 06:58:41 +00005087}
5088
Chris Lattnere7c768e2006-04-18 03:24:30 +00005089/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5090/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00005091static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesened2eee62009-02-06 01:31:28 +00005092 SDValue Op2, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005093 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005094 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00005095 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005096 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnere7c768e2006-04-18 03:24:30 +00005097}
5098
5099
Chris Lattnerbdd558c2006-04-17 17:55:10 +00005100/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5101/// amount. The result has the specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005102static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005103 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattnerbdd558c2006-04-17 17:55:10 +00005104 // Force LHS/RHS to be the right type.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005105 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5106 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsd038e042008-07-21 10:20:31 +00005107
Nate Begeman9008ca62009-04-27 18:41:29 +00005108 int Ops[16];
Chris Lattnerbdd558c2006-04-17 17:55:10 +00005109 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005110 Ops[i] = i + Amt;
Owen Anderson825b72b2009-08-11 20:47:22 +00005111 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005112 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00005113}
5114
Chris Lattnerf1b47082006-04-14 05:19:18 +00005115// If this is a case we can't handle, return null and let the default
5116// expansion code take care of it. If we CAN select this case, and if it
5117// selects to a single instruction, return Op. Otherwise, if we can codegen
5118// this case more efficiently than a constant pool load, lower it to the
5119// sequence of ops that should be used.
Dan Gohmand858e902010-04-17 15:26:15 +00005120SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5121 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005122 SDLoc dl(Op);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005123 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5124 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Micheldf380432009-02-25 03:12:50 +00005125
Bob Wilson24e338e2009-03-02 23:24:16 +00005126 // Check if this is a splat of a constant value.
5127 APInt APSplatBits, APSplatUndef;
5128 unsigned SplatBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005129 bool HasAnyUndefs;
Bob Wilsonf2950b02009-03-03 19:26:27 +00005130 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen1e608812009-11-13 01:45:18 +00005131 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilsonf2950b02009-03-03 19:26:27 +00005132 return SDValue();
Evan Chenga87008d2009-02-25 22:49:59 +00005133
Bob Wilsonf2950b02009-03-03 19:26:27 +00005134 unsigned SplatBits = APSplatBits.getZExtValue();
5135 unsigned SplatUndef = APSplatUndef.getZExtValue();
5136 unsigned SplatSize = SplatBitSize / 8;
Scott Michelfdc40a02009-02-17 22:15:04 +00005137
Bob Wilsonf2950b02009-03-03 19:26:27 +00005138 // First, handle single instruction cases.
5139
5140 // All zeros?
5141 if (SplatBits == 0) {
5142 // Canonicalize all zero vectors to be v4i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005143 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5144 SDValue Z = DAG.getConstant(0, MVT::i32);
5145 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005146 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattnerf1b47082006-04-14 05:19:18 +00005147 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00005148 return Op;
5149 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00005150
Bob Wilsonf2950b02009-03-03 19:26:27 +00005151 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5152 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5153 (32-SplatBitSize));
5154 if (SextVal >= -16 && SextVal <= 15)
5155 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005156
5157
Bob Wilsonf2950b02009-03-03 19:26:27 +00005158 // Two instruction sequences.
Scott Michelfdc40a02009-02-17 22:15:04 +00005159
Bob Wilsonf2950b02009-03-03 19:26:27 +00005160 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtabc40282013-02-20 20:41:42 +00005161 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5162 // If this value is in the range [17,31] and is odd, use:
5163 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5164 // If this value is in the range [-31,-17] and is odd, use:
5165 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5166 // Note the last two are three-instruction sequences.
5167 if (SextVal >= -32 && SextVal <= 31) {
5168 // To avoid having these optimizations undone by constant folding,
5169 // we convert to a pseudo that will be expanded later into one of
5170 // the above forms.
5171 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidtb34c79e2013-02-20 15:50:31 +00005172 EVT VT = Op.getValueType();
5173 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5174 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5175 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005176 }
5177
5178 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5179 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5180 // for fneg/fabs.
5181 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5182 // Make -1 and vspltisw -1:
Owen Anderson825b72b2009-08-11 20:47:22 +00005183 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005184
5185 // Make the VSLW intrinsic, computing 0x8000_0000.
5186 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5187 OnesV, DAG, dl);
5188
5189 // xor by OnesV to invert it.
Owen Anderson825b72b2009-08-11 20:47:22 +00005190 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005191 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005192 }
5193
5194 // Check to see if this is a wide variety of vsplti*, binop self cases.
5195 static const signed char SplatCsts[] = {
5196 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5197 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5198 };
5199
5200 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5201 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5202 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5203 int i = SplatCsts[idx];
5204
5205 // Figure out what shift amount will be used by altivec if shifted by i in
5206 // this splat size.
5207 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5208
5209 // vsplti + shl self.
Richard Smith1144af32012-08-24 23:29:28 +00005210 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005211 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005212 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5213 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5214 Intrinsic::ppc_altivec_vslw
5215 };
5216 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005217 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00005218 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005219
Bob Wilsonf2950b02009-03-03 19:26:27 +00005220 // vsplti + srl self.
5221 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005222 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005223 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5224 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5225 Intrinsic::ppc_altivec_vsrw
5226 };
5227 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005228 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00005229 }
5230
Bob Wilsonf2950b02009-03-03 19:26:27 +00005231 // vsplti + sra self.
5232 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005233 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005234 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5235 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5236 Intrinsic::ppc_altivec_vsraw
5237 };
5238 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005239 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00005240 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005241
Bob Wilsonf2950b02009-03-03 19:26:27 +00005242 // vsplti + rol self.
5243 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5244 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005245 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005246 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5247 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5248 Intrinsic::ppc_altivec_vrlw
5249 };
5250 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005251 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005252 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005253
Bob Wilsonf2950b02009-03-03 19:26:27 +00005254 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith1144af32012-08-24 23:29:28 +00005255 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005256 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005257 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnerdbce85d2006-04-17 18:09:22 +00005258 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00005259 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith1144af32012-08-24 23:29:28 +00005260 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005261 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005262 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattnerf1b47082006-04-14 05:19:18 +00005263 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00005264 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith1144af32012-08-24 23:29:28 +00005265 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005266 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005267 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5268 }
5269 }
5270
Dan Gohman475871a2008-07-27 21:46:04 +00005271 return SDValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00005272}
5273
Chris Lattner59138102006-04-17 05:28:54 +00005274/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5275/// the specified operations to build the shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00005276static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00005277 SDValue RHS, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005278 SDLoc dl) {
Chris Lattner59138102006-04-17 05:28:54 +00005279 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling77959322008-09-17 00:30:57 +00005280 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner59138102006-04-17 05:28:54 +00005281 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005282
Chris Lattner59138102006-04-17 05:28:54 +00005283 enum {
Chris Lattner00402c72006-05-16 04:20:24 +00005284 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 +00005285 OP_VMRGHW,
5286 OP_VMRGLW,
5287 OP_VSPLTISW0,
5288 OP_VSPLTISW1,
5289 OP_VSPLTISW2,
5290 OP_VSPLTISW3,
5291 OP_VSLDOI4,
5292 OP_VSLDOI8,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00005293 OP_VSLDOI12
Chris Lattner59138102006-04-17 05:28:54 +00005294 };
Scott Michelfdc40a02009-02-17 22:15:04 +00005295
Chris Lattner59138102006-04-17 05:28:54 +00005296 if (OpNum == OP_COPY) {
5297 if (LHSID == (1*9+2)*9+3) return LHS;
5298 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5299 return RHS;
5300 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005301
Dan Gohman475871a2008-07-27 21:46:04 +00005302 SDValue OpLHS, OpRHS;
Dale Johannesened2eee62009-02-06 01:31:28 +00005303 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5304 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005305
Nate Begeman9008ca62009-04-27 18:41:29 +00005306 int ShufIdxs[16];
Chris Lattner59138102006-04-17 05:28:54 +00005307 switch (OpNum) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005308 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner59138102006-04-17 05:28:54 +00005309 case OP_VMRGHW:
5310 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5311 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5312 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5313 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5314 break;
5315 case OP_VMRGLW:
5316 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5317 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5318 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5319 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5320 break;
5321 case OP_VSPLTISW0:
5322 for (unsigned i = 0; i != 16; ++i)
5323 ShufIdxs[i] = (i&3)+0;
5324 break;
5325 case OP_VSPLTISW1:
5326 for (unsigned i = 0; i != 16; ++i)
5327 ShufIdxs[i] = (i&3)+4;
5328 break;
5329 case OP_VSPLTISW2:
5330 for (unsigned i = 0; i != 16; ++i)
5331 ShufIdxs[i] = (i&3)+8;
5332 break;
5333 case OP_VSPLTISW3:
5334 for (unsigned i = 0; i != 16; ++i)
5335 ShufIdxs[i] = (i&3)+12;
5336 break;
5337 case OP_VSLDOI4:
Dale Johannesened2eee62009-02-06 01:31:28 +00005338 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005339 case OP_VSLDOI8:
Dale Johannesened2eee62009-02-06 01:31:28 +00005340 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005341 case OP_VSLDOI12:
Dale Johannesened2eee62009-02-06 01:31:28 +00005342 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005343 }
Owen Andersone50ed302009-08-10 22:56:29 +00005344 EVT VT = OpLHS.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005345 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5346 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00005347 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005348 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner59138102006-04-17 05:28:54 +00005349}
5350
Chris Lattnerf1b47082006-04-14 05:19:18 +00005351/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5352/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5353/// return the code it can be lowered into. Worst case, it can always be
5354/// lowered into a vperm.
Scott Michelfdc40a02009-02-17 22:15:04 +00005355SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005356 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005357 SDLoc dl(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005358 SDValue V1 = Op.getOperand(0);
5359 SDValue V2 = Op.getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005360 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Andersone50ed302009-08-10 22:56:29 +00005361 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005362
Chris Lattnerf1b47082006-04-14 05:19:18 +00005363 // Cases that are handled by instructions that take permute immediates
5364 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5365 // selected by the instruction selector.
5366 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005367 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5368 PPC::isSplatShuffleMask(SVOp, 2) ||
5369 PPC::isSplatShuffleMask(SVOp, 4) ||
5370 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5371 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5372 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5373 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5374 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5375 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5376 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5377 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5378 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00005379 return Op;
5380 }
5381 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005382
Chris Lattnerf1b47082006-04-14 05:19:18 +00005383 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5384 // and produce a fixed permutation. If any of these match, do not lower to
5385 // VPERM.
Nate Begeman9008ca62009-04-27 18:41:29 +00005386 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5387 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5388 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5389 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5390 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5391 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5392 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5393 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5394 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattnerf1b47082006-04-14 05:19:18 +00005395 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005396
Chris Lattner59138102006-04-17 05:28:54 +00005397 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5398 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005399 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005400
Chris Lattner59138102006-04-17 05:28:54 +00005401 unsigned PFIndexes[4];
5402 bool isFourElementShuffle = true;
5403 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5404 unsigned EltNo = 8; // Start out undef.
5405 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman9008ca62009-04-27 18:41:29 +00005406 if (PermMask[i*4+j] < 0)
Chris Lattner59138102006-04-17 05:28:54 +00005407 continue; // Undef, ignore it.
Scott Michelfdc40a02009-02-17 22:15:04 +00005408
Nate Begeman9008ca62009-04-27 18:41:29 +00005409 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner59138102006-04-17 05:28:54 +00005410 if ((ByteSource & 3) != j) {
5411 isFourElementShuffle = false;
5412 break;
5413 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005414
Chris Lattner59138102006-04-17 05:28:54 +00005415 if (EltNo == 8) {
5416 EltNo = ByteSource/4;
5417 } else if (EltNo != ByteSource/4) {
5418 isFourElementShuffle = false;
5419 break;
5420 }
5421 }
5422 PFIndexes[i] = EltNo;
5423 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005424
5425 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner59138102006-04-17 05:28:54 +00005426 // perfect shuffle vector to determine if it is cost effective to do this as
5427 // discrete instructions, or whether we should use a vperm.
5428 if (isFourElementShuffle) {
5429 // Compute the index in the perfect shuffle table.
Scott Michelfdc40a02009-02-17 22:15:04 +00005430 unsigned PFTableIndex =
Chris Lattner59138102006-04-17 05:28:54 +00005431 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelfdc40a02009-02-17 22:15:04 +00005432
Chris Lattner59138102006-04-17 05:28:54 +00005433 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5434 unsigned Cost = (PFEntry >> 30);
Scott Michelfdc40a02009-02-17 22:15:04 +00005435
Chris Lattner59138102006-04-17 05:28:54 +00005436 // Determining when to avoid vperm is tricky. Many things affect the cost
5437 // of vperm, particularly how many times the perm mask needs to be computed.
5438 // For example, if the perm mask can be hoisted out of a loop or is already
5439 // used (perhaps because there are multiple permutes with the same shuffle
5440 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5441 // the loop requires an extra register.
5442 //
5443 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelfdc40a02009-02-17 22:15:04 +00005444 // generated in 3 or fewer operations. When we have loop information
Chris Lattner59138102006-04-17 05:28:54 +00005445 // available, if this block is within a loop, we should avoid using vperm
5446 // for 3-operation perms and use a constant pool load instead.
Scott Michelfdc40a02009-02-17 22:15:04 +00005447 if (Cost < 3)
Dale Johannesened2eee62009-02-06 01:31:28 +00005448 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005449 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005450
Chris Lattnerf1b47082006-04-14 05:19:18 +00005451 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5452 // vector that will get spilled to the constant pool.
5453 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelfdc40a02009-02-17 22:15:04 +00005454
Chris Lattnerf1b47082006-04-14 05:19:18 +00005455 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5456 // that it is in input element units, not in bytes. Convert now.
Owen Andersone50ed302009-08-10 22:56:29 +00005457 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005458 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelfdc40a02009-02-17 22:15:04 +00005459
Dan Gohman475871a2008-07-27 21:46:04 +00005460 SmallVector<SDValue, 16> ResultMask;
Nate Begeman9008ca62009-04-27 18:41:29 +00005461 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5462 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00005463
Chris Lattnerf1b47082006-04-14 05:19:18 +00005464 for (unsigned j = 0; j != BytesPerElement; ++j)
5465 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson825b72b2009-08-11 20:47:22 +00005466 MVT::i32));
Chris Lattnerf1b47082006-04-14 05:19:18 +00005467 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005468
Owen Anderson825b72b2009-08-11 20:47:22 +00005469 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga87008d2009-02-25 22:49:59 +00005470 &ResultMask[0], ResultMask.size());
Dale Johannesened2eee62009-02-06 01:31:28 +00005471 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattnerf1b47082006-04-14 05:19:18 +00005472}
5473
Chris Lattner90564f22006-04-18 17:59:36 +00005474/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5475/// altivec comparison. If it is, return true and fill in Opc/isDot with
5476/// information about the intrinsic.
Dan Gohman475871a2008-07-27 21:46:04 +00005477static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner90564f22006-04-18 17:59:36 +00005478 bool &isDot) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005479 unsigned IntrinsicID =
5480 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00005481 CompareOpc = -1;
5482 isDot = false;
5483 switch (IntrinsicID) {
5484 default: return false;
5485 // Comparison predicates.
Chris Lattner1a635d62006-04-14 06:01:58 +00005486 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5487 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5488 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5489 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5490 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5491 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5492 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5493 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5494 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5495 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5496 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5497 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5498 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005499
Chris Lattner1a635d62006-04-14 06:01:58 +00005500 // Normal Comparisons.
5501 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5502 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5503 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5504 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5505 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5506 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5507 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5508 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5509 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5510 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5511 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5512 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5513 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5514 }
Chris Lattner90564f22006-04-18 17:59:36 +00005515 return true;
5516}
5517
5518/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5519/// lower, do it, otherwise return null.
Scott Michelfdc40a02009-02-17 22:15:04 +00005520SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005521 SelectionDAG &DAG) const {
Chris Lattner90564f22006-04-18 17:59:36 +00005522 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5523 // opcode number of the comparison.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005524 SDLoc dl(Op);
Chris Lattner90564f22006-04-18 17:59:36 +00005525 int CompareOpc;
5526 bool isDot;
5527 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman475871a2008-07-27 21:46:04 +00005528 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelfdc40a02009-02-17 22:15:04 +00005529
Chris Lattner90564f22006-04-18 17:59:36 +00005530 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner1a635d62006-04-14 06:01:58 +00005531 if (!isDot) {
Dale Johannesen3484c092009-02-05 22:07:54 +00005532 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner149add02010-03-14 22:44:11 +00005533 Op.getOperand(1), Op.getOperand(2),
5534 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005535 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner1a635d62006-04-14 06:01:58 +00005536 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005537
Chris Lattner1a635d62006-04-14 06:01:58 +00005538 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman475871a2008-07-27 21:46:04 +00005539 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00005540 Op.getOperand(2), // LHS
5541 Op.getOperand(3), // RHS
Owen Anderson825b72b2009-08-11 20:47:22 +00005542 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00005543 };
Benjamin Kramer3853f742013-03-07 20:33:29 +00005544 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesen3484c092009-02-05 22:07:54 +00005545 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00005546
Chris Lattner1a635d62006-04-14 06:01:58 +00005547 // Now that we have the comparison, emit a copy from the CR to a GPR.
5548 // This is flagged to the above dot comparison.
Ulrich Weigand965b20e2013-07-03 17:05:42 +00005549 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005550 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelfdc40a02009-02-17 22:15:04 +00005551 CompNode.getValue(1));
5552
Chris Lattner1a635d62006-04-14 06:01:58 +00005553 // Unpack the result based on how the target uses it.
5554 unsigned BitNo; // Bit # of CR6.
5555 bool InvertBit; // Invert result?
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005556 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner1a635d62006-04-14 06:01:58 +00005557 default: // Can't happen, don't crash on invalid number though.
5558 case 0: // Return the value of the EQ bit of CR6.
5559 BitNo = 0; InvertBit = false;
5560 break;
5561 case 1: // Return the inverted value of the EQ bit of CR6.
5562 BitNo = 0; InvertBit = true;
5563 break;
5564 case 2: // Return the value of the LT bit of CR6.
5565 BitNo = 2; InvertBit = false;
5566 break;
5567 case 3: // Return the inverted value of the LT bit of CR6.
5568 BitNo = 2; InvertBit = true;
5569 break;
5570 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005571
Chris Lattner1a635d62006-04-14 06:01:58 +00005572 // Shift the bit into the low position.
Owen Anderson825b72b2009-08-11 20:47:22 +00005573 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5574 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00005575 // Isolate the bit.
Owen Anderson825b72b2009-08-11 20:47:22 +00005576 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5577 DAG.getConstant(1, MVT::i32));
Scott Michelfdc40a02009-02-17 22:15:04 +00005578
Chris Lattner1a635d62006-04-14 06:01:58 +00005579 // If we are supposed to, toggle the bit.
5580 if (InvertBit)
Owen Anderson825b72b2009-08-11 20:47:22 +00005581 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5582 DAG.getConstant(1, MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00005583 return Flags;
5584}
5585
Scott Michelfdc40a02009-02-17 22:15:04 +00005586SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005587 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005588 SDLoc dl(Op);
Chris Lattner1a635d62006-04-14 06:01:58 +00005589 // Create a stack slot that is 16-byte aligned.
5590 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00005591 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen08673d22010-05-03 22:59:34 +00005592 EVT PtrVT = getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00005593 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005594
Chris Lattner1a635d62006-04-14 06:01:58 +00005595 // Store the input value into Value#0 of the stack slot.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005596 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00005597 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00005598 false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00005599 // Load it out.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00005600 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005601 false, false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00005602}
5603
Dan Gohmand858e902010-04-17 15:26:15 +00005604SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005605 SDLoc dl(Op);
Owen Anderson825b72b2009-08-11 20:47:22 +00005606 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00005607 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005608
Owen Anderson825b72b2009-08-11 20:47:22 +00005609 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
5610 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelfdc40a02009-02-17 22:15:04 +00005611
Dan Gohman475871a2008-07-27 21:46:04 +00005612 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesened2eee62009-02-06 01:31:28 +00005613 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005614
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005615 // Shrinkify inputs to v8i16.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005616 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
5617 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
5618 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelfdc40a02009-02-17 22:15:04 +00005619
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005620 // Low parts multiplied together, generating 32-bit results (we ignore the
5621 // top parts).
Dan Gohman475871a2008-07-27 21:46:04 +00005622 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson825b72b2009-08-11 20:47:22 +00005623 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00005624
Dan Gohman475871a2008-07-27 21:46:04 +00005625 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson825b72b2009-08-11 20:47:22 +00005626 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005627 // Shift the high parts up 16 bits.
Scott Michelfdc40a02009-02-17 22:15:04 +00005628 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesened2eee62009-02-06 01:31:28 +00005629 Neg16, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00005630 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
5631 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005632 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005633
Owen Anderson825b72b2009-08-11 20:47:22 +00005634 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005635
Chris Lattnercea2aa72006-04-18 04:28:57 +00005636 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesened2eee62009-02-06 01:31:28 +00005637 LHS, RHS, Zero, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00005638 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005639 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005640
Chris Lattner19a81522006-04-18 03:57:35 +00005641 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00005642 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson825b72b2009-08-11 20:47:22 +00005643 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005644 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00005645
Chris Lattner19a81522006-04-18 03:57:35 +00005646 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00005647 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson825b72b2009-08-11 20:47:22 +00005648 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005649 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00005650
Chris Lattner19a81522006-04-18 03:57:35 +00005651 // Merge the results together.
Nate Begeman9008ca62009-04-27 18:41:29 +00005652 int Ops[16];
Chris Lattner19a81522006-04-18 03:57:35 +00005653 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005654 Ops[i*2 ] = 2*i+1;
5655 Ops[i*2+1] = 2*i+1+16;
Chris Lattner19a81522006-04-18 03:57:35 +00005656 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005657 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005658 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00005659 llvm_unreachable("Unknown mul to lower!");
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005660 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00005661}
5662
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00005663/// LowerOperation - Provide custom lowering hooks for some operations.
5664///
Dan Gohmand858e902010-04-17 15:26:15 +00005665SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00005666 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005667 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00005668 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson3d90dbe2009-11-04 21:31:18 +00005669 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00005670 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackyfd42ed62012-06-04 17:36:38 +00005671 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman37efe672006-04-22 18:53:45 +00005672 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00005673 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +00005674 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
5675 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005676 case ISD::VASTART:
Dan Gohman1e93df62010-04-17 14:41:14 +00005677 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelfdc40a02009-02-17 22:15:04 +00005678
5679 case ISD::VAARG:
Dan Gohman1e93df62010-04-17 14:41:14 +00005680 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray01119992007-04-03 13:59:52 +00005681
Jim Laskeyefc7e522006-12-04 22:04:42 +00005682 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner9f0bc652007-02-25 05:34:32 +00005683 case ISD::DYNAMIC_STACKALLOC:
5684 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng54fc97d2008-04-19 01:30:48 +00005685
Hal Finkel7ee74a62013-03-21 21:37:52 +00005686 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
5687 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
5688
Chris Lattner1a635d62006-04-14 06:01:58 +00005689 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen4c9369d2009-06-04 20:53:52 +00005690 case ISD::FP_TO_UINT:
5691 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005692 SDLoc(Op));
Hal Finkel46479192013-04-01 17:52:07 +00005693 case ISD::UINT_TO_FP:
5694 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00005695 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005696
Chris Lattner1a635d62006-04-14 06:01:58 +00005697 // Lower 64-bit shifts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00005698 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
5699 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
5700 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005701
Chris Lattner1a635d62006-04-14 06:01:58 +00005702 // Vector-related lowering.
5703 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5704 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5705 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5706 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00005707 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005708
Hal Finkelb1fd3cd2013-05-15 21:37:41 +00005709 // For counter-based loop handling.
5710 case ISD::INTRINSIC_W_CHAIN: return SDValue();
5711
Chris Lattner3fc027d2007-12-08 06:59:59 +00005712 // Frame & Return address.
5713 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005714 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00005715 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00005716}
5717
Duncan Sands1607f052008-12-01 11:39:25 +00005718void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
5719 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005720 SelectionDAG &DAG) const {
Roman Divackybdb226e2011-06-28 15:30:42 +00005721 const TargetMachine &TM = getTargetMachine();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005722 SDLoc dl(N);
Chris Lattner1f873002007-11-28 18:44:47 +00005723 switch (N->getOpcode()) {
Duncan Sands57760d92008-10-28 15:00:32 +00005724 default:
Craig Topperbc219812012-02-07 02:50:20 +00005725 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkelb1fd3cd2013-05-15 21:37:41 +00005726 case ISD::INTRINSIC_W_CHAIN: {
5727 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
5728 Intrinsic::ppc_is_decremented_ctr_nonzero)
5729 break;
5730
5731 assert(N->getValueType(0) == MVT::i1 &&
5732 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault225ed702013-05-18 00:21:46 +00005733 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkelb1fd3cd2013-05-15 21:37:41 +00005734 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
5735 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
5736 N->getOperand(1));
5737
5738 Results.push_back(NewInt);
5739 Results.push_back(NewInt.getValue(1));
5740 break;
5741 }
Roman Divackybdb226e2011-06-28 15:30:42 +00005742 case ISD::VAARG: {
5743 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
5744 || TM.getSubtarget<PPCSubtarget>().isPPC64())
5745 return;
5746
5747 EVT VT = N->getValueType(0);
5748
5749 if (VT == MVT::i64) {
5750 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
5751
5752 Results.push_back(NewNode);
5753 Results.push_back(NewNode.getValue(1));
5754 }
5755 return;
5756 }
Duncan Sands1607f052008-12-01 11:39:25 +00005757 case ISD::FP_ROUND_INREG: {
Owen Anderson825b72b2009-08-11 20:47:22 +00005758 assert(N->getValueType(0) == MVT::ppcf128);
5759 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelfdc40a02009-02-17 22:15:04 +00005760 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005761 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00005762 DAG.getIntPtrConstant(0));
Dale Johannesen3484c092009-02-05 22:07:54 +00005763 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005764 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00005765 DAG.getIntPtrConstant(1));
5766
Ulrich Weigand7d35d3f2013-03-26 10:56:22 +00005767 // Add the two halves of the long double in round-to-zero mode.
5768 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands1607f052008-12-01 11:39:25 +00005769
5770 // We know the low half is about to be thrown away, so just use something
5771 // convenient.
Owen Anderson825b72b2009-08-11 20:47:22 +00005772 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesen3484c092009-02-05 22:07:54 +00005773 FPreg, FPreg));
Duncan Sands1607f052008-12-01 11:39:25 +00005774 return;
Duncan Sandsa7360f02008-07-19 16:26:02 +00005775 }
Duncan Sands1607f052008-12-01 11:39:25 +00005776 case ISD::FP_TO_SINT:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00005777 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands1607f052008-12-01 11:39:25 +00005778 return;
Chris Lattner1f873002007-11-28 18:44:47 +00005779 }
5780}
5781
5782
Chris Lattner1a635d62006-04-14 06:01:58 +00005783//===----------------------------------------------------------------------===//
5784// Other Lowering Code
5785//===----------------------------------------------------------------------===//
5786
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00005787MachineBasicBlock *
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005788PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00005789 bool is64bit, unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00005790 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005791 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5792
5793 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5794 MachineFunction *F = BB->getParent();
5795 MachineFunction::iterator It = BB;
5796 ++It;
5797
5798 unsigned dest = MI->getOperand(0).getReg();
5799 unsigned ptrA = MI->getOperand(1).getReg();
5800 unsigned ptrB = MI->getOperand(2).getReg();
5801 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00005802 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005803
5804 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5805 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5806 F->insert(It, loopMBB);
5807 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005808 exitMBB->splice(exitMBB->begin(), BB,
5809 llvm::next(MachineBasicBlock::iterator(MI)),
5810 BB->end());
5811 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005812
5813 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesen0e55f062008-08-29 18:29:46 +00005814 unsigned TmpReg = (!BinOpcode) ? incr :
5815 RegInfo.createVirtualRegister(
Dale Johannesena619d012008-09-02 20:30:23 +00005816 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5817 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005818
5819 // thisMBB:
5820 // ...
5821 // fallthrough --> loopMBB
5822 BB->addSuccessor(loopMBB);
5823
5824 // loopMBB:
5825 // l[wd]arx dest, ptr
5826 // add r0, dest, incr
5827 // st[wd]cx. r0, ptr
5828 // bne- loopMBB
5829 // fallthrough --> exitMBB
5830 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005831 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005832 .addReg(ptrA).addReg(ptrB);
Dale Johannesen0e55f062008-08-29 18:29:46 +00005833 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00005834 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
5835 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005836 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005837 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00005838 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005839 BB->addSuccessor(loopMBB);
5840 BB->addSuccessor(exitMBB);
5841
5842 // exitMBB:
5843 // ...
5844 BB = exitMBB;
5845 return BB;
5846}
5847
5848MachineBasicBlock *
Scott Michelfdc40a02009-02-17 22:15:04 +00005849PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesen97efa362008-08-28 17:53:09 +00005850 MachineBasicBlock *BB,
5851 bool is8bit, // operation
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00005852 unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00005853 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesen97efa362008-08-28 17:53:09 +00005854 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5855 // In 64 bit mode we have to use 64 bits for addresses, even though the
5856 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
5857 // registers without caring whether they're 32 or 64, but here we're
5858 // doing actual arithmetic on the addresses.
5859 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkel76973702013-03-21 23:45:03 +00005860 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen97efa362008-08-28 17:53:09 +00005861
5862 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5863 MachineFunction *F = BB->getParent();
5864 MachineFunction::iterator It = BB;
5865 ++It;
5866
5867 unsigned dest = MI->getOperand(0).getReg();
5868 unsigned ptrA = MI->getOperand(1).getReg();
5869 unsigned ptrB = MI->getOperand(2).getReg();
5870 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00005871 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen97efa362008-08-28 17:53:09 +00005872
5873 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5874 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5875 F->insert(It, loopMBB);
5876 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005877 exitMBB->splice(exitMBB->begin(), BB,
5878 llvm::next(MachineBasicBlock::iterator(MI)),
5879 BB->end());
5880 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen97efa362008-08-28 17:53:09 +00005881
5882 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00005883 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00005884 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5885 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen97efa362008-08-28 17:53:09 +00005886 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
5887 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
5888 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
5889 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
5890 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
5891 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
5892 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
5893 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
5894 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
5895 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen0e55f062008-08-29 18:29:46 +00005896 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00005897 unsigned Ptr1Reg;
Dale Johannesen0e55f062008-08-29 18:29:46 +00005898 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00005899
5900 // thisMBB:
5901 // ...
5902 // fallthrough --> loopMBB
5903 BB->addSuccessor(loopMBB);
5904
5905 // The 4-byte load must be aligned, while a char or short may be
5906 // anywhere in the word. Hence all this nasty bookkeeping code.
5907 // add ptr1, ptrA, ptrB [copy if ptrA==0]
5908 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00005909 // xori shift, shift1, 24 [16]
Dale Johannesen97efa362008-08-28 17:53:09 +00005910 // rlwinm ptr, ptr1, 0, 0, 29
5911 // slw incr2, incr, shift
5912 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
5913 // slw mask, mask2, shift
5914 // loopMBB:
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005915 // lwarx tmpDest, ptr
Dale Johannesen0e55f062008-08-29 18:29:46 +00005916 // add tmp, tmpDest, incr2
5917 // andc tmp2, tmpDest, mask
Dale Johannesen97efa362008-08-28 17:53:09 +00005918 // and tmp3, tmp, mask
5919 // or tmp4, tmp3, tmp2
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005920 // stwcx. tmp4, ptr
Dale Johannesen97efa362008-08-28 17:53:09 +00005921 // bne- loopMBB
5922 // fallthrough --> exitMBB
Dale Johannesen0e55f062008-08-29 18:29:46 +00005923 // srw dest, tmpDest, shift
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00005924 if (ptrA != ZeroReg) {
Dale Johannesen97efa362008-08-28 17:53:09 +00005925 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005926 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005927 .addReg(ptrA).addReg(ptrB);
5928 } else {
5929 Ptr1Reg = ptrB;
5930 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00005931 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005932 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005933 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005934 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
5935 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00005936 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005937 .addReg(Ptr1Reg).addImm(0).addImm(61);
5938 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00005939 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005940 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005941 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005942 .addReg(incr).addReg(ShiftReg);
5943 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00005944 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen97efa362008-08-28 17:53:09 +00005945 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00005946 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
5947 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesen97efa362008-08-28 17:53:09 +00005948 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00005949 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005950 .addReg(Mask2Reg).addReg(ShiftReg);
5951
5952 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005953 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00005954 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen0e55f062008-08-29 18:29:46 +00005955 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00005956 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00005957 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005958 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00005959 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005960 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005961 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005962 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00005963 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidtdebf7d32013-04-02 18:37:08 +00005964 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00005965 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005966 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00005967 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesen97efa362008-08-28 17:53:09 +00005968 BB->addSuccessor(loopMBB);
5969 BB->addSuccessor(exitMBB);
5970
5971 // exitMBB:
5972 // ...
5973 BB = exitMBB;
Jakob Stoklund Olesen5fcb81d2011-04-04 17:57:29 +00005974 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
5975 .addReg(ShiftReg);
Dale Johannesen97efa362008-08-28 17:53:09 +00005976 return BB;
5977}
5978
Hal Finkel7ee74a62013-03-21 21:37:52 +00005979llvm::MachineBasicBlock*
5980PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
5981 MachineBasicBlock *MBB) const {
5982 DebugLoc DL = MI->getDebugLoc();
5983 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5984
5985 MachineFunction *MF = MBB->getParent();
5986 MachineRegisterInfo &MRI = MF->getRegInfo();
5987
5988 const BasicBlock *BB = MBB->getBasicBlock();
5989 MachineFunction::iterator I = MBB;
5990 ++I;
5991
5992 // Memory Reference
5993 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
5994 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
5995
5996 unsigned DstReg = MI->getOperand(0).getReg();
5997 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
5998 assert(RC->hasType(MVT::i32) && "Invalid destination!");
5999 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6000 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6001
6002 MVT PVT = getPointerTy();
6003 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6004 "Invalid Pointer Size!");
6005 // For v = setjmp(buf), we generate
6006 //
6007 // thisMBB:
6008 // SjLjSetup mainMBB
6009 // bl mainMBB
6010 // v_restore = 1
6011 // b sinkMBB
6012 //
6013 // mainMBB:
6014 // buf[LabelOffset] = LR
6015 // v_main = 0
6016 //
6017 // sinkMBB:
6018 // v = phi(main, restore)
6019 //
6020
6021 MachineBasicBlock *thisMBB = MBB;
6022 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6023 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6024 MF->insert(I, mainMBB);
6025 MF->insert(I, sinkMBB);
6026
6027 MachineInstrBuilder MIB;
6028
6029 // Transfer the remainder of BB and its successor edges to sinkMBB.
6030 sinkMBB->splice(sinkMBB->begin(), MBB,
6031 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
6032 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6033
6034 // Note that the structure of the jmp_buf used here is not compatible
6035 // with that used by libc, and is not designed to be. Specifically, it
6036 // stores only those 'reserved' registers that LLVM does not otherwise
6037 // understand how to spill. Also, by convention, by the time this
6038 // intrinsic is called, Clang has already stored the frame address in the
6039 // first slot of the buffer and stack address in the third. Following the
6040 // X86 target code, we'll store the jump address in the second slot. We also
6041 // need to save the TOC pointer (R2) to handle jumps between shared
6042 // libraries, and that will be stored in the fourth slot. The thread
6043 // identifier (R13) is not affected.
6044
6045 // thisMBB:
6046 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6047 const int64_t TOCOffset = 3 * PVT.getStoreSize();
6048
6049 // Prepare IP either in reg.
6050 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6051 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6052 unsigned BufReg = MI->getOperand(1).getReg();
6053
6054 if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) {
6055 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6056 .addReg(PPC::X2)
Ulrich Weigand347a5072013-05-16 17:58:02 +00006057 .addImm(TOCOffset)
Hal Finkel7ee74a62013-03-21 21:37:52 +00006058 .addReg(BufReg);
6059
6060 MIB.setMemRefs(MMOBegin, MMOEnd);
6061 }
6062
6063 // Setup
Hal Finkelcaeeb182013-04-04 22:55:54 +00006064 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling80ada582013-06-07 07:55:53 +00006065 const PPCRegisterInfo *TRI =
6066 static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo());
6067 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel7ee74a62013-03-21 21:37:52 +00006068
6069 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6070
6071 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6072 .addMBB(mainMBB);
6073 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6074
6075 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6076 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6077
6078 // mainMBB:
6079 // mainDstReg = 0
6080 MIB = BuildMI(mainMBB, DL,
6081 TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
6082
6083 // Store IP
6084 if (PPCSubTarget.isPPC64()) {
6085 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6086 .addReg(LabelReg)
Ulrich Weigand347a5072013-05-16 17:58:02 +00006087 .addImm(LabelOffset)
Hal Finkel7ee74a62013-03-21 21:37:52 +00006088 .addReg(BufReg);
6089 } else {
6090 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6091 .addReg(LabelReg)
6092 .addImm(LabelOffset)
6093 .addReg(BufReg);
6094 }
6095
6096 MIB.setMemRefs(MMOBegin, MMOEnd);
6097
6098 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6099 mainMBB->addSuccessor(sinkMBB);
6100
6101 // sinkMBB:
6102 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6103 TII->get(PPC::PHI), DstReg)
6104 .addReg(mainDstReg).addMBB(mainMBB)
6105 .addReg(restoreDstReg).addMBB(thisMBB);
6106
6107 MI->eraseFromParent();
6108 return sinkMBB;
6109}
6110
6111MachineBasicBlock *
6112PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6113 MachineBasicBlock *MBB) const {
6114 DebugLoc DL = MI->getDebugLoc();
6115 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6116
6117 MachineFunction *MF = MBB->getParent();
6118 MachineRegisterInfo &MRI = MF->getRegInfo();
6119
6120 // Memory Reference
6121 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6122 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6123
6124 MVT PVT = getPointerTy();
6125 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6126 "Invalid Pointer Size!");
6127
6128 const TargetRegisterClass *RC =
6129 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6130 unsigned Tmp = MRI.createVirtualRegister(RC);
6131 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6132 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6133 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
6134
6135 MachineInstrBuilder MIB;
6136
6137 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6138 const int64_t SPOffset = 2 * PVT.getStoreSize();
6139 const int64_t TOCOffset = 3 * PVT.getStoreSize();
6140
6141 unsigned BufReg = MI->getOperand(0).getReg();
6142
6143 // Reload FP (the jumped-to function may not have had a
6144 // frame pointer, and if so, then its r31 will be restored
6145 // as necessary).
6146 if (PVT == MVT::i64) {
6147 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6148 .addImm(0)
6149 .addReg(BufReg);
6150 } else {
6151 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6152 .addImm(0)
6153 .addReg(BufReg);
6154 }
6155 MIB.setMemRefs(MMOBegin, MMOEnd);
6156
6157 // Reload IP
6158 if (PVT == MVT::i64) {
6159 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand347a5072013-05-16 17:58:02 +00006160 .addImm(LabelOffset)
Hal Finkel7ee74a62013-03-21 21:37:52 +00006161 .addReg(BufReg);
6162 } else {
6163 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6164 .addImm(LabelOffset)
6165 .addReg(BufReg);
6166 }
6167 MIB.setMemRefs(MMOBegin, MMOEnd);
6168
6169 // Reload SP
6170 if (PVT == MVT::i64) {
6171 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand347a5072013-05-16 17:58:02 +00006172 .addImm(SPOffset)
Hal Finkel7ee74a62013-03-21 21:37:52 +00006173 .addReg(BufReg);
6174 } else {
6175 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6176 .addImm(SPOffset)
6177 .addReg(BufReg);
6178 }
6179 MIB.setMemRefs(MMOBegin, MMOEnd);
6180
6181 // FIXME: When we also support base pointers, that register must also be
6182 // restored here.
6183
6184 // Reload TOC
6185 if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) {
6186 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand347a5072013-05-16 17:58:02 +00006187 .addImm(TOCOffset)
Hal Finkel7ee74a62013-03-21 21:37:52 +00006188 .addReg(BufReg);
6189
6190 MIB.setMemRefs(MMOBegin, MMOEnd);
6191 }
6192
6193 // Jump
6194 BuildMI(*MBB, MI, DL,
6195 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6196 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6197
6198 MI->eraseFromParent();
6199 return MBB;
6200}
6201
Dale Johannesen97efa362008-08-28 17:53:09 +00006202MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006203PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006204 MachineBasicBlock *BB) const {
Hal Finkel7ee74a62013-03-21 21:37:52 +00006205 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6206 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6207 return emitEHSjLjSetJmp(MI, BB);
6208 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6209 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6210 return emitEHSjLjLongJmp(MI, BB);
6211 }
6212
Evan Chengc0f64ff2006-11-27 23:37:22 +00006213 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng53301922008-07-12 02:23:19 +00006214
6215 // To "insert" these instructions we actually have to insert their
6216 // control-flow patterns.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006217 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006218 MachineFunction::iterator It = BB;
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006219 ++It;
Evan Cheng53301922008-07-12 02:23:19 +00006220
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006221 MachineFunction *F = BB->getParent();
Evan Cheng53301922008-07-12 02:23:19 +00006222
Hal Finkel009f7af2012-06-22 23:10:08 +00006223 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6224 MI->getOpcode() == PPC::SELECT_CC_I8)) {
Hal Finkelff56d1a2013-04-05 23:29:01 +00006225 SmallVector<MachineOperand, 2> Cond;
6226 Cond.push_back(MI->getOperand(4));
6227 Cond.push_back(MI->getOperand(1));
6228
Hal Finkel009f7af2012-06-22 23:10:08 +00006229 DebugLoc dl = MI->getDebugLoc();
Bill Wendling80ada582013-06-07 07:55:53 +00006230 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6231 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
6232 Cond, MI->getOperand(2).getReg(),
6233 MI->getOperand(3).getReg());
Hal Finkel009f7af2012-06-22 23:10:08 +00006234 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6235 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6236 MI->getOpcode() == PPC::SELECT_CC_F4 ||
6237 MI->getOpcode() == PPC::SELECT_CC_F8 ||
6238 MI->getOpcode() == PPC::SELECT_CC_VRRC) {
6239
Evan Cheng53301922008-07-12 02:23:19 +00006240
6241 // The incoming instruction knows the destination vreg to set, the
6242 // condition code register to branch on, the true/false values to
6243 // select between, and a branch opcode to use.
6244
6245 // thisMBB:
6246 // ...
6247 // TrueVal = ...
6248 // cmpTY ccX, r1, r2
6249 // bCC copy1MBB
6250 // fallthrough --> copy0MBB
6251 MachineBasicBlock *thisMBB = BB;
6252 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6253 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
6254 unsigned SelectPred = MI->getOperand(4).getImm();
Dale Johannesen536a2f12009-02-13 02:27:39 +00006255 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00006256 F->insert(It, copy0MBB);
6257 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006258
6259 // Transfer the remainder of BB and its successor edges to sinkMBB.
6260 sinkMBB->splice(sinkMBB->begin(), BB,
6261 llvm::next(MachineBasicBlock::iterator(MI)),
6262 BB->end());
6263 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6264
Evan Cheng53301922008-07-12 02:23:19 +00006265 // Next, add the true and fallthrough blocks as its successors.
6266 BB->addSuccessor(copy0MBB);
6267 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006268
Dan Gohman14152b42010-07-06 20:24:04 +00006269 BuildMI(BB, dl, TII->get(PPC::BCC))
6270 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6271
Evan Cheng53301922008-07-12 02:23:19 +00006272 // copy0MBB:
6273 // %FalseValue = ...
6274 // # fallthrough to sinkMBB
6275 BB = copy0MBB;
Scott Michelfdc40a02009-02-17 22:15:04 +00006276
Evan Cheng53301922008-07-12 02:23:19 +00006277 // Update machine-CFG edges
6278 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006279
Evan Cheng53301922008-07-12 02:23:19 +00006280 // sinkMBB:
6281 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6282 // ...
6283 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006284 BuildMI(*BB, BB->begin(), dl,
6285 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng53301922008-07-12 02:23:19 +00006286 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
6287 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6288 }
Dale Johannesen97efa362008-08-28 17:53:09 +00006289 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
6290 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
6291 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
6292 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006293 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
6294 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
6295 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
6296 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006297
6298 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
6299 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
6300 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
6301 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006302 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
6303 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
6304 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
6305 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006306
6307 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
6308 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
6309 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
6310 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006311 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
6312 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
6313 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
6314 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006315
6316 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
6317 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
6318 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
6319 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006320 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
6321 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
6322 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
6323 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006324
6325 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesen209a4092008-09-11 02:15:03 +00006326 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesen97efa362008-08-28 17:53:09 +00006327 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesen209a4092008-09-11 02:15:03 +00006328 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006329 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesen209a4092008-09-11 02:15:03 +00006330 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006331 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesen209a4092008-09-11 02:15:03 +00006332 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006333
6334 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6335 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6336 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6337 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006338 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6339 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6340 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6341 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006342
Dale Johannesen0e55f062008-08-29 18:29:46 +00006343 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6344 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6345 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6346 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6347 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6348 BB = EmitAtomicBinary(MI, BB, false, 0);
6349 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6350 BB = EmitAtomicBinary(MI, BB, true, 0);
6351
Evan Cheng53301922008-07-12 02:23:19 +00006352 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6353 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6354 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6355
6356 unsigned dest = MI->getOperand(0).getReg();
6357 unsigned ptrA = MI->getOperand(1).getReg();
6358 unsigned ptrB = MI->getOperand(2).getReg();
6359 unsigned oldval = MI->getOperand(3).getReg();
6360 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00006361 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00006362
Dale Johannesen65e39732008-08-25 18:53:26 +00006363 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6364 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6365 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng53301922008-07-12 02:23:19 +00006366 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen65e39732008-08-25 18:53:26 +00006367 F->insert(It, loop1MBB);
6368 F->insert(It, loop2MBB);
6369 F->insert(It, midMBB);
Evan Cheng53301922008-07-12 02:23:19 +00006370 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006371 exitMBB->splice(exitMBB->begin(), BB,
6372 llvm::next(MachineBasicBlock::iterator(MI)),
6373 BB->end());
6374 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng53301922008-07-12 02:23:19 +00006375
6376 // thisMBB:
6377 // ...
6378 // fallthrough --> loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00006379 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00006380
Dale Johannesen65e39732008-08-25 18:53:26 +00006381 // loop1MBB:
Evan Cheng53301922008-07-12 02:23:19 +00006382 // l[wd]arx dest, ptr
Dale Johannesen65e39732008-08-25 18:53:26 +00006383 // cmp[wd] dest, oldval
6384 // bne- midMBB
6385 // loop2MBB:
Evan Cheng53301922008-07-12 02:23:19 +00006386 // st[wd]cx. newval, ptr
6387 // bne- loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00006388 // b exitBB
6389 // midMBB:
6390 // st[wd]cx. dest, ptr
6391 // exitBB:
6392 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006393 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng53301922008-07-12 02:23:19 +00006394 .addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006395 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng53301922008-07-12 02:23:19 +00006396 .addReg(oldval).addReg(dest);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006397 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00006398 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6399 BB->addSuccessor(loop2MBB);
6400 BB->addSuccessor(midMBB);
6401
6402 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006403 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng53301922008-07-12 02:23:19 +00006404 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006405 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00006406 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006407 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen65e39732008-08-25 18:53:26 +00006408 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00006409 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006410
Dale Johannesen65e39732008-08-25 18:53:26 +00006411 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006412 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen65e39732008-08-25 18:53:26 +00006413 .addReg(dest).addReg(ptrA).addReg(ptrB);
6414 BB->addSuccessor(exitMBB);
6415
Evan Cheng53301922008-07-12 02:23:19 +00006416 // exitMBB:
6417 // ...
6418 BB = exitMBB;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006419 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6420 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6421 // We must use 64-bit registers for addresses when targeting 64-bit,
6422 // since we're actually doing arithmetic on them. Other registers
6423 // can be 32-bit.
6424 bool is64bit = PPCSubTarget.isPPC64();
6425 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6426
6427 unsigned dest = MI->getOperand(0).getReg();
6428 unsigned ptrA = MI->getOperand(1).getReg();
6429 unsigned ptrB = MI->getOperand(2).getReg();
6430 unsigned oldval = MI->getOperand(3).getReg();
6431 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00006432 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006433
6434 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6435 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6436 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6437 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6438 F->insert(It, loop1MBB);
6439 F->insert(It, loop2MBB);
6440 F->insert(It, midMBB);
6441 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006442 exitMBB->splice(exitMBB->begin(), BB,
6443 llvm::next(MachineBasicBlock::iterator(MI)),
6444 BB->end());
6445 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006446
6447 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00006448 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00006449 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6450 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006451 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6452 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6453 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6454 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6455 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6456 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6457 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6458 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6459 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6460 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6461 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6462 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6463 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6464 unsigned Ptr1Reg;
6465 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkel76973702013-03-21 23:45:03 +00006466 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006467 // thisMBB:
6468 // ...
6469 // fallthrough --> loopMBB
6470 BB->addSuccessor(loop1MBB);
6471
6472 // The 4-byte load must be aligned, while a char or short may be
6473 // anywhere in the word. Hence all this nasty bookkeeping code.
6474 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6475 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00006476 // xori shift, shift1, 24 [16]
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006477 // rlwinm ptr, ptr1, 0, 0, 29
6478 // slw newval2, newval, shift
6479 // slw oldval2, oldval,shift
6480 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6481 // slw mask, mask2, shift
6482 // and newval3, newval2, mask
6483 // and oldval3, oldval2, mask
6484 // loop1MBB:
6485 // lwarx tmpDest, ptr
6486 // and tmp, tmpDest, mask
6487 // cmpw tmp, oldval3
6488 // bne- midMBB
6489 // loop2MBB:
6490 // andc tmp2, tmpDest, mask
6491 // or tmp4, tmp2, newval3
6492 // stwcx. tmp4, ptr
6493 // bne- loop1MBB
6494 // b exitBB
6495 // midMBB:
6496 // stwcx. tmpDest, ptr
6497 // exitBB:
6498 // srw dest, tmpDest, shift
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006499 if (ptrA != ZeroReg) {
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006500 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006501 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006502 .addReg(ptrA).addReg(ptrB);
6503 } else {
6504 Ptr1Reg = ptrB;
6505 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00006506 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006507 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006508 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006509 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6510 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00006511 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006512 .addReg(Ptr1Reg).addImm(0).addImm(61);
6513 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00006514 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006515 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006516 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006517 .addReg(newval).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006518 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006519 .addReg(oldval).addReg(ShiftReg);
6520 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00006521 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006522 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00006523 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6524 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6525 .addReg(Mask3Reg).addImm(65535);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006526 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00006527 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006528 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006529 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006530 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006531 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006532 .addReg(OldVal2Reg).addReg(MaskReg);
6533
6534 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006535 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006536 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006537 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6538 .addReg(TmpDestReg).addReg(MaskReg);
6539 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006540 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006541 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006542 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6543 BB->addSuccessor(loop2MBB);
6544 BB->addSuccessor(midMBB);
6545
6546 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006547 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6548 .addReg(TmpDestReg).addReg(MaskReg);
6549 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6550 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6551 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006552 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006553 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006554 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006555 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006556 BB->addSuccessor(loop1MBB);
6557 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006558
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006559 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006560 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006561 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006562 BB->addSuccessor(exitMBB);
6563
6564 // exitMBB:
6565 // ...
6566 BB = exitMBB;
Jakob Stoklund Olesen5fcb81d2011-04-04 17:57:29 +00006567 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
6568 .addReg(ShiftReg);
Ulrich Weigand7d35d3f2013-03-26 10:56:22 +00006569 } else if (MI->getOpcode() == PPC::FADDrtz) {
6570 // This pseudo performs an FADD with rounding mode temporarily forced
6571 // to round-to-zero. We emit this via custom inserter since the FPSCR
6572 // is not modeled at the SelectionDAG level.
6573 unsigned Dest = MI->getOperand(0).getReg();
6574 unsigned Src1 = MI->getOperand(1).getReg();
6575 unsigned Src2 = MI->getOperand(2).getReg();
6576 DebugLoc dl = MI->getDebugLoc();
6577
6578 MachineRegisterInfo &RegInfo = F->getRegInfo();
6579 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
6580
6581 // Save FPSCR value.
6582 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
6583
6584 // Set rounding mode to round-to-zero.
6585 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
6586 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
6587
6588 // Perform addition.
6589 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
6590
6591 // Restore FPSCR value.
6592 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkel0882fd62013-03-29 19:41:55 +00006593 } else if (MI->getOpcode() == PPC::FRINDrint ||
6594 MI->getOpcode() == PPC::FRINSrint) {
6595 bool isf32 = MI->getOpcode() == PPC::FRINSrint;
6596 unsigned Dest = MI->getOperand(0).getReg();
6597 unsigned Src = MI->getOperand(1).getReg();
6598 DebugLoc dl = MI->getDebugLoc();
6599
6600 MachineRegisterInfo &RegInfo = F->getRegInfo();
6601 unsigned CRReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
6602
6603 // Perform the rounding.
6604 BuildMI(*BB, MI, dl, TII->get(isf32 ? PPC::FRINS : PPC::FRIND), Dest)
6605 .addReg(Src);
6606
6607 // Compare the results.
6608 BuildMI(*BB, MI, dl, TII->get(isf32 ? PPC::FCMPUS : PPC::FCMPUD), CRReg)
6609 .addReg(Dest).addReg(Src);
6610
6611 // If the results were not equal, then set the FPSCR XX bit.
6612 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6613 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6614 F->insert(It, midMBB);
6615 F->insert(It, exitMBB);
6616 exitMBB->splice(exitMBB->begin(), BB,
6617 llvm::next(MachineBasicBlock::iterator(MI)),
6618 BB->end());
6619 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6620
6621 BuildMI(*BB, MI, dl, TII->get(PPC::BCC))
6622 .addImm(PPC::PRED_EQ).addReg(CRReg).addMBB(exitMBB);
6623
6624 BB->addSuccessor(midMBB);
6625 BB->addSuccessor(exitMBB);
6626
6627 BB = midMBB;
6628
6629 // Set the FPSCR XX bit (FE_INEXACT). Note that we cannot just set
6630 // the FI bit here because that will not automatically set XX also,
6631 // and XX is what libm interprets as the FE_INEXACT flag.
6632 BuildMI(BB, dl, TII->get(PPC::MTFSB1)).addImm(/* 38 - 32 = */ 6);
6633 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
6634
6635 BB->addSuccessor(exitMBB);
6636
6637 BB = exitMBB;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006638 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006639 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng53301922008-07-12 02:23:19 +00006640 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006641
Dan Gohman14152b42010-07-06 20:24:04 +00006642 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006643 return BB;
6644}
6645
Chris Lattner1a635d62006-04-14 06:01:58 +00006646//===----------------------------------------------------------------------===//
6647// Target Optimization Hooks
6648//===----------------------------------------------------------------------===//
6649
Hal Finkel63c32a72013-04-03 17:44:56 +00006650SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
6651 DAGCombinerInfo &DCI) const {
Hal Finkel827307b2013-04-03 04:01:11 +00006652 if (DCI.isAfterLegalizeVectorOps())
6653 return SDValue();
6654
Hal Finkel63c32a72013-04-03 17:44:56 +00006655 EVT VT = Op.getValueType();
6656
6657 if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) ||
6658 (VT == MVT::f64 && PPCSubTarget.hasFRE()) ||
6659 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel827307b2013-04-03 04:01:11 +00006660
6661 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6662 // For the reciprocal, we need to find the zero of the function:
6663 // F(X) = A X - 1 [which has a zero at X = 1/A]
6664 // =>
6665 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
6666 // does not require additional intermediate precision]
6667
6668 // Convergence is quadratic, so we essentially double the number of digits
6669 // correct after every iteration. The minimum architected relative
6670 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6671 // 23 digits and double has 52 digits.
6672 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkel63c32a72013-04-03 17:44:56 +00006673 if (VT.getScalarType() == MVT::f64)
Hal Finkel827307b2013-04-03 04:01:11 +00006674 ++Iterations;
6675
6676 SelectionDAG &DAG = DCI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006677 SDLoc dl(Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006678
6679 SDValue FPOne =
Hal Finkel63c32a72013-04-03 17:44:56 +00006680 DAG.getConstantFP(1.0, VT.getScalarType());
6681 if (VT.isVector()) {
6682 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel827307b2013-04-03 04:01:11 +00006683 "Unknown vector type");
Hal Finkel63c32a72013-04-03 17:44:56 +00006684 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel827307b2013-04-03 04:01:11 +00006685 FPOne, FPOne, FPOne, FPOne);
6686 }
6687
Hal Finkel63c32a72013-04-03 17:44:56 +00006688 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006689 DCI.AddToWorklist(Est.getNode());
6690
6691 // Newton iterations: Est = Est + Est (1 - Arg * Est)
6692 for (int i = 0; i < Iterations; ++i) {
Hal Finkel63c32a72013-04-03 17:44:56 +00006693 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel827307b2013-04-03 04:01:11 +00006694 DCI.AddToWorklist(NewEst.getNode());
6695
Hal Finkel63c32a72013-04-03 17:44:56 +00006696 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006697 DCI.AddToWorklist(NewEst.getNode());
6698
Hal Finkel63c32a72013-04-03 17:44:56 +00006699 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006700 DCI.AddToWorklist(NewEst.getNode());
6701
Hal Finkel63c32a72013-04-03 17:44:56 +00006702 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006703 DCI.AddToWorklist(Est.getNode());
6704 }
6705
6706 return Est;
6707 }
6708
6709 return SDValue();
6710}
6711
Hal Finkel63c32a72013-04-03 17:44:56 +00006712SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel827307b2013-04-03 04:01:11 +00006713 DAGCombinerInfo &DCI) const {
6714 if (DCI.isAfterLegalizeVectorOps())
6715 return SDValue();
6716
Hal Finkel63c32a72013-04-03 17:44:56 +00006717 EVT VT = Op.getValueType();
6718
6719 if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) ||
6720 (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) ||
6721 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel827307b2013-04-03 04:01:11 +00006722
6723 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6724 // For the reciprocal sqrt, we need to find the zero of the function:
6725 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
6726 // =>
6727 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
6728 // As a result, we precompute A/2 prior to the iteration loop.
6729
6730 // Convergence is quadratic, so we essentially double the number of digits
6731 // correct after every iteration. The minimum architected relative
6732 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6733 // 23 digits and double has 52 digits.
6734 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkel63c32a72013-04-03 17:44:56 +00006735 if (VT.getScalarType() == MVT::f64)
Hal Finkel827307b2013-04-03 04:01:11 +00006736 ++Iterations;
6737
6738 SelectionDAG &DAG = DCI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006739 SDLoc dl(Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006740
Hal Finkel63c32a72013-04-03 17:44:56 +00006741 SDValue FPThreeHalves =
6742 DAG.getConstantFP(1.5, VT.getScalarType());
6743 if (VT.isVector()) {
6744 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel827307b2013-04-03 04:01:11 +00006745 "Unknown vector type");
Hal Finkel63c32a72013-04-03 17:44:56 +00006746 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
6747 FPThreeHalves, FPThreeHalves,
6748 FPThreeHalves, FPThreeHalves);
Hal Finkel827307b2013-04-03 04:01:11 +00006749 }
6750
Hal Finkel63c32a72013-04-03 17:44:56 +00006751 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006752 DCI.AddToWorklist(Est.getNode());
6753
6754 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
6755 // this entire sequence requires only one FP constant.
Hal Finkel63c32a72013-04-03 17:44:56 +00006756 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006757 DCI.AddToWorklist(HalfArg.getNode());
6758
Hal Finkel63c32a72013-04-03 17:44:56 +00006759 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006760 DCI.AddToWorklist(HalfArg.getNode());
6761
6762 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
6763 for (int i = 0; i < Iterations; ++i) {
Hal Finkel63c32a72013-04-03 17:44:56 +00006764 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel827307b2013-04-03 04:01:11 +00006765 DCI.AddToWorklist(NewEst.getNode());
6766
Hal Finkel63c32a72013-04-03 17:44:56 +00006767 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006768 DCI.AddToWorklist(NewEst.getNode());
6769
Hal Finkel63c32a72013-04-03 17:44:56 +00006770 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006771 DCI.AddToWorklist(NewEst.getNode());
6772
Hal Finkel63c32a72013-04-03 17:44:56 +00006773 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006774 DCI.AddToWorklist(Est.getNode());
6775 }
6776
6777 return Est;
6778 }
6779
6780 return SDValue();
6781}
6782
Hal Finkel119da2e2013-05-27 02:06:39 +00006783// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
6784// not enforce equality of the chain operands.
6785static bool isConsecutiveLS(LSBaseSDNode *LS, LSBaseSDNode *Base,
6786 unsigned Bytes, int Dist,
6787 SelectionDAG &DAG) {
6788 EVT VT = LS->getMemoryVT();
6789 if (VT.getSizeInBits() / 8 != Bytes)
6790 return false;
6791
6792 SDValue Loc = LS->getBasePtr();
6793 SDValue BaseLoc = Base->getBasePtr();
6794 if (Loc.getOpcode() == ISD::FrameIndex) {
6795 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6796 return false;
6797 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6798 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6799 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6800 int FS = MFI->getObjectSize(FI);
6801 int BFS = MFI->getObjectSize(BFI);
6802 if (FS != BFS || FS != (int)Bytes) return false;
6803 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6804 }
6805
6806 // Handle X+C
6807 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6808 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6809 return true;
6810
6811 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6812 const GlobalValue *GV1 = NULL;
6813 const GlobalValue *GV2 = NULL;
6814 int64_t Offset1 = 0;
6815 int64_t Offset2 = 0;
6816 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6817 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6818 if (isGA1 && isGA2 && GV1 == GV2)
6819 return Offset1 == (Offset2 + Dist*Bytes);
6820 return false;
6821}
6822
Hal Finkel1907cad2013-05-26 18:08:30 +00006823// Return true is there is a nearyby consecutive load to the one provided
6824// (regardless of alignment). We search up and down the chain, looking though
6825// token factors and other loads (but nothing else). As a result, a true
6826// results indicates that it is safe to create a new consecutive load adjacent
6827// to the load provided.
6828static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
6829 SDValue Chain = LD->getChain();
6830 EVT VT = LD->getMemoryVT();
6831
6832 SmallSet<SDNode *, 16> LoadRoots;
6833 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
6834 SmallSet<SDNode *, 16> Visited;
6835
6836 // First, search up the chain, branching to follow all token-factor operands.
6837 // If we find a consecutive load, then we're done, otherwise, record all
6838 // nodes just above the top-level loads and token factors.
6839 while (!Queue.empty()) {
6840 SDNode *ChainNext = Queue.pop_back_val();
6841 if (!Visited.insert(ChainNext))
6842 continue;
6843
6844 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(ChainNext)) {
Hal Finkel119da2e2013-05-27 02:06:39 +00006845 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel1907cad2013-05-26 18:08:30 +00006846 return true;
6847
6848 if (!Visited.count(ChainLD->getChain().getNode()))
6849 Queue.push_back(ChainLD->getChain().getNode());
6850 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
6851 for (SDNode::op_iterator O = ChainNext->op_begin(),
6852 OE = ChainNext->op_end(); O != OE; ++O)
6853 if (!Visited.count(O->getNode()))
6854 Queue.push_back(O->getNode());
6855 } else
6856 LoadRoots.insert(ChainNext);
6857 }
6858
6859 // Second, search down the chain, starting from the top-level nodes recorded
6860 // in the first phase. These top-level nodes are the nodes just above all
6861 // loads and token factors. Starting with their uses, recursively look though
6862 // all loads (just the chain uses) and token factors to find a consecutive
6863 // load.
6864 Visited.clear();
6865 Queue.clear();
6866
6867 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
6868 IE = LoadRoots.end(); I != IE; ++I) {
6869 Queue.push_back(*I);
6870
6871 while (!Queue.empty()) {
6872 SDNode *LoadRoot = Queue.pop_back_val();
6873 if (!Visited.insert(LoadRoot))
6874 continue;
6875
6876 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(LoadRoot))
Hal Finkel119da2e2013-05-27 02:06:39 +00006877 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel1907cad2013-05-26 18:08:30 +00006878 return true;
6879
6880 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
6881 UE = LoadRoot->use_end(); UI != UE; ++UI)
6882 if (((isa<LoadSDNode>(*UI) &&
6883 cast<LoadSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
6884 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
6885 Queue.push_back(*UI);
6886 }
6887 }
6888
6889 return false;
6890}
6891
Duncan Sands25cf2272008-11-24 14:53:14 +00006892SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
6893 DAGCombinerInfo &DCI) const {
Dan Gohmanf0757b02010-04-21 01:34:56 +00006894 const TargetMachine &TM = getTargetMachine();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006895 SelectionDAG &DAG = DCI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006896 SDLoc dl(N);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006897 switch (N->getOpcode()) {
6898 default: break;
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006899 case PPCISD::SHL:
6900 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00006901 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006902 return N->getOperand(0);
6903 }
6904 break;
6905 case PPCISD::SRL:
6906 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00006907 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006908 return N->getOperand(0);
6909 }
6910 break;
6911 case PPCISD::SRA:
6912 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00006913 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006914 C->isAllOnesValue()) // -1 >>s V -> -1.
6915 return N->getOperand(0);
6916 }
6917 break;
Hal Finkel827307b2013-04-03 04:01:11 +00006918 case ISD::FDIV: {
6919 assert(TM.Options.UnsafeFPMath &&
6920 "Reciprocal estimates require UnsafeFPMath");
Scott Michelfdc40a02009-02-17 22:15:04 +00006921
Hal Finkel827307b2013-04-03 04:01:11 +00006922 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkel63c32a72013-04-03 17:44:56 +00006923 SDValue RV =
6924 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Hal Finkel827307b2013-04-03 04:01:11 +00006925 if (RV.getNode() != 0) {
6926 DCI.AddToWorklist(RV.getNode());
6927 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
6928 N->getOperand(0), RV);
6929 }
Hal Finkel7530a9f2013-04-04 22:44:12 +00006930 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
6931 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
6932 SDValue RV =
6933 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
6934 DCI);
6935 if (RV.getNode() != 0) {
6936 DCI.AddToWorklist(RV.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006937 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)),
Hal Finkel7530a9f2013-04-04 22:44:12 +00006938 N->getValueType(0), RV);
6939 DCI.AddToWorklist(RV.getNode());
6940 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
6941 N->getOperand(0), RV);
6942 }
6943 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
6944 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
6945 SDValue RV =
6946 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
6947 DCI);
6948 if (RV.getNode() != 0) {
6949 DCI.AddToWorklist(RV.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006950 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)),
Hal Finkel7530a9f2013-04-04 22:44:12 +00006951 N->getValueType(0), RV,
6952 N->getOperand(1).getOperand(1));
6953 DCI.AddToWorklist(RV.getNode());
6954 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
6955 N->getOperand(0), RV);
6956 }
Hal Finkel827307b2013-04-03 04:01:11 +00006957 }
6958
Hal Finkel63c32a72013-04-03 17:44:56 +00006959 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Hal Finkel827307b2013-04-03 04:01:11 +00006960 if (RV.getNode() != 0) {
6961 DCI.AddToWorklist(RV.getNode());
6962 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
6963 N->getOperand(0), RV);
6964 }
6965
6966 }
6967 break;
6968 case ISD::FSQRT: {
6969 assert(TM.Options.UnsafeFPMath &&
6970 "Reciprocal estimates require UnsafeFPMath");
6971
6972 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
6973 // reciprocal sqrt.
Hal Finkel63c32a72013-04-03 17:44:56 +00006974 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Hal Finkel827307b2013-04-03 04:01:11 +00006975 if (RV.getNode() != 0) {
6976 DCI.AddToWorklist(RV.getNode());
Hal Finkel63c32a72013-04-03 17:44:56 +00006977 RV = DAGCombineFastRecip(RV, DCI);
Hal Finkel827307b2013-04-03 04:01:11 +00006978 if (RV.getNode() != 0)
6979 return RV;
6980 }
6981
6982 }
6983 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006984 case ISD::SINT_TO_FP:
Chris Lattnera7a58542006-06-16 17:34:12 +00006985 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00006986 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
6987 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
6988 // We allow the src/dst to be either f32/f64, but the intermediate
6989 // type must be i64.
Owen Anderson825b72b2009-08-11 20:47:22 +00006990 if (N->getOperand(0).getValueType() == MVT::i64 &&
6991 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00006992 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006993 if (Val.getValueType() == MVT::f32) {
6994 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00006995 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00006996 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006997
Owen Anderson825b72b2009-08-11 20:47:22 +00006998 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00006999 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00007000 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00007001 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00007002 if (N->getValueType(0) == MVT::f32) {
7003 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner0bd48932008-01-17 07:00:52 +00007004 DAG.getIntPtrConstant(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00007005 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00007006 }
7007 return Val;
Owen Anderson825b72b2009-08-11 20:47:22 +00007008 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00007009 // If the intermediate type is i32, we can avoid the load/store here
7010 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00007011 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00007012 }
7013 }
7014 break;
Chris Lattner51269842006-03-01 05:50:56 +00007015 case ISD::STORE:
7016 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
7017 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnera7a02fb2008-01-18 16:54:56 +00007018 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner51269842006-03-01 05:50:56 +00007019 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007020 N->getOperand(1).getValueType() == MVT::i32 &&
7021 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00007022 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007023 if (Val.getValueType() == MVT::f32) {
7024 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00007025 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00007026 }
Owen Anderson825b72b2009-08-11 20:47:22 +00007027 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00007028 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00007029
Hal Finkelf170cc92013-04-01 15:37:53 +00007030 SDValue Ops[] = {
7031 N->getOperand(0), Val, N->getOperand(2),
7032 DAG.getValueType(N->getOperand(1).getValueType())
7033 };
7034
7035 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
7036 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
7037 cast<StoreSDNode>(N)->getMemoryVT(),
7038 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greifba36cb52008-08-28 21:40:38 +00007039 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00007040 return Val;
7041 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007042
Chris Lattnerd9989382006-07-10 20:56:58 +00007043 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman6acaaa82009-09-25 00:57:30 +00007044 if (cast<StoreSDNode>(N)->isUnindexed() &&
7045 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00007046 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007047 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkelefdd4672013-03-28 19:25:55 +00007048 N->getOperand(1).getValueType() == MVT::i16 ||
7049 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel2544f222013-03-28 20:23:46 +00007050 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkelefdd4672013-03-28 19:25:55 +00007051 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman475871a2008-07-27 21:46:04 +00007052 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnerd9989382006-07-10 20:56:58 +00007053 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson825b72b2009-08-11 20:47:22 +00007054 if (BSwapOp.getValueType() == MVT::i16)
7055 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnerd9989382006-07-10 20:56:58 +00007056
Dan Gohmanc76909a2009-09-25 20:36:54 +00007057 SDValue Ops[] = {
7058 N->getOperand(0), BSwapOp, N->getOperand(2),
7059 DAG.getValueType(N->getOperand(1).getValueType())
7060 };
7061 return
7062 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
7063 Ops, array_lengthof(Ops),
7064 cast<StoreSDNode>(N)->getMemoryVT(),
7065 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnerd9989382006-07-10 20:56:58 +00007066 }
7067 break;
Hal Finkel80d10de2013-05-24 23:00:14 +00007068 case ISD::LOAD: {
7069 LoadSDNode *LD = cast<LoadSDNode>(N);
7070 EVT VT = LD->getValueType(0);
7071 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
7072 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
7073 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
7074 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
7075 DCI.getDAGCombineLevel() == AfterLegalizeTypes &&
7076 LD->getAlignment() < ABIAlignment) {
7077 // This is a type-legal unaligned Altivec load.
7078 SDValue Chain = LD->getChain();
7079 SDValue Ptr = LD->getBasePtr();
7080
7081 // This implements the loading of unaligned vectors as described in
7082 // the venerable Apple Velocity Engine overview. Specifically:
7083 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
7084 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
7085 //
7086 // The general idea is to expand a sequence of one or more unaligned
7087 // loads into a alignment-based permutation-control instruction (lvsl),
7088 // a series of regular vector loads (which always truncate their
7089 // input address to an aligned address), and a series of permutations.
7090 // The results of these permutations are the requested loaded values.
7091 // The trick is that the last "extra" load is not taken from the address
7092 // you might suspect (sizeof(vector) bytes after the last requested
7093 // load), but rather sizeof(vector) - 1 bytes after the last
7094 // requested vector. The point of this is to avoid a page fault if the
7095 // base address happend to be aligned. This works because if the base
7096 // address is aligned, then adding less than a full vector length will
7097 // cause the last vector in the sequence to be (re)loaded. Otherwise,
7098 // the next vector will be fetched as you might suspect was necessary.
7099
Hal Finkel5a0e6042013-05-25 04:05:05 +00007100 // We might be able to reuse the permutation generation from
Hal Finkel80d10de2013-05-24 23:00:14 +00007101 // a different base address offset from this one by an aligned amount.
Hal Finkel5a0e6042013-05-25 04:05:05 +00007102 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
7103 // optimization later.
Hal Finkel80d10de2013-05-24 23:00:14 +00007104 SDValue PermCntl = BuildIntrinsicOp(Intrinsic::ppc_altivec_lvsl, Ptr,
7105 DAG, dl, MVT::v16i8);
7106
7107 // Refine the alignment of the original load (a "new" load created here
7108 // which was identical to the first except for the alignment would be
7109 // merged with the existing node regardless).
7110 MachineFunction &MF = DAG.getMachineFunction();
7111 MachineMemOperand *MMO =
7112 MF.getMachineMemOperand(LD->getPointerInfo(),
7113 LD->getMemOperand()->getFlags(),
7114 LD->getMemoryVT().getStoreSize(),
7115 ABIAlignment);
7116 LD->refineAlignment(MMO);
7117 SDValue BaseLoad = SDValue(LD, 0);
7118
7119 // Note that the value of IncOffset (which is provided to the next
7120 // load's pointer info offset value, and thus used to calculate the
7121 // alignment), and the value of IncValue (which is actually used to
7122 // increment the pointer value) are different! This is because we
7123 // require the next load to appear to be aligned, even though it
7124 // is actually offset from the base pointer by a lesser amount.
7125 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel1907cad2013-05-26 18:08:30 +00007126 int IncValue = IncOffset;
7127
7128 // Walk (both up and down) the chain looking for another load at the real
7129 // (aligned) offset (the alignment of the other load does not matter in
7130 // this case). If found, then do not use the offset reduction trick, as
7131 // that will prevent the loads from being later combined (as they would
7132 // otherwise be duplicates).
7133 if (!findConsecutiveLoad(LD, DAG))
7134 --IncValue;
7135
Hal Finkel80d10de2013-05-24 23:00:14 +00007136 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
7137 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
7138
Hal Finkel80d10de2013-05-24 23:00:14 +00007139 SDValue ExtraLoad =
7140 DAG.getLoad(VT, dl, Chain, Ptr,
7141 LD->getPointerInfo().getWithOffset(IncOffset),
7142 LD->isVolatile(), LD->isNonTemporal(),
7143 LD->isInvariant(), ABIAlignment);
7144
7145 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7146 BaseLoad.getValue(1), ExtraLoad.getValue(1));
7147
7148 if (BaseLoad.getValueType() != MVT::v4i32)
7149 BaseLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, BaseLoad);
7150
7151 if (ExtraLoad.getValueType() != MVT::v4i32)
7152 ExtraLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ExtraLoad);
7153
7154 SDValue Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
7155 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
7156
7157 if (VT != MVT::v4i32)
7158 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
7159
7160 // Now we need to be really careful about how we update the users of the
7161 // original load. We cannot just call DCI.CombineTo (or
7162 // DAG.ReplaceAllUsesWith for that matter), because the load still has
7163 // uses created here (the permutation for example) that need to stay.
7164 SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
7165 while (UI != UE) {
7166 SDUse &Use = UI.getUse();
7167 SDNode *User = *UI;
7168 // Note: BaseLoad is checked here because it might not be N, but a
7169 // bitcast of N.
7170 if (User == Perm.getNode() || User == BaseLoad.getNode() ||
7171 User == TF.getNode() || Use.getResNo() > 1) {
7172 ++UI;
7173 continue;
7174 }
7175
7176 SDValue To = Use.getResNo() ? TF : Perm;
7177 ++UI;
7178
7179 SmallVector<SDValue, 8> Ops;
7180 for (SDNode::op_iterator O = User->op_begin(),
7181 OE = User->op_end(); O != OE; ++O) {
7182 if (*O == Use)
7183 Ops.push_back(To);
7184 else
7185 Ops.push_back(*O);
7186 }
7187
7188 DAG.UpdateNodeOperands(User, Ops.data(), Ops.size());
7189 }
7190
7191 return SDValue(N, 0);
7192 }
7193 }
7194 break;
Hal Finkel5a0e6042013-05-25 04:05:05 +00007195 case ISD::INTRINSIC_WO_CHAIN:
7196 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() ==
7197 Intrinsic::ppc_altivec_lvsl &&
7198 N->getOperand(1)->getOpcode() == ISD::ADD) {
7199 SDValue Add = N->getOperand(1);
7200
7201 if (DAG.MaskedValueIsZero(Add->getOperand(1),
7202 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
7203 Add.getValueType().getScalarType().getSizeInBits()))) {
7204 SDNode *BasePtr = Add->getOperand(0).getNode();
7205 for (SDNode::use_iterator UI = BasePtr->use_begin(),
7206 UE = BasePtr->use_end(); UI != UE; ++UI) {
7207 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
7208 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
7209 Intrinsic::ppc_altivec_lvsl) {
7210 // We've found another LVSL, and this address if an aligned
7211 // multiple of that one. The results will be the same, so use the
7212 // one we've just found instead.
7213
7214 return SDValue(*UI, 0);
7215 }
7216 }
7217 }
7218 }
Chris Lattnerd9989382006-07-10 20:56:58 +00007219 case ISD::BSWAP:
7220 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greifba36cb52008-08-28 21:40:38 +00007221 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnerd9989382006-07-10 20:56:58 +00007222 N->getOperand(0).hasOneUse() &&
Hal Finkelefdd4672013-03-28 19:25:55 +00007223 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
7224 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel2544f222013-03-28 20:23:46 +00007225 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkelefdd4672013-03-28 19:25:55 +00007226 N->getValueType(0) == MVT::i64))) {
Dan Gohman475871a2008-07-27 21:46:04 +00007227 SDValue Load = N->getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +00007228 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnerd9989382006-07-10 20:56:58 +00007229 // Create the byte-swapping load.
Dan Gohman475871a2008-07-27 21:46:04 +00007230 SDValue Ops[] = {
Evan Cheng466685d2006-10-09 20:57:25 +00007231 LD->getChain(), // Chain
7232 LD->getBasePtr(), // Ptr
Chris Lattner79e490a2006-08-11 17:18:05 +00007233 DAG.getValueType(N->getValueType(0)) // VT
7234 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00007235 SDValue BSLoad =
7236 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkelefdd4672013-03-28 19:25:55 +00007237 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
7238 MVT::i64 : MVT::i32, MVT::Other),
Hal Finkelb52980b2013-03-28 19:43:12 +00007239 Ops, 3, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnerd9989382006-07-10 20:56:58 +00007240
Scott Michelfdc40a02009-02-17 22:15:04 +00007241 // If this is an i16 load, insert the truncate.
Dan Gohman475871a2008-07-27 21:46:04 +00007242 SDValue ResVal = BSLoad;
Owen Anderson825b72b2009-08-11 20:47:22 +00007243 if (N->getValueType(0) == MVT::i16)
7244 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelfdc40a02009-02-17 22:15:04 +00007245
Chris Lattnerd9989382006-07-10 20:56:58 +00007246 // First, combine the bswap away. This makes the value produced by the
7247 // load dead.
7248 DCI.CombineTo(N, ResVal);
7249
7250 // Next, combine the load away, we give it a bogus result value but a real
7251 // chain result. The result value is dead because the bswap is dead.
Gabor Greifba36cb52008-08-28 21:40:38 +00007252 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00007253
Chris Lattnerd9989382006-07-10 20:56:58 +00007254 // Return N so it doesn't get rechecked!
Dan Gohman475871a2008-07-27 21:46:04 +00007255 return SDValue(N, 0);
Chris Lattnerd9989382006-07-10 20:56:58 +00007256 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007257
Chris Lattner51269842006-03-01 05:50:56 +00007258 break;
Chris Lattner4468c222006-03-31 06:02:07 +00007259 case PPCISD::VCMP: {
7260 // If a VCMPo node already exists with exactly the same operands as this
7261 // node, use its result instead of this node (VCMPo computes both a CR6 and
7262 // a normal output).
7263 //
7264 if (!N->getOperand(0).hasOneUse() &&
7265 !N->getOperand(1).hasOneUse() &&
7266 !N->getOperand(2).hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00007267
Chris Lattner4468c222006-03-31 06:02:07 +00007268 // Scan all of the users of the LHS, looking for VCMPo's that match.
7269 SDNode *VCMPoNode = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00007270
Gabor Greifba36cb52008-08-28 21:40:38 +00007271 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattner4468c222006-03-31 06:02:07 +00007272 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
7273 UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00007274 if (UI->getOpcode() == PPCISD::VCMPo &&
7275 UI->getOperand(1) == N->getOperand(1) &&
7276 UI->getOperand(2) == N->getOperand(2) &&
7277 UI->getOperand(0) == N->getOperand(0)) {
7278 VCMPoNode = *UI;
Chris Lattner4468c222006-03-31 06:02:07 +00007279 break;
7280 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007281
Chris Lattner00901202006-04-18 18:28:22 +00007282 // If there is no VCMPo node, or if the flag value has a single use, don't
7283 // transform this.
7284 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
7285 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007286
7287 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner00901202006-04-18 18:28:22 +00007288 // chain, this transformation is more complex. Note that multiple things
7289 // could use the value result, which we should ignore.
7290 SDNode *FlagUser = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00007291 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner00901202006-04-18 18:28:22 +00007292 FlagUser == 0; ++UI) {
7293 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman89684502008-07-27 20:43:25 +00007294 SDNode *User = *UI;
Chris Lattner00901202006-04-18 18:28:22 +00007295 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007296 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner00901202006-04-18 18:28:22 +00007297 FlagUser = User;
7298 break;
7299 }
7300 }
7301 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007302
Ulrich Weigand965b20e2013-07-03 17:05:42 +00007303 // If the user is a MFOCRF instruction, we know this is safe.
7304 // Otherwise we give up for right now.
7305 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman475871a2008-07-27 21:46:04 +00007306 return SDValue(VCMPoNode, 0);
Chris Lattner4468c222006-03-31 06:02:07 +00007307 }
7308 break;
7309 }
Chris Lattner90564f22006-04-18 17:59:36 +00007310 case ISD::BR_CC: {
7311 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigand965b20e2013-07-03 17:05:42 +00007312 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner90564f22006-04-18 17:59:36 +00007313 // lowering is done pre-legalize, because the legalizer lowers the predicate
7314 // compare down to code that is difficult to reassemble.
7315 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00007316 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkelb1fd3cd2013-05-15 21:37:41 +00007317
7318 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
7319 // value. If so, pass-through the AND to get to the intrinsic.
7320 if (LHS.getOpcode() == ISD::AND &&
7321 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
7322 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
7323 Intrinsic::ppc_is_decremented_ctr_nonzero &&
7324 isa<ConstantSDNode>(LHS.getOperand(1)) &&
7325 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
7326 isZero())
7327 LHS = LHS.getOperand(0);
7328
7329 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
7330 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
7331 Intrinsic::ppc_is_decremented_ctr_nonzero &&
7332 isa<ConstantSDNode>(RHS)) {
7333 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
7334 "Counter decrement comparison is not EQ or NE");
7335
7336 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
7337 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
7338 (CC == ISD::SETNE && !Val);
7339
7340 // We now need to make the intrinsic dead (it cannot be instruction
7341 // selected).
7342 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
7343 assert(LHS.getNode()->hasOneUse() &&
7344 "Counter decrement has more than one use");
7345
7346 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
7347 N->getOperand(0), N->getOperand(4));
7348 }
7349
Chris Lattner90564f22006-04-18 17:59:36 +00007350 int CompareOpc;
7351 bool isDot;
Scott Michelfdc40a02009-02-17 22:15:04 +00007352
Chris Lattner90564f22006-04-18 17:59:36 +00007353 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
7354 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
7355 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
7356 assert(isDot && "Can't compare against a vector result!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007357
Chris Lattner90564f22006-04-18 17:59:36 +00007358 // If this is a comparison against something other than 0/1, then we know
7359 // that the condition is never/always true.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007360 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00007361 if (Val != 0 && Val != 1) {
7362 if (CC == ISD::SETEQ) // Cond never true, remove branch.
7363 return N->getOperand(0);
7364 // Always !=, turn it into an unconditional branch.
Owen Anderson825b72b2009-08-11 20:47:22 +00007365 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner90564f22006-04-18 17:59:36 +00007366 N->getOperand(0), N->getOperand(4));
7367 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007368
Chris Lattner90564f22006-04-18 17:59:36 +00007369 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00007370
Chris Lattner90564f22006-04-18 17:59:36 +00007371 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman475871a2008-07-27 21:46:04 +00007372 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00007373 LHS.getOperand(2), // LHS of compare
7374 LHS.getOperand(3), // RHS of compare
Owen Anderson825b72b2009-08-11 20:47:22 +00007375 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00007376 };
Benjamin Kramer3853f742013-03-07 20:33:29 +00007377 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesen3484c092009-02-05 22:07:54 +00007378 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00007379
Chris Lattner90564f22006-04-18 17:59:36 +00007380 // Unpack the result based on how the target uses it.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007381 PPC::Predicate CompOpc;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007382 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner90564f22006-04-18 17:59:36 +00007383 default: // Can't happen, don't crash on invalid number though.
7384 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007385 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner90564f22006-04-18 17:59:36 +00007386 break;
7387 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007388 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner90564f22006-04-18 17:59:36 +00007389 break;
7390 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007391 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner90564f22006-04-18 17:59:36 +00007392 break;
7393 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007394 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner90564f22006-04-18 17:59:36 +00007395 break;
7396 }
7397
Owen Anderson825b72b2009-08-11 20:47:22 +00007398 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
7399 DAG.getConstant(CompOpc, MVT::i32),
7400 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner90564f22006-04-18 17:59:36 +00007401 N->getOperand(4), CompNode.getValue(1));
7402 }
7403 break;
7404 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00007405 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007406
Dan Gohman475871a2008-07-27 21:46:04 +00007407 return SDValue();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00007408}
7409
Chris Lattner1a635d62006-04-14 06:01:58 +00007410//===----------------------------------------------------------------------===//
7411// Inline Assembly Support
7412//===----------------------------------------------------------------------===//
7413
Dan Gohman475871a2008-07-27 21:46:04 +00007414void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Scott Michelfdc40a02009-02-17 22:15:04 +00007415 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007416 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007417 const SelectionDAG &DAG,
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007418 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00007419 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007420 switch (Op.getOpcode()) {
7421 default: break;
Chris Lattnerd9989382006-07-10 20:56:58 +00007422 case PPCISD::LBRX: {
7423 // lhbrx is known to have the top bits cleared out.
Dan Gohmanae03af22009-09-27 23:17:47 +00007424 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnerd9989382006-07-10 20:56:58 +00007425 KnownZero = 0xFFFF0000;
7426 break;
7427 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007428 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007429 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007430 default: break;
7431 case Intrinsic::ppc_altivec_vcmpbfp_p:
7432 case Intrinsic::ppc_altivec_vcmpeqfp_p:
7433 case Intrinsic::ppc_altivec_vcmpequb_p:
7434 case Intrinsic::ppc_altivec_vcmpequh_p:
7435 case Intrinsic::ppc_altivec_vcmpequw_p:
7436 case Intrinsic::ppc_altivec_vcmpgefp_p:
7437 case Intrinsic::ppc_altivec_vcmpgtfp_p:
7438 case Intrinsic::ppc_altivec_vcmpgtsb_p:
7439 case Intrinsic::ppc_altivec_vcmpgtsh_p:
7440 case Intrinsic::ppc_altivec_vcmpgtsw_p:
7441 case Intrinsic::ppc_altivec_vcmpgtub_p:
7442 case Intrinsic::ppc_altivec_vcmpgtuh_p:
7443 case Intrinsic::ppc_altivec_vcmpgtuw_p:
7444 KnownZero = ~1U; // All bits but the low one are known to be zero.
7445 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007446 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007447 }
7448 }
7449}
7450
7451
Chris Lattner4234f572007-03-25 02:14:49 +00007452/// getConstraintType - Given a constraint, return the type of
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00007453/// constraint it is for this target.
Scott Michelfdc40a02009-02-17 22:15:04 +00007454PPCTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007455PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
7456 if (Constraint.size() == 1) {
7457 switch (Constraint[0]) {
7458 default: break;
7459 case 'b':
7460 case 'r':
7461 case 'f':
7462 case 'v':
7463 case 'y':
7464 return C_RegisterClass;
Hal Finkel827b7a02012-11-05 18:18:42 +00007465 case 'Z':
7466 // FIXME: While Z does indicate a memory constraint, it specifically
7467 // indicates an r+r address (used in conjunction with the 'y' modifier
7468 // in the replacement string). Currently, we're forcing the base
7469 // register to be r0 in the asm printer (which is interpreted as zero)
7470 // and forming the complete address in the second register. This is
7471 // suboptimal.
7472 return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00007473 }
7474 }
7475 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00007476}
7477
John Thompson44ab89e2010-10-29 17:29:13 +00007478/// Examine constraint type and operand type and determine a weight value.
7479/// This object must already have been set up with the operand type
7480/// and the current alternative constraint selected.
7481TargetLowering::ConstraintWeight
7482PPCTargetLowering::getSingleConstraintMatchWeight(
7483 AsmOperandInfo &info, const char *constraint) const {
7484 ConstraintWeight weight = CW_Invalid;
7485 Value *CallOperandVal = info.CallOperandVal;
7486 // If we don't have a value, we can't do a match,
7487 // but allow it at the lowest weight.
7488 if (CallOperandVal == NULL)
7489 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007490 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00007491 // Look at the constraint type.
7492 switch (*constraint) {
7493 default:
7494 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7495 break;
7496 case 'b':
7497 if (type->isIntegerTy())
7498 weight = CW_Register;
7499 break;
7500 case 'f':
7501 if (type->isFloatTy())
7502 weight = CW_Register;
7503 break;
7504 case 'd':
7505 if (type->isDoubleTy())
7506 weight = CW_Register;
7507 break;
7508 case 'v':
7509 if (type->isVectorTy())
7510 weight = CW_Register;
7511 break;
7512 case 'y':
7513 weight = CW_Register;
7514 break;
Hal Finkel827b7a02012-11-05 18:18:42 +00007515 case 'Z':
7516 weight = CW_Memory;
7517 break;
John Thompson44ab89e2010-10-29 17:29:13 +00007518 }
7519 return weight;
7520}
7521
Scott Michelfdc40a02009-02-17 22:15:04 +00007522std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner331d1bc2006-11-02 01:44:04 +00007523PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier5b3fca52013-06-22 18:37:38 +00007524 MVT VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00007525 if (Constraint.size() == 1) {
Chris Lattner331d1bc2006-11-02 01:44:04 +00007526 // GCC RS6000 Constraint Letters
7527 switch (Constraint[0]) {
7528 case 'b': // R1-R31
Hal Finkela548afc2013-03-19 18:51:05 +00007529 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
7530 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
7531 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00007532 case 'r': // R0-R31
Owen Anderson825b72b2009-08-11 20:47:22 +00007533 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperc9099502012-04-20 06:31:50 +00007534 return std::make_pair(0U, &PPC::G8RCRegClass);
7535 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00007536 case 'f':
Ulrich Weigand78dab642012-10-29 17:49:34 +00007537 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00007538 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand78dab642012-10-29 17:49:34 +00007539 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00007540 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00007541 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007542 case 'v':
Craig Topperc9099502012-04-20 06:31:50 +00007543 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00007544 case 'y': // crrc
Craig Topperc9099502012-04-20 06:31:50 +00007545 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattnerddc787d2006-01-31 19:20:21 +00007546 }
7547 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007548
Chris Lattner331d1bc2006-11-02 01:44:04 +00007549 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerddc787d2006-01-31 19:20:21 +00007550}
Chris Lattner763317d2006-02-07 00:47:13 +00007551
Chris Lattner331d1bc2006-11-02 01:44:04 +00007552
Chris Lattner48884cd2007-08-25 00:47:38 +00007553/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesen1784d162010-06-25 21:55:36 +00007554/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher471e4222011-06-08 23:55:35 +00007555void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007556 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +00007557 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00007558 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007559 SDValue Result(0,0);
Eric Christopher471e4222011-06-08 23:55:35 +00007560
Eric Christopher100c8332011-06-02 23:16:42 +00007561 // Only support length 1 constraints.
7562 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007563
Eric Christopher100c8332011-06-02 23:16:42 +00007564 char Letter = Constraint[0];
Chris Lattner763317d2006-02-07 00:47:13 +00007565 switch (Letter) {
7566 default: break;
7567 case 'I':
7568 case 'J':
7569 case 'K':
7570 case 'L':
7571 case 'M':
7572 case 'N':
7573 case 'O':
7574 case 'P': {
Chris Lattner9f5d5782007-05-15 01:31:05 +00007575 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattner48884cd2007-08-25 00:47:38 +00007576 if (!CST) return; // Must be an immediate to match.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007577 unsigned Value = CST->getZExtValue();
Chris Lattner763317d2006-02-07 00:47:13 +00007578 switch (Letter) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007579 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner763317d2006-02-07 00:47:13 +00007580 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007581 if ((short)Value == (int)Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00007582 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007583 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007584 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
7585 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007586 if ((short)Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00007587 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007588 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007589 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007590 if ((Value >> 16) == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00007591 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007592 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007593 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007594 if (Value > 31)
Chris Lattner48884cd2007-08-25 00:47:38 +00007595 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007596 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007597 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007598 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattner48884cd2007-08-25 00:47:38 +00007599 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007600 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007601 case 'O': // "O" is the constant zero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007602 if (Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00007603 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007604 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007605 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007606 if ((short)-Value == (int)-Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00007607 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007608 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007609 }
7610 break;
7611 }
7612 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007613
Gabor Greifba36cb52008-08-28 21:40:38 +00007614 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00007615 Ops.push_back(Result);
7616 return;
7617 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007618
Chris Lattner763317d2006-02-07 00:47:13 +00007619 // Handle standard constraint letters.
Eric Christopher100c8332011-06-02 23:16:42 +00007620 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner763317d2006-02-07 00:47:13 +00007621}
Evan Chengc4c62572006-03-13 23:20:37 +00007622
Chris Lattnerc9addb72007-03-30 23:15:24 +00007623// isLegalAddressingMode - Return true if the addressing mode represented
7624// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007625bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007626 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +00007627 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelfdc40a02009-02-17 22:15:04 +00007628
Chris Lattnerc9addb72007-03-30 23:15:24 +00007629 // PPC allows a sign-extended 16-bit immediate field.
7630 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
7631 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007632
Chris Lattnerc9addb72007-03-30 23:15:24 +00007633 // No global is ever allowed as a base.
7634 if (AM.BaseGV)
7635 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007636
7637 // PPC only support r+r,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007638 switch (AM.Scale) {
7639 case 0: // "r+i" or just "i", depending on HasBaseReg.
7640 break;
7641 case 1:
7642 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
7643 return false;
7644 // Otherwise we have r+r or r+i.
7645 break;
7646 case 2:
7647 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
7648 return false;
7649 // Allow 2*r as r+r.
7650 break;
Chris Lattner7c7ba9d2007-04-09 22:10:05 +00007651 default:
7652 // No other scales are supported.
7653 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007654 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007655
Chris Lattnerc9addb72007-03-30 23:15:24 +00007656 return true;
7657}
7658
Dan Gohmand858e902010-04-17 15:26:15 +00007659SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
7660 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007661 MachineFunction &MF = DAG.getMachineFunction();
7662 MachineFrameInfo *MFI = MF.getFrameInfo();
7663 MFI->setReturnAddressIsTaken(true);
7664
Andrew Trickac6d9be2013-05-25 02:42:55 +00007665 SDLoc dl(Op);
Dale Johannesen08673d22010-05-03 22:59:34 +00007666 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattner3fc027d2007-12-08 06:59:59 +00007667
Dale Johannesen08673d22010-05-03 22:59:34 +00007668 // Make sure the function does not optimize away the store of the RA to
7669 // the stack.
Chris Lattner3fc027d2007-12-08 06:59:59 +00007670 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen08673d22010-05-03 22:59:34 +00007671 FuncInfo->setLRStoreRequired();
7672 bool isPPC64 = PPCSubTarget.isPPC64();
7673 bool isDarwinABI = PPCSubTarget.isDarwinABI();
7674
7675 if (Depth > 0) {
7676 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7677 SDValue Offset =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007678
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00007679 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen08673d22010-05-03 22:59:34 +00007680 isPPC64? MVT::i64 : MVT::i32);
7681 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
7682 DAG.getNode(ISD::ADD, dl, getPointerTy(),
7683 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007684 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen08673d22010-05-03 22:59:34 +00007685 }
Chris Lattner3fc027d2007-12-08 06:59:59 +00007686
Chris Lattner3fc027d2007-12-08 06:59:59 +00007687 // Just load the return address off the stack.
Dan Gohman475871a2008-07-27 21:46:04 +00007688 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen08673d22010-05-03 22:59:34 +00007689 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007690 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattner3fc027d2007-12-08 06:59:59 +00007691}
7692
Dan Gohmand858e902010-04-17 15:26:15 +00007693SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
7694 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007695 SDLoc dl(Op);
Dale Johannesen08673d22010-05-03 22:59:34 +00007696 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007697
Owen Andersone50ed302009-08-10 22:56:29 +00007698 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00007699 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelfdc40a02009-02-17 22:15:04 +00007700
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00007701 MachineFunction &MF = DAG.getMachineFunction();
7702 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen08673d22010-05-03 22:59:34 +00007703 MFI->setFrameAddressIsTaken(true);
Hal Finkele9cc0a02013-03-21 19:03:19 +00007704
7705 // Naked functions never have a frame pointer, and so we use r1. For all
7706 // other functions, this decision must be delayed until during PEI.
7707 unsigned FrameReg;
7708 if (MF.getFunction()->getAttributes().hasAttribute(
7709 AttributeSet::FunctionIndex, Attribute::Naked))
7710 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
7711 else
7712 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
7713
Dale Johannesen08673d22010-05-03 22:59:34 +00007714 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
7715 PtrVT);
7716 while (Depth--)
7717 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007718 FrameAddr, MachinePointerInfo(), false, false,
7719 false, 0);
Dale Johannesen08673d22010-05-03 22:59:34 +00007720 return FrameAddr;
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00007721}
Dan Gohman54aeea32008-10-21 03:41:46 +00007722
7723bool
7724PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7725 // The PowerPC target isn't yet aware of offsets.
7726 return false;
7727}
Tilmann Schellerffd02002009-07-03 06:45:56 +00007728
Evan Cheng42642d02010-04-01 20:10:42 +00007729/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengf28f8bc2010-04-02 19:36:14 +00007730/// and store operations as a result of memset, memcpy, and memmove
7731/// lowering. If DstAlign is zero that means it's safe to destination
7732/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
7733/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00007734/// probably because the source does not need to be loaded. If 'IsMemset' is
7735/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
7736/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
7737/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00007738/// It returns EVT::Other if the type should be determined using generic
7739/// target-independent logic.
Evan Cheng255f20f2010-04-01 06:04:33 +00007740EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
7741 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00007742 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00007743 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00007744 MachineFunction &MF) const {
Tilmann Schellerffd02002009-07-03 06:45:56 +00007745 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007746 return MVT::i64;
Tilmann Schellerffd02002009-07-03 06:45:56 +00007747 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00007748 return MVT::i32;
Tilmann Schellerffd02002009-07-03 06:45:56 +00007749 }
7750}
Hal Finkel3f31d492012-04-01 19:23:08 +00007751
Hal Finkel2d37f7b2013-03-15 15:27:13 +00007752bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
7753 bool *Fast) const {
7754 if (DisablePPCUnaligned)
7755 return false;
7756
7757 // PowerPC supports unaligned memory access for simple non-vector types.
7758 // Although accessing unaligned addresses is not as efficient as accessing
7759 // aligned addresses, it is generally more efficient than manual expansion,
7760 // and generally only traps for software emulation when crossing page
7761 // boundaries.
7762
7763 if (!VT.isSimple())
7764 return false;
7765
7766 if (VT.getSimpleVT().isVector())
7767 return false;
7768
7769 if (VT == MVT::ppcf128)
7770 return false;
7771
7772 if (Fast)
7773 *Fast = true;
7774
7775 return true;
7776}
7777
Hal Finkel070b8db2012-06-22 00:49:52 +00007778/// isFMAFasterThanMulAndAdd - Return true if an FMA operation is faster than
7779/// a pair of mul and add instructions. fmuladd intrinsics will be expanded to
7780/// FMAs when this method returns true (and FMAs are legal), otherwise fmuladd
7781/// is expanded to mul + add.
7782bool PPCTargetLowering::isFMAFasterThanMulAndAdd(EVT VT) const {
7783 if (!VT.isSimple())
7784 return false;
7785
7786 switch (VT.getSimpleVT().SimpleTy) {
7787 case MVT::f32:
7788 case MVT::f64:
7789 case MVT::v4f32:
7790 return true;
7791 default:
7792 break;
7793 }
7794
7795 return false;
7796}
7797
Hal Finkel3f31d492012-04-01 19:23:08 +00007798Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel71ffcfe2012-06-10 19:32:29 +00007799 if (DisableILPPref)
7800 return TargetLowering::getSchedulingPreference(N);
Hal Finkel3f31d492012-04-01 19:23:08 +00007801
Hal Finkel71ffcfe2012-06-10 19:32:29 +00007802 return Sched::ILP;
Hal Finkel3f31d492012-04-01 19:23:08 +00007803}
7804