blob: 527430238cba00da673080fddc8589fa0fa204bb [file] [log] [blame]
Nate Begeman0b71e002005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattnerf22556d2005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf22556d2005-08-16 17:14:42 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattnerf22556d2005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6f3b9542005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Jim Laskey48850c12006-11-16 22:43:37 +000016#include "PPCMachineFunctionInfo.h"
Bill Wendlingdd3fe942010-03-12 02:00:43 +000017#include "PPCPerfectShuffle.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000018#include "PPCTargetMachine.h"
Bill Schmidt22d40dc2013-05-13 19:34:37 +000019#include "PPCTargetObjectFile.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000020#include "llvm/ADT/STLExtras.h"
Chris Lattner4f2e4e02007-03-06 00:59:59 +000021#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner9b577f12005-08-26 21:23:58 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000026#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000027#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/Constants.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/Intrinsics.h"
Chris Lattnerce645542006-11-10 02:08:47 +000033#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
Craig Topperb25fda92012-03-17 18:46:09 +000035#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000036#include "llvm/Support/raw_ostream.h"
Craig Topperb25fda92012-03-17 18:46:09 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000038using namespace llvm;
39
Hal Finkel595817e2012-06-04 02:21:00 +000040static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
41cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000042
Hal Finkel4e9f1a82012-06-10 19:32:29 +000043static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
44cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
45
Hal Finkel8d7fbc92013-03-15 15:27:13 +000046static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
47cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
48
Hal Finkel940ab932014-02-28 00:27:01 +000049// FIXME: Remove this once the bug has been fixed!
50extern cl::opt<bool> ANDIGlueBug;
51
Chris Lattner5e693ed2009-07-28 03:13:23 +000052static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) {
53 if (TM.getSubtargetImpl()->isDarwin())
Bill Wendlingbbcaa402010-03-15 21:09:38 +000054 return new TargetLoweringObjectFileMachO();
Bill Wendlingdd3fe942010-03-12 02:00:43 +000055
Bill Schmidt22d40dc2013-05-13 19:34:37 +000056 if (TM.getSubtargetImpl()->isSVR4ABI())
57 return new PPC64LinuxTargetObjectFile();
58
Bruno Cardoso Lopes62e6a8b2009-08-13 23:30:21 +000059 return new TargetLoweringObjectFileELF();
Chris Lattner5e693ed2009-07-28 03:13:23 +000060}
61
Chris Lattner584a11a2006-11-02 01:44:04 +000062PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Chris Lattner5e693ed2009-07-28 03:13:23 +000063 : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) {
Evan Cheng39e90022012-07-02 22:39:56 +000064 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
Scott Michelcf0da6c2009-02-17 22:15:04 +000065
Nate Begeman4dd38312005-10-21 00:02:42 +000066 setPow2DivIsCheap();
Dale Johannesenc31eb202008-07-31 18:13:12 +000067
Chris Lattnera028e7a2005-09-27 22:18:25 +000068 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000069 setUseUnderscoreSetJmp(true);
70 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000071
Chris Lattnerd10babf2010-10-10 18:34:00 +000072 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
73 // arguments are at least 4/8 bytes aligned.
Evan Cheng39e90022012-07-02 22:39:56 +000074 bool isPPC64 = Subtarget->isPPC64();
75 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000076
Chris Lattnerf22556d2005-08-16 17:14:42 +000077 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000078 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
79 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
80 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +000081
Evan Cheng5d9fd972006-10-04 00:56:09 +000082 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson9f944592009-08-11 20:47:22 +000083 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
84 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sands95d46ef2008-01-23 20:39:46 +000085
Owen Anderson9f944592009-08-11 20:47:22 +000086 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000087
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000088 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000089 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
90 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
91 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
92 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
93 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
94 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
95 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
96 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
97 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
98 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +000099
Hal Finkel940ab932014-02-28 00:27:01 +0000100 if (Subtarget->useCRBits()) {
101 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
102
Hal Finkel6a56b212014-03-05 22:14:00 +0000103 if (isPPC64 || Subtarget->hasFPCVT()) {
104 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
105 AddPromotedToType (ISD::SINT_TO_FP, MVT::i1,
106 isPPC64 ? MVT::i64 : MVT::i32);
107 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
108 AddPromotedToType (ISD::UINT_TO_FP, MVT::i1,
109 isPPC64 ? MVT::i64 : MVT::i32);
110 } else {
111 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom);
112 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom);
113 }
Hal Finkel940ab932014-02-28 00:27:01 +0000114
115 // PowerPC does not support direct load / store of condition registers
116 setOperationAction(ISD::LOAD, MVT::i1, Custom);
117 setOperationAction(ISD::STORE, MVT::i1, Custom);
118
119 // FIXME: Remove this once the ANDI glue bug is fixed:
120 if (ANDIGlueBug)
121 setOperationAction(ISD::TRUNCATE, MVT::i1, Custom);
122
123 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
124 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
125 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
126 setTruncStoreAction(MVT::i32, MVT::i1, Expand);
127 setTruncStoreAction(MVT::i16, MVT::i1, Expand);
128 setTruncStoreAction(MVT::i8, MVT::i1, Expand);
129
130 addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass);
131 }
132
Dale Johannesen666323e2007-10-10 01:01:31 +0000133 // This is used in the ppcf128->int sequence. Note it has different semantics
134 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +0000135 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000136
Roman Divacky1faf5b02012-08-16 18:19:29 +0000137 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000138 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
139 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
140 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
141 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
142 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000143 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000144
Chris Lattnerf22556d2005-08-16 17:14:42 +0000145 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000146 setOperationAction(ISD::SREM, MVT::i32, Expand);
147 setOperationAction(ISD::UREM, MVT::i32, Expand);
148 setOperationAction(ISD::SREM, MVT::i64, Expand);
149 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000150
151 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000152 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
153 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
154 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
155 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
156 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
157 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
158 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
159 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000160
Dan Gohman482732a2007-10-11 23:21:31 +0000161 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000162 setOperationAction(ISD::FSIN , MVT::f64, Expand);
163 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000164 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000165 setOperationAction(ISD::FREM , MVT::f64, Expand);
166 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000167 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000168 setOperationAction(ISD::FSIN , MVT::f32, Expand);
169 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000170 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000171 setOperationAction(ISD::FREM , MVT::f32, Expand);
172 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000173 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000174
Owen Anderson9f944592009-08-11 20:47:22 +0000175 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000176
Chris Lattnerf22556d2005-08-16 17:14:42 +0000177 // If we're enabling GP optimizations, use hardware square root
Hal Finkel2e103312013-04-03 04:01:11 +0000178 if (!Subtarget->hasFSQRT() &&
179 !(TM.Options.UnsafeFPMath &&
180 Subtarget->hasFRSQRTE() && Subtarget->hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000181 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000182
183 if (!Subtarget->hasFSQRT() &&
184 !(TM.Options.UnsafeFPMath &&
185 Subtarget->hasFRSQRTES() && Subtarget->hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000186 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000187
Hal Finkeldbc78e12013-08-19 05:01:02 +0000188 if (Subtarget->hasFCPSGN()) {
189 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
190 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
191 } else {
192 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
193 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
194 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000195
Hal Finkelc20a08d2013-03-29 08:57:48 +0000196 if (Subtarget->hasFPRND()) {
197 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
198 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
199 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000200 setOperationAction(ISD::FROUND, MVT::f64, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000201
202 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
203 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
204 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000205 setOperationAction(ISD::FROUND, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000206 }
207
Nate Begeman2fba8a32006-01-14 03:14:10 +0000208 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000209 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000210 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000211 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
212 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000213 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000214 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000215 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
216 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000217
Hal Finkela4d07482013-03-28 13:29:47 +0000218 if (Subtarget->hasPOPCNTD()) {
Hal Finkel290376d2013-04-01 15:58:15 +0000219 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000220 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
221 } else {
222 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
223 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
224 }
225
Nate Begeman1b8121b2006-01-11 21:21:00 +0000226 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000227 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
228 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000229
Hal Finkel940ab932014-02-28 00:27:01 +0000230 if (!Subtarget->useCRBits()) {
231 // PowerPC does not have Select
232 setOperationAction(ISD::SELECT, MVT::i32, Expand);
233 setOperationAction(ISD::SELECT, MVT::i64, Expand);
234 setOperationAction(ISD::SELECT, MVT::f32, Expand);
235 setOperationAction(ISD::SELECT, MVT::f64, Expand);
236 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000237
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000238 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000239 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
240 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000241
Nate Begeman7e7f4392006-02-01 07:19:44 +0000242 // PowerPC wants to optimize integer setcc a bit
Hal Finkel940ab932014-02-28 00:27:01 +0000243 if (!Subtarget->useCRBits())
244 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000245
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000246 // PowerPC does not have BRCOND which requires SetCC
Hal Finkel940ab932014-02-28 00:27:01 +0000247 if (!Subtarget->useCRBits())
248 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000249
Owen Anderson9f944592009-08-11 20:47:22 +0000250 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000251
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000252 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000253 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000254
Jim Laskey6267b2c2005-08-17 00:40:22 +0000255 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000256 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
257 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000258
Wesley Peck527da1b2010-11-23 03:31:01 +0000259 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
260 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
261 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
262 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattnerc46fc242005-12-23 05:13:35 +0000263
Chris Lattner84b49d52006-04-28 21:56:10 +0000264 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000265 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000266
Hal Finkel1996f3d2013-03-27 19:10:42 +0000267 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000268 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
269 // support continuation, user-level threading, and etc.. As a result, no
270 // other SjLj exception interfaces are implemented and please don't build
271 // your own exception handling based on them.
272 // LLVM/Clang supports zero-cost DWARF exception handling.
273 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
274 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000275
276 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000277 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000278 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
279 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000280 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000281 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
282 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
283 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
284 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000285 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000286 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
287 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000288
Nate Begemanf69d13b2008-08-11 17:36:31 +0000289 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000290 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000291
292 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000293 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
294 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000295
Nate Begemane74795c2006-01-25 18:21:52 +0000296 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000297 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000298
Evan Cheng39e90022012-07-02 22:39:56 +0000299 if (Subtarget->isSVR4ABI()) {
300 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000301 // VAARG always uses double-word chunks, so promote anything smaller.
302 setOperationAction(ISD::VAARG, MVT::i1, Promote);
303 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
304 setOperationAction(ISD::VAARG, MVT::i8, Promote);
305 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
306 setOperationAction(ISD::VAARG, MVT::i16, Promote);
307 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
308 setOperationAction(ISD::VAARG, MVT::i32, Promote);
309 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
310 setOperationAction(ISD::VAARG, MVT::Other, Expand);
311 } else {
312 // VAARG is custom lowered with the 32-bit SVR4 ABI.
313 setOperationAction(ISD::VAARG, MVT::Other, Custom);
314 setOperationAction(ISD::VAARG, MVT::i64, Custom);
315 }
Roman Divacky4394e682011-06-28 15:30:42 +0000316 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000317 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000318
Roman Divackyc3825df2013-07-25 21:36:47 +0000319 if (Subtarget->isSVR4ABI() && !isPPC64)
320 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
321 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
322 else
323 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
324
Chris Lattner5bd514d2006-01-15 09:02:48 +0000325 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000326 setOperationAction(ISD::VAEND , MVT::Other, Expand);
327 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
328 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
329 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
330 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000331
Chris Lattner6961fc72006-03-26 10:06:40 +0000332 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000333 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000334
Hal Finkel25c19922013-05-15 21:37:41 +0000335 // To handle counter-based loop conditions.
336 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
337
Dale Johannesen160be0f2008-11-07 22:54:33 +0000338 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000339 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
340 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
341 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
342 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
343 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
344 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
345 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
346 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
347 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
348 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
349 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
350 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000351
Evan Cheng39e90022012-07-02 22:39:56 +0000352 if (Subtarget->has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000353 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000354 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
355 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
356 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
357 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000358 // This is just the low 32 bits of a (signed) fp->i64 conversion.
359 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000360 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000361
Hal Finkelf6d45f22013-04-01 17:52:07 +0000362 if (PPCSubTarget.hasLFIWAX() || Subtarget->isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000363 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000364 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000365 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000366 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000367 }
368
Hal Finkelf6d45f22013-04-01 17:52:07 +0000369 // With the instructions enabled under FPCVT, we can do everything.
370 if (PPCSubTarget.hasFPCVT()) {
371 if (Subtarget->has64BitSupport()) {
372 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
373 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
374 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
375 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
376 }
377
378 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
379 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
380 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
381 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
382 }
383
Evan Cheng39e90022012-07-02 22:39:56 +0000384 if (Subtarget->use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000385 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000386 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000387 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000388 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000389 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000390 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
391 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
392 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000393 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000394 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000395 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
396 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
397 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000398 }
Evan Cheng19264272006-03-01 01:11:20 +0000399
Evan Cheng39e90022012-07-02 22:39:56 +0000400 if (Subtarget->hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000401 // First set operation action for all vector types to expand. Then we
402 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson9f944592009-08-11 20:47:22 +0000403 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
404 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
405 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands13237ac2008-06-06 12:08:01 +0000406
Chris Lattner06a21ba2006-04-16 01:37:57 +0000407 // add/sub are legal for all supported vector VT's.
Duncan Sands13237ac2008-06-06 12:08:01 +0000408 setOperationAction(ISD::ADD , VT, Legal);
409 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000410
Chris Lattner95c7adc2006-04-04 17:25:31 +0000411 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000412 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000413 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000414
415 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000416 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000417 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000418 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000419 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000420 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000421 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000422 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000423 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000424 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000425 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000426 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000427 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000428
Chris Lattner06a21ba2006-04-16 01:37:57 +0000429 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000430 setOperationAction(ISD::MUL , VT, Expand);
431 setOperationAction(ISD::SDIV, VT, Expand);
432 setOperationAction(ISD::SREM, VT, Expand);
433 setOperationAction(ISD::UDIV, VT, Expand);
434 setOperationAction(ISD::UREM, VT, Expand);
435 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000436 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000437 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000438 setOperationAction(ISD::FSQRT, VT, Expand);
439 setOperationAction(ISD::FLOG, VT, Expand);
440 setOperationAction(ISD::FLOG10, VT, Expand);
441 setOperationAction(ISD::FLOG2, VT, Expand);
442 setOperationAction(ISD::FEXP, VT, Expand);
443 setOperationAction(ISD::FEXP2, VT, Expand);
444 setOperationAction(ISD::FSIN, VT, Expand);
445 setOperationAction(ISD::FCOS, VT, Expand);
446 setOperationAction(ISD::FABS, VT, Expand);
447 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000448 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000449 setOperationAction(ISD::FCEIL, VT, Expand);
450 setOperationAction(ISD::FTRUNC, VT, Expand);
451 setOperationAction(ISD::FRINT, VT, Expand);
452 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000453 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
454 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
455 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
456 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
457 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
458 setOperationAction(ISD::UDIVREM, VT, Expand);
459 setOperationAction(ISD::SDIVREM, VT, Expand);
460 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
461 setOperationAction(ISD::FPOW, VT, Expand);
462 setOperationAction(ISD::CTPOP, VT, Expand);
463 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000464 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000465 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000466 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000467 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000468 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
469
470 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
471 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
472 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
473 setTruncStoreAction(VT, InnerVT, Expand);
474 }
475 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
476 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
477 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000478 }
479
Chris Lattner95c7adc2006-04-04 17:25:31 +0000480 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
481 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000482 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000483
Owen Anderson9f944592009-08-11 20:47:22 +0000484 setOperationAction(ISD::AND , MVT::v4i32, Legal);
485 setOperationAction(ISD::OR , MVT::v4i32, Legal);
486 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
487 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Hal Finkel940ab932014-02-28 00:27:01 +0000488 setOperationAction(ISD::SELECT, MVT::v4i32,
489 Subtarget->useCRBits() ? Legal : Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000490 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000491 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
492 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
493 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
494 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000495 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
496 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
497 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
498 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000499
Craig Topperabadc662012-04-20 06:31:50 +0000500 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
501 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
502 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
503 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000504
Owen Anderson9f944592009-08-11 20:47:22 +0000505 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000506 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000507
Hal Finkel27774d92014-03-13 07:58:58 +0000508 if (TM.Options.UnsafeFPMath || Subtarget->hasVSX()) {
Hal Finkel2e103312013-04-03 04:01:11 +0000509 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
510 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
511 }
512
Owen Anderson9f944592009-08-11 20:47:22 +0000513 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
514 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
515 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000516
Owen Anderson9f944592009-08-11 20:47:22 +0000517 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
518 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000519
Owen Anderson9f944592009-08-11 20:47:22 +0000520 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
521 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
522 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
523 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000524
525 // Altivec does not contain unordered floating-point compare instructions
526 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
527 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
528 setCondCodeAction(ISD::SETUGT, MVT::v4f32, Expand);
529 setCondCodeAction(ISD::SETUGE, MVT::v4f32, Expand);
530 setCondCodeAction(ISD::SETULT, MVT::v4f32, Expand);
531 setCondCodeAction(ISD::SETULE, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000532
533 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
534 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000535
536 if (Subtarget->hasVSX()) {
537 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
Hal Finkel82569b62014-03-27 22:22:48 +0000538 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
Hal Finkel27774d92014-03-13 07:58:58 +0000539
540 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
541 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
542 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
543 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
544 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
545
546 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
547
548 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
549 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
550
551 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
552 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
553
Hal Finkel732f0f72014-03-26 12:49:28 +0000554 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
555 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal);
556 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
557 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
558 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
559
Hal Finkel27774d92014-03-13 07:58:58 +0000560 // Share the Altivec comparison restrictions.
561 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand);
562 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand);
563 setCondCodeAction(ISD::SETUGT, MVT::v2f64, Expand);
564 setCondCodeAction(ISD::SETUGE, MVT::v2f64, Expand);
565 setCondCodeAction(ISD::SETULT, MVT::v2f64, Expand);
566 setCondCodeAction(ISD::SETULE, MVT::v2f64, Expand);
567
568 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand);
569 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand);
570
Hal Finkel9281c9a2014-03-26 18:26:30 +0000571 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
572 setOperationAction(ISD::STORE, MVT::v2f64, Legal);
573
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000574 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal);
575
Hal Finkel19be5062014-03-29 05:29:01 +0000576 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000577
578 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass);
579 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass);
Hal Finkela6c8b512014-03-26 16:12:58 +0000580
581 // VSX v2i64 only supports non-arithmetic operations.
582 setOperationAction(ISD::ADD, MVT::v2i64, Expand);
583 setOperationAction(ISD::SUB, MVT::v2i64, Expand);
584
Hal Finkelad801b72014-03-27 21:26:33 +0000585 setOperationAction(ISD::SHL, MVT::v2i64, Expand);
586 setOperationAction(ISD::SRA, MVT::v2i64, Expand);
587 setOperationAction(ISD::SRL, MVT::v2i64, Expand);
588
Hal Finkel9281c9a2014-03-26 18:26:30 +0000589 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
590 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64);
591 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
592 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64);
593
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000594 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal);
595
Hal Finkel7279f4b2014-03-26 19:13:54 +0000596 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
597 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
598 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
599 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
600
Hal Finkela6c8b512014-03-26 16:12:58 +0000601 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000602 }
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000603 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000604
Hal Finkel70381a72012-08-04 14:10:46 +0000605 if (Subtarget->has64BitSupport()) {
Hal Finkel322e41a2012-04-01 20:08:17 +0000606 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel70381a72012-08-04 14:10:46 +0000607 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
608 }
Hal Finkel322e41a2012-04-01 20:08:17 +0000609
Eli Friedman7dfa7912011-08-29 18:23:02 +0000610 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
611 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkel1b5ff082012-12-25 17:22:53 +0000612 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
613 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000614
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000615 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidta76bf5a2013-04-23 18:49:44 +0000616 // Altivec instructions set fields to all zeros or all ones.
617 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000618
Evan Cheng39e90022012-07-02 22:39:56 +0000619 if (isPPC64) {
Chris Lattner454436d2006-10-18 01:20:43 +0000620 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000621 setExceptionPointerRegister(PPC::X3);
622 setExceptionSelectorRegister(PPC::X4);
623 } else {
Chris Lattner454436d2006-10-18 01:20:43 +0000624 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000625 setExceptionPointerRegister(PPC::R3);
626 setExceptionSelectorRegister(PPC::R4);
627 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000628
Chris Lattnerf4184352006-03-01 04:57:39 +0000629 // We have target-specific dag combine patterns for the following nodes:
630 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000631 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000632 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000633 setTargetDAGCombine(ISD::BR_CC);
Hal Finkel940ab932014-02-28 00:27:01 +0000634 if (Subtarget->useCRBits())
635 setTargetDAGCombine(ISD::BRCOND);
Chris Lattnera7976d32006-07-10 20:56:58 +0000636 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000637 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000638
Hal Finkel46043ed2014-03-01 21:36:57 +0000639 setTargetDAGCombine(ISD::SIGN_EXTEND);
640 setTargetDAGCombine(ISD::ZERO_EXTEND);
641 setTargetDAGCombine(ISD::ANY_EXTEND);
642
Hal Finkel940ab932014-02-28 00:27:01 +0000643 if (Subtarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000644 setTargetDAGCombine(ISD::TRUNCATE);
645 setTargetDAGCombine(ISD::SETCC);
646 setTargetDAGCombine(ISD::SELECT_CC);
647 }
648
Hal Finkel2e103312013-04-03 04:01:11 +0000649 // Use reciprocal estimates.
650 if (TM.Options.UnsafeFPMath) {
651 setTargetDAGCombine(ISD::FDIV);
652 setTargetDAGCombine(ISD::FSQRT);
653 }
654
Dale Johannesen10432e52007-10-19 00:59:18 +0000655 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng39e90022012-07-02 22:39:56 +0000656 if (Subtarget->isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000657 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000658 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
659 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000660 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
661 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000662 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
663 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
664 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
665 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
666 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000667 }
668
Hal Finkel940ab932014-02-28 00:27:01 +0000669 // With 32 condition bits, we don't need to sink (and duplicate) compares
670 // aggressively in CodeGenPrep.
671 if (Subtarget->useCRBits())
672 setHasMultipleConditionRegisters();
673
Hal Finkel65298572011-10-17 18:53:03 +0000674 setMinFunctionAlignment(2);
675 if (PPCSubTarget.isDarwin())
676 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000677
Evan Cheng39e90022012-07-02 22:39:56 +0000678 if (isPPC64 && Subtarget->isJITCodeModel())
679 // Temporary workaround for the inability of PPC64 JIT to handle jump
680 // tables.
681 setSupportJumpTables(false);
682
Eli Friedman30a49e92011-08-03 21:06:02 +0000683 setInsertFencesForAtomic(true);
684
Hal Finkel21442b22013-09-11 23:05:25 +0000685 if (Subtarget->enableMachineScheduler())
686 setSchedulingPreference(Sched::Source);
687 else
688 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000689
Chris Lattnerf22556d2005-08-16 17:14:42 +0000690 computeRegisterProperties();
Hal Finkel742b5352012-08-28 16:12:39 +0000691
692 // The Freescale cores does better with aggressive inlining of memcpy and
693 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
694 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
695 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000696 MaxStoresPerMemset = 32;
697 MaxStoresPerMemsetOptSize = 16;
698 MaxStoresPerMemcpy = 32;
699 MaxStoresPerMemcpyOptSize = 8;
700 MaxStoresPerMemmove = 32;
701 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel742b5352012-08-28 16:12:39 +0000702
703 setPrefFunctionAlignment(4);
Hal Finkel742b5352012-08-28 16:12:39 +0000704 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000705}
706
Hal Finkel262a2242013-09-12 23:20:06 +0000707/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
708/// the desired ByVal argument alignment.
709static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
710 unsigned MaxMaxAlign) {
711 if (MaxAlign == MaxMaxAlign)
712 return;
713 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
714 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
715 MaxAlign = 32;
716 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
717 MaxAlign = 16;
718 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
719 unsigned EltAlign = 0;
720 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
721 if (EltAlign > MaxAlign)
722 MaxAlign = EltAlign;
723 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
724 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
725 unsigned EltAlign = 0;
726 getMaxByValAlign(STy->getElementType(i), EltAlign, MaxMaxAlign);
727 if (EltAlign > MaxAlign)
728 MaxAlign = EltAlign;
729 if (MaxAlign == MaxMaxAlign)
730 break;
731 }
732 }
733}
734
Dale Johannesencbde4c22008-02-28 22:31:51 +0000735/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
736/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000737unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000738 // Darwin passes everything on 4 byte boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000739 if (PPCSubTarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000740 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000741
742 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000743 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000744 unsigned Align = PPCSubTarget.isPPC64() ? 8 : 4;
745 if (PPCSubTarget.hasAltivec() || PPCSubTarget.hasQPX())
746 getMaxByValAlign(Ty, Align, PPCSubTarget.hasQPX() ? 32 : 16);
747 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000748}
749
Chris Lattner347ed8a2006-01-09 23:52:17 +0000750const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
751 switch (Opcode) {
752 default: return 0;
Evan Cheng32e376f2008-07-12 02:23:19 +0000753 case PPCISD::FSEL: return "PPCISD::FSEL";
754 case PPCISD::FCFID: return "PPCISD::FCFID";
755 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
756 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000757 case PPCISD::FRE: return "PPCISD::FRE";
758 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000759 case PPCISD::STFIWX: return "PPCISD::STFIWX";
760 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
761 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
762 case PPCISD::VPERM: return "PPCISD::VPERM";
763 case PPCISD::Hi: return "PPCISD::Hi";
764 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000765 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller79fef932009-12-18 13:00:15 +0000766 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
767 case PPCISD::LOAD: return "PPCISD::LOAD";
768 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000769 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
770 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
771 case PPCISD::SRL: return "PPCISD::SRL";
772 case PPCISD::SRA: return "PPCISD::SRA";
773 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000774 case PPCISD::CALL: return "PPCISD::CALL";
775 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000776 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000777 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng32e376f2008-07-12 02:23:19 +0000778 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel756810f2013-03-21 21:37:52 +0000779 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
780 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000781 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng32e376f2008-07-12 02:23:19 +0000782 case PPCISD::VCMP: return "PPCISD::VCMP";
783 case PPCISD::VCMPo: return "PPCISD::VCMPo";
784 case PPCISD::LBRX: return "PPCISD::LBRX";
785 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng32e376f2008-07-12 02:23:19 +0000786 case PPCISD::LARX: return "PPCISD::LARX";
787 case PPCISD::STCX: return "PPCISD::STCX";
788 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +0000789 case PPCISD::BDNZ: return "PPCISD::BDNZ";
790 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000791 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000792 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000793 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +0000794 case PPCISD::CR6SET: return "PPCISD::CR6SET";
795 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34627e32012-11-27 17:35:46 +0000796 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
797 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
798 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Roman Divacky32143e22013-12-20 18:08:54 +0000799 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000800 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
801 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000802 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000803 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
804 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
805 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000806 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
807 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
808 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
809 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
810 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +0000811 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +0000812 case PPCISD::SC: return "PPCISD::SC";
Chris Lattner347ed8a2006-01-09 23:52:17 +0000813 }
814}
815
Matt Arsenault758659232013-05-18 00:21:46 +0000816EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000817 if (!VT.isVector())
Hal Finkel940ab932014-02-28 00:27:01 +0000818 return PPCSubTarget.useCRBits() ? MVT::i1 : MVT::i32;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000819 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000820}
821
Chris Lattner4211ca92006-04-14 06:01:58 +0000822//===----------------------------------------------------------------------===//
823// Node matching predicates, for use by the tblgen matching code.
824//===----------------------------------------------------------------------===//
825
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000826/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000827static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000828 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000829 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000830 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000831 // Maybe this has already been legalized into the constant pool?
832 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000833 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000834 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000835 }
836 return false;
837}
838
Chris Lattnere8b83b42006-04-06 17:23:16 +0000839/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
840/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000841static bool isConstantOrUndef(int Op, int Val) {
842 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000843}
844
845/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
846/// VPKUHUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000847bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000848 if (!isUnary) {
849 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000850 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000851 return false;
852 } else {
853 for (unsigned i = 0; i != 8; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000854 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
855 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000856 return false;
857 }
Chris Lattner1d338192006-04-06 18:26:28 +0000858 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000859}
860
861/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
862/// VPKUWUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000863bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000864 if (!isUnary) {
865 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000866 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
867 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000868 return false;
869 } else {
870 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000871 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
872 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
873 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
874 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000875 return false;
876 }
Chris Lattner1d338192006-04-06 18:26:28 +0000877 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000878}
879
Chris Lattnerf38e0332006-04-06 22:02:42 +0000880/// isVMerge - Common function, used to match vmrg* shuffles.
881///
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000882static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +0000883 unsigned LHSStart, unsigned RHSStart) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000884 if (N->getValueType(0) != MVT::v16i8)
885 return false;
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000886 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
887 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000888
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000889 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
890 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000891 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000892 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000893 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000894 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000895 return false;
896 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000897 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +0000898}
899
900/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
901/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000902bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000903 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000904 if (!isUnary)
905 return isVMerge(N, UnitSize, 8, 24);
906 return isVMerge(N, UnitSize, 8, 8);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000907}
908
909/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
910/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000911bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000912 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000913 if (!isUnary)
914 return isVMerge(N, UnitSize, 0, 16);
915 return isVMerge(N, UnitSize, 0, 0);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000916}
917
918
Chris Lattner1d338192006-04-06 18:26:28 +0000919/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
920/// amount, otherwise return -1.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000921int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000922 if (N->getValueType(0) != MVT::v16i8)
923 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000924
925 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +0000926
Chris Lattner1d338192006-04-06 18:26:28 +0000927 // Find the first non-undef value in the shuffle mask.
928 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000929 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +0000930 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000931
Chris Lattner1d338192006-04-06 18:26:28 +0000932 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000933
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000934 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +0000935 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000936 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +0000937 if (ShiftAmt < i) return -1;
938 ShiftAmt -= i;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000939
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000940 if (!isUnary) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000941 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000942 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000943 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000944 return -1;
945 } else {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000946 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000947 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000948 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000949 return -1;
950 }
Chris Lattner1d338192006-04-06 18:26:28 +0000951 return ShiftAmt;
952}
Chris Lattnerffc47562006-03-20 06:33:01 +0000953
954/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
955/// specifies a splat of a single element that is suitable for input to
956/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000957bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +0000958 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +0000959 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000960
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000961 // This is a splat operation if each element of the permute is the same, and
962 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000963 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +0000964
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000965 // FIXME: Handle UNDEF elements too!
966 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +0000967 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000968
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000969 // Check that the indices are consecutive, in the case of a multi-byte element
970 // splatted with a v16i8 mask.
971 for (unsigned i = 1; i != EltSize; ++i)
972 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000973 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000974
Chris Lattner95c7adc2006-04-04 17:25:31 +0000975 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000976 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +0000977 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000978 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000979 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000980 }
Chris Lattner95c7adc2006-04-04 17:25:31 +0000981 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +0000982}
983
Evan Cheng581d2792007-07-30 07:51:22 +0000984/// isAllNegativeZeroVector - Returns true if all elements of build_vector
985/// are -0.0.
986bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000987 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
988
989 APInt APVal, APUndef;
990 unsigned BitSize;
991 bool HasAnyUndefs;
Wesley Peck527da1b2010-11-23 03:31:01 +0000992
Dale Johannesen5f4eecf2009-11-13 01:45:18 +0000993 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000994 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000995 return CFP->getValueAPF().isNegZero();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000996
Evan Cheng581d2792007-07-30 07:51:22 +0000997 return false;
998}
999
Chris Lattnerffc47562006-03-20 06:33:01 +00001000/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
1001/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner95c7adc2006-04-04 17:25:31 +00001002unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001003 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1004 assert(isSplatShuffleMask(SVOp, EltSize));
1005 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +00001006}
1007
Chris Lattner74cf9ff2006-04-12 17:37:20 +00001008/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001009/// by using a vspltis[bhw] instruction of the specified element size, return
1010/// the constant being splatted. The ByteSize field indicates the number of
1011/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001012SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
1013 SDValue OpVal(0, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001014
1015 // If ByteSize of the splat is bigger than the element size of the
1016 // build_vector, then we have a case where we are checking for a splat where
1017 // multiple elements of the buildvector are folded together into a single
1018 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
1019 unsigned EltSize = 16/N->getNumOperands();
1020 if (EltSize < ByteSize) {
1021 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001022 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001023 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001024
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001025 // See if all of the elements in the buildvector agree across.
1026 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1027 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1028 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001029 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001030
Scott Michelcf0da6c2009-02-17 22:15:04 +00001031
Gabor Greiff304a7a2008-08-28 21:40:38 +00001032 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001033 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
1034 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001035 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001036 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001037
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001038 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
1039 // either constant or undef values that are identical for each chunk. See
1040 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001041
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001042 // Check to see if all of the leading entries are either 0 or -1. If
1043 // neither, then this won't fit into the immediate field.
1044 bool LeadingZero = true;
1045 bool LeadingOnes = true;
1046 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001047 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001048
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001049 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
1050 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
1051 }
1052 // Finally, check the least significant entry.
1053 if (LeadingZero) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001054 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +00001055 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00001056 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001057 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +00001058 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001059 }
1060 if (LeadingOnes) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001061 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +00001062 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +00001063 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001064 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +00001065 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001066 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001067
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001068 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001069 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001070
Chris Lattner2771e2c2006-03-25 06:12:06 +00001071 // Check to see if this buildvec has a single non-undef value in its elements.
1072 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1073 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001074 if (OpVal.getNode() == 0)
Chris Lattner2771e2c2006-03-25 06:12:06 +00001075 OpVal = N->getOperand(i);
1076 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001077 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001078 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001079
Gabor Greiff304a7a2008-08-28 21:40:38 +00001080 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001081
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001082 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001083 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001084 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001085 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001086 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001087 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001088 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001089 }
1090
1091 // If the splat value is larger than the element value, then we can never do
1092 // this splat. The only case that we could fit the replicated bits into our
1093 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001094 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001095
Chris Lattner2771e2c2006-03-25 06:12:06 +00001096 // If the element value is larger than the splat value, cut it in half and
1097 // check to see if the two halves are equal. Continue doing this until we
1098 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
1099 while (ValSizeInBytes > ByteSize) {
1100 ValSizeInBytes >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001101
Chris Lattner2771e2c2006-03-25 06:12:06 +00001102 // If the top half equals the bottom half, we're still ok.
Chris Lattner39cc7172006-04-05 17:39:25 +00001103 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
1104 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001105 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001106 }
1107
1108 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001109 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001110
Evan Chengb1ddc982006-03-26 09:52:32 +00001111 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001112 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001113
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001114 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001115 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +00001116 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001117 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001118}
1119
Chris Lattner4211ca92006-04-14 06:01:58 +00001120//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001121// Addressing Mode Selection
1122//===----------------------------------------------------------------------===//
1123
1124/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1125/// or 64-bit immediate, and if the value can be accurately represented as a
1126/// sign extension from a 16-bit value. If so, this returns true and the
1127/// immediate.
1128static bool isIntS16Immediate(SDNode *N, short &Imm) {
1129 if (N->getOpcode() != ISD::Constant)
1130 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001131
Dan Gohmaneffb8942008-09-12 16:56:44 +00001132 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001133 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001134 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001135 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001136 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001137}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001138static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001139 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001140}
1141
1142
1143/// SelectAddressRegReg - Given the specified addressed, check to see if it
1144/// can be represented as an indexed [r+r] operation. Returns false if it
1145/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001146bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1147 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001148 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001149 short imm = 0;
1150 if (N.getOpcode() == ISD::ADD) {
1151 if (isIntS16Immediate(N.getOperand(1), imm))
1152 return false; // r+i
1153 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1154 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001155
Chris Lattnera801fced2006-11-08 02:15:41 +00001156 Base = N.getOperand(0);
1157 Index = N.getOperand(1);
1158 return true;
1159 } else if (N.getOpcode() == ISD::OR) {
1160 if (isIntS16Immediate(N.getOperand(1), imm))
1161 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001162
Chris Lattnera801fced2006-11-08 02:15:41 +00001163 // If this is an or of disjoint bitfields, we can codegen this as an add
1164 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1165 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001166 APInt LHSKnownZero, LHSKnownOne;
1167 APInt RHSKnownZero, RHSKnownOne;
1168 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001169 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001170
Dan Gohmanf19609a2008-02-27 01:23:58 +00001171 if (LHSKnownZero.getBoolValue()) {
1172 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001173 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001174 // If all of the bits are known zero on the LHS or RHS, the add won't
1175 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001176 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001177 Base = N.getOperand(0);
1178 Index = N.getOperand(1);
1179 return true;
1180 }
1181 }
1182 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001183
Chris Lattnera801fced2006-11-08 02:15:41 +00001184 return false;
1185}
1186
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001187// If we happen to be doing an i64 load or store into a stack slot that has
1188// less than a 4-byte alignment, then the frame-index elimination may need to
1189// use an indexed load or store instruction (because the offset may not be a
1190// multiple of 4). The extra register needed to hold the offset comes from the
1191// register scavenger, and it is possible that the scavenger will need to use
1192// an emergency spill slot. As a result, we need to make sure that a spill slot
1193// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1194// stack slot.
1195static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1196 // FIXME: This does not handle the LWA case.
1197 if (VT != MVT::i64)
1198 return;
1199
Hal Finkel7ab3db52013-07-10 15:29:01 +00001200 // NOTE: We'll exclude negative FIs here, which come from argument
1201 // lowering, because there are no known test cases triggering this problem
1202 // using packed structures (or similar). We can remove this exclusion if
1203 // we find such a test case. The reason why this is so test-case driven is
1204 // because this entire 'fixup' is only to prevent crashes (from the
1205 // register scavenger) on not-really-valid inputs. For example, if we have:
1206 // %a = alloca i1
1207 // %b = bitcast i1* %a to i64*
1208 // store i64* a, i64 b
1209 // then the store should really be marked as 'align 1', but is not. If it
1210 // were marked as 'align 1' then the indexed form would have been
1211 // instruction-selected initially, and the problem this 'fixup' is preventing
1212 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001213 if (FrameIdx < 0)
1214 return;
1215
1216 MachineFunction &MF = DAG.getMachineFunction();
1217 MachineFrameInfo *MFI = MF.getFrameInfo();
1218
1219 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1220 if (Align >= 4)
1221 return;
1222
1223 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1224 FuncInfo->setHasNonRISpills();
1225}
1226
Chris Lattnera801fced2006-11-08 02:15:41 +00001227/// Returns true if the address N can be represented by a base register plus
1228/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001229/// represented as reg+reg. If Aligned is true, only accept displacements
1230/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001231bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001232 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001233 SelectionDAG &DAG,
1234 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001235 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001236 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001237 // If this can be more profitably realized as r+r, fail.
1238 if (SelectAddressRegReg(N, Disp, Base, DAG))
1239 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001240
Chris Lattnera801fced2006-11-08 02:15:41 +00001241 if (N.getOpcode() == ISD::ADD) {
1242 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001243 if (isIntS16Immediate(N.getOperand(1), imm) &&
1244 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001245 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001246 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1247 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001248 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001249 } else {
1250 Base = N.getOperand(0);
1251 }
1252 return true; // [r+i]
1253 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1254 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001255 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001256 && "Cannot handle constant offsets yet!");
1257 Disp = N.getOperand(1).getOperand(0); // The global address.
1258 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001259 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001260 Disp.getOpcode() == ISD::TargetConstantPool ||
1261 Disp.getOpcode() == ISD::TargetJumpTable);
1262 Base = N.getOperand(0);
1263 return true; // [&g+r]
1264 }
1265 } else if (N.getOpcode() == ISD::OR) {
1266 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001267 if (isIntS16Immediate(N.getOperand(1), imm) &&
1268 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001269 // If this is an or of disjoint bitfields, we can codegen this as an add
1270 // (for better address arithmetic) if the LHS and RHS of the OR are
1271 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001272 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001273 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001274
Dan Gohmanf19609a2008-02-27 01:23:58 +00001275 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001276 // If all of the bits are known zero on the LHS or RHS, the add won't
1277 // carry.
1278 Base = N.getOperand(0);
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001279 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001280 return true;
1281 }
1282 }
1283 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1284 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001285
Chris Lattnera801fced2006-11-08 02:15:41 +00001286 // If this address fits entirely in a 16-bit sext immediate field, codegen
1287 // this as "d, 0"
1288 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001289 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001290 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkelf70c41e2013-03-21 23:45:03 +00001291 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1292 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001293 return true;
1294 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001295
1296 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001297 if ((CN->getValueType(0) == MVT::i32 ||
1298 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1299 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001300 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001301
Chris Lattnera801fced2006-11-08 02:15:41 +00001302 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001303 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001304
Owen Anderson9f944592009-08-11 20:47:22 +00001305 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1306 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001307 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001308 return true;
1309 }
1310 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001311
Chris Lattnera801fced2006-11-08 02:15:41 +00001312 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001313 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001314 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001315 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1316 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001317 Base = N;
1318 return true; // [r+0]
1319}
1320
1321/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1322/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001323bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1324 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001325 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001326 // Check to see if we can easily represent this as an [r+r] address. This
1327 // will fail if it thinks that the address is more profitably represented as
1328 // reg+imm, e.g. where imm = 0.
1329 if (SelectAddressRegReg(N, Base, Index, DAG))
1330 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001331
Chris Lattnera801fced2006-11-08 02:15:41 +00001332 // If the operand is an addition, always emit this as [r+r], since this is
1333 // better (for code size, and execution, as the memop does the add for free)
1334 // than emitting an explicit add.
1335 if (N.getOpcode() == ISD::ADD) {
1336 Base = N.getOperand(0);
1337 Index = N.getOperand(1);
1338 return true;
1339 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001340
Chris Lattnera801fced2006-11-08 02:15:41 +00001341 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkelf70c41e2013-03-21 23:45:03 +00001342 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1343 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001344 Index = N;
1345 return true;
1346}
1347
Chris Lattnera801fced2006-11-08 02:15:41 +00001348/// getPreIndexedAddressParts - returns true by value, base pointer and
1349/// offset pointer and addressing mode by reference if the node's address
1350/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001351bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1352 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001353 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001354 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001355 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001356
Ulrich Weigande90b0222013-03-22 14:58:48 +00001357 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001358 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001359 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001360 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001361 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1362 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001363 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001364 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001365 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001366 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001367 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001368 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001369 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001370 } else
1371 return false;
1372
Chris Lattner68371252006-11-14 01:38:31 +00001373 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands13237ac2008-06-06 12:08:01 +00001374 if (VT.isVector())
Chris Lattner68371252006-11-14 01:38:31 +00001375 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001376
Ulrich Weigande90b0222013-03-22 14:58:48 +00001377 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1378
1379 // Common code will reject creating a pre-inc form if the base pointer
1380 // is a frame index, or if N is a store and the base pointer is either
1381 // the same as or a predecessor of the value being stored. Check for
1382 // those situations here, and try with swapped Base/Offset instead.
1383 bool Swap = false;
1384
1385 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1386 Swap = true;
1387 else if (!isLoad) {
1388 SDValue Val = cast<StoreSDNode>(N)->getValue();
1389 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1390 Swap = true;
1391 }
1392
1393 if (Swap)
1394 std::swap(Base, Offset);
1395
Hal Finkelca542be2012-06-20 15:43:03 +00001396 AM = ISD::PRE_INC;
1397 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001398 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001399
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001400 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001401 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001402 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001403 return false;
1404 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001405 // LDU/STU need an address with at least 4-byte alignment.
1406 if (Alignment < 4)
1407 return false;
1408
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001409 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001410 return false;
1411 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001412
Chris Lattnerb314b152006-11-11 00:08:42 +00001413 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001414 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1415 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001416 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001417 LD->getExtensionType() == ISD::SEXTLOAD &&
1418 isa<ConstantSDNode>(Offset))
1419 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001420 }
1421
Chris Lattnerce645542006-11-10 02:08:47 +00001422 AM = ISD::PRE_INC;
1423 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001424}
1425
1426//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001427// LowerOperation implementation
1428//===----------------------------------------------------------------------===//
1429
Chris Lattneredb9d842010-11-15 02:46:57 +00001430/// GetLabelAccessInfo - Return true if we should reference labels using a
1431/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1432static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Chris Lattnerdd6df842010-11-15 03:13:19 +00001433 unsigned &LoOpFlags, const GlobalValue *GV = 0) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001434 HiOpFlags = PPCII::MO_HA;
1435 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001436
Chris Lattneredb9d842010-11-15 02:46:57 +00001437 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1438 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peck527da1b2010-11-23 03:31:01 +00001439 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattneredb9d842010-11-15 02:46:57 +00001440 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattnerdd6df842010-11-15 03:13:19 +00001441 if (isPIC) {
1442 HiOpFlags |= PPCII::MO_PIC_FLAG;
1443 LoOpFlags |= PPCII::MO_PIC_FLAG;
1444 }
1445
1446 // If this is a reference to a global value that requires a non-lazy-ptr, make
1447 // sure that instruction lowering adds it.
1448 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1449 HiOpFlags |= PPCII::MO_NLP_FLAG;
1450 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001451
Chris Lattnerdd6df842010-11-15 03:13:19 +00001452 if (GV->hasHiddenVisibility()) {
1453 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1454 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1455 }
1456 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001457
Chris Lattneredb9d842010-11-15 02:46:57 +00001458 return isPIC;
1459}
1460
1461static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1462 SelectionDAG &DAG) {
1463 EVT PtrVT = HiPart.getValueType();
1464 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001465 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001466
1467 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1468 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001469
Chris Lattneredb9d842010-11-15 02:46:57 +00001470 // With PIC, the first instruction is actually "GR+hi(&G)".
1471 if (isPIC)
1472 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1473 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001474
Chris Lattneredb9d842010-11-15 02:46:57 +00001475 // Generate non-pic code that has direct accesses to the constant pool.
1476 // The address of the global is just (hi(&g)+lo(&g)).
1477 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1478}
1479
Scott Michelcf0da6c2009-02-17 22:15:04 +00001480SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001481 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001482 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001483 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001484 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001485
Roman Divackyace47072012-08-24 16:26:02 +00001486 // 64-bit SVR4 ABI code is always position-independent.
1487 // The actual address of the GlobalValue is stored in the TOC.
1488 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1489 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001490 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001491 DAG.getRegister(PPC::X2, MVT::i64));
1492 }
1493
Chris Lattneredb9d842010-11-15 02:46:57 +00001494 unsigned MOHiFlag, MOLoFlag;
1495 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1496 SDValue CPIHi =
1497 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1498 SDValue CPILo =
1499 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1500 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001501}
1502
Dan Gohman21cea8a2010-04-17 15:26:15 +00001503SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001504 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001505 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001506
Roman Divackyace47072012-08-24 16:26:02 +00001507 // 64-bit SVR4 ABI code is always position-independent.
1508 // The actual address of the GlobalValue is stored in the TOC.
1509 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1510 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001511 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001512 DAG.getRegister(PPC::X2, MVT::i64));
1513 }
1514
Chris Lattneredb9d842010-11-15 02:46:57 +00001515 unsigned MOHiFlag, MOLoFlag;
1516 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1517 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1518 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1519 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001520}
1521
Dan Gohman21cea8a2010-04-17 15:26:15 +00001522SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1523 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001524 EVT PtrVT = Op.getValueType();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001525
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001526 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peck527da1b2010-11-23 03:31:01 +00001527
Chris Lattneredb9d842010-11-15 02:46:57 +00001528 unsigned MOHiFlag, MOLoFlag;
1529 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001530 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1531 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001532 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1533}
1534
Roman Divackye3f15c982012-06-04 17:36:38 +00001535SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1536 SelectionDAG &DAG) const {
1537
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001538 // FIXME: TLS addresses currently use medium model code sequences,
1539 // which is the most useful form. Eventually support for small and
1540 // large models could be added if users need it, at the cost of
1541 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001542 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001543 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001544 const GlobalValue *GV = GA->getGlobal();
1545 EVT PtrVT = getPointerTy();
1546 bool is64bit = PPCSubTarget.isPPC64();
1547
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001548 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001549
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001550 if (Model == TLSModel::LocalExec) {
1551 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001552 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001553 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001554 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001555 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1556 is64bit ? MVT::i64 : MVT::i32);
1557 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1558 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1559 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001560
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001561 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001562 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001563 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1564 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00001565 SDValue GOTPtr;
1566 if (is64bit) {
1567 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1568 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1569 PtrVT, GOTReg, TGA);
1570 } else
1571 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001572 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00001573 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001574 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001575 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001576
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001577 if (Model == TLSModel::GeneralDynamic) {
1578 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1579 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1580 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1581 GOTReg, TGA);
1582 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1583 GOTEntryHi, TGA);
1584
1585 // We need a chain node, and don't have one handy. The underlying
1586 // call has no side effects, so using the function entry node
1587 // suffices.
1588 SDValue Chain = DAG.getEntryNode();
1589 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1590 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1591 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1592 PtrVT, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001593 // The return value from GET_TLS_ADDR really is in X3 already, but
1594 // some hacks are needed here to tie everything together. The extra
1595 // copies dissolve during subsequent transforms.
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001596 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1597 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1598 }
1599
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001600 if (Model == TLSModel::LocalDynamic) {
1601 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1602 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1603 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1604 GOTReg, TGA);
1605 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1606 GOTEntryHi, TGA);
1607
1608 // We need a chain node, and don't have one handy. The underlying
1609 // call has no side effects, so using the function entry node
1610 // suffices.
1611 SDValue Chain = DAG.getEntryNode();
1612 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1613 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1614 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1615 PtrVT, ParmReg, TGA);
1616 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1617 // some hacks are needed here to tie everything together. The extra
1618 // copies dissolve during subsequent transforms.
1619 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1620 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt9ed4dbc2012-12-13 20:57:10 +00001621 Chain, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001622 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1623 }
1624
1625 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00001626}
1627
Chris Lattneredb9d842010-11-15 02:46:57 +00001628SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1629 SelectionDAG &DAG) const {
1630 EVT PtrVT = Op.getValueType();
1631 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001632 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00001633 const GlobalValue *GV = GSDN->getGlobal();
1634
Chris Lattneredb9d842010-11-15 02:46:57 +00001635 // 64-bit SVR4 ABI code is always position-independent.
1636 // The actual address of the GlobalValue is stored in the TOC.
1637 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1638 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1639 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1640 DAG.getRegister(PPC::X2, MVT::i64));
1641 }
1642
Chris Lattnerdd6df842010-11-15 03:13:19 +00001643 unsigned MOHiFlag, MOLoFlag;
1644 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00001645
Chris Lattnerdd6df842010-11-15 03:13:19 +00001646 SDValue GAHi =
1647 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1648 SDValue GALo =
1649 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00001650
Chris Lattnerdd6df842010-11-15 03:13:19 +00001651 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00001652
Chris Lattnerdd6df842010-11-15 03:13:19 +00001653 // If the global reference is actually to a non-lazy-pointer, we have to do an
1654 // extra load to get the address of the global.
1655 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1656 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001657 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00001658 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00001659}
1660
Dan Gohman21cea8a2010-04-17 15:26:15 +00001661SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00001662 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001663 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001664
Chris Lattner4211ca92006-04-14 06:01:58 +00001665 // If we're comparing for equality to zero, expose the fact that this is
1666 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1667 // fold the new nodes.
1668 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1669 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001670 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001671 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001672 if (VT.bitsLT(MVT::i32)) {
1673 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001674 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001675 }
Duncan Sands13237ac2008-06-06 12:08:01 +00001676 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001677 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1678 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00001679 DAG.getConstant(Log2b, MVT::i32));
1680 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00001681 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001682 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00001683 // optimized. FIXME: revisit this when we can custom lower all setcc
1684 // optimizations.
1685 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001686 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001687 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001688
Chris Lattner4211ca92006-04-14 06:01:58 +00001689 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00001690 // by xor'ing the rhs with the lhs, which is faster than setting a
1691 // condition register, reading it back out, and masking the correct bit. The
1692 // normal approach here uses sub to do this instead of xor. Using xor exposes
1693 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001694 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00001695 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001696 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001697 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00001698 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001699 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00001700 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001701 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001702}
1703
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001704SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001705 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00001706 SDNode *Node = Op.getNode();
1707 EVT VT = Node->getValueType(0);
1708 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1709 SDValue InChain = Node->getOperand(0);
1710 SDValue VAListPtr = Node->getOperand(1);
1711 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001712 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001713
Roman Divacky4394e682011-06-28 15:30:42 +00001714 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1715
1716 // gpr_index
1717 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1718 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1719 false, false, 0);
1720 InChain = GprIndex.getValue(1);
1721
1722 if (VT == MVT::i64) {
1723 // Check if GprIndex is even
1724 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1725 DAG.getConstant(1, MVT::i32));
1726 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1727 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1728 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1729 DAG.getConstant(1, MVT::i32));
1730 // Align GprIndex to be even if it isn't
1731 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1732 GprIndex);
1733 }
1734
1735 // fpr index is 1 byte after gpr
1736 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1737 DAG.getConstant(1, MVT::i32));
1738
1739 // fpr
1740 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1741 FprPtr, MachinePointerInfo(SV), MVT::i8,
1742 false, false, 0);
1743 InChain = FprIndex.getValue(1);
1744
1745 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1746 DAG.getConstant(8, MVT::i32));
1747
1748 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1749 DAG.getConstant(4, MVT::i32));
1750
1751 // areas
1752 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001753 MachinePointerInfo(), false, false,
1754 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001755 InChain = OverflowArea.getValue(1);
1756
1757 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001758 MachinePointerInfo(), false, false,
1759 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001760 InChain = RegSaveArea.getValue(1);
1761
1762 // select overflow_area if index > 8
1763 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1764 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1765
Roman Divacky4394e682011-06-28 15:30:42 +00001766 // adjustment constant gpr_index * 4/8
1767 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1768 VT.isInteger() ? GprIndex : FprIndex,
1769 DAG.getConstant(VT.isInteger() ? 4 : 8,
1770 MVT::i32));
1771
1772 // OurReg = RegSaveArea + RegConstant
1773 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1774 RegConstant);
1775
1776 // Floating types are 32 bytes into RegSaveArea
1777 if (VT.isFloatingPoint())
1778 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1779 DAG.getConstant(32, MVT::i32));
1780
1781 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1782 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1783 VT.isInteger() ? GprIndex : FprIndex,
1784 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1785 MVT::i32));
1786
1787 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1788 VT.isInteger() ? VAListPtr : FprPtr,
1789 MachinePointerInfo(SV),
1790 MVT::i8, false, false, 0);
1791
1792 // determine if we should load from reg_save_area or overflow_area
1793 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1794
1795 // increase overflow_area by 4/8 if gpr/fpr > 8
1796 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1797 DAG.getConstant(VT.isInteger() ? 4 : 8,
1798 MVT::i32));
1799
1800 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1801 OverflowAreaPlusN);
1802
1803 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1804 OverflowAreaPtr,
1805 MachinePointerInfo(),
1806 MVT::i32, false, false, 0);
1807
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00001808 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001809 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001810}
1811
Roman Divackyc3825df2013-07-25 21:36:47 +00001812SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
1813 const PPCSubtarget &Subtarget) const {
1814 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
1815
1816 // We have to copy the entire va_list struct:
1817 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
1818 return DAG.getMemcpy(Op.getOperand(0), Op,
1819 Op.getOperand(1), Op.getOperand(2),
1820 DAG.getConstant(12, MVT::i32), 8, false, true,
1821 MachinePointerInfo(), MachinePointerInfo());
1822}
1823
Duncan Sandsa0984362011-09-06 13:37:06 +00001824SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1825 SelectionDAG &DAG) const {
1826 return Op.getOperand(0);
1827}
1828
1829SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1830 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00001831 SDValue Chain = Op.getOperand(0);
1832 SDValue Trmp = Op.getOperand(1); // trampoline
1833 SDValue FPtr = Op.getOperand(2); // nested function
1834 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00001835 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00001836
Owen Anderson53aa7a92009-08-10 22:56:29 +00001837 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00001838 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00001839 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001840 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00001841 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00001842
Scott Michelcf0da6c2009-02-17 22:15:04 +00001843 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00001844 TargetLowering::ArgListEntry Entry;
1845
1846 Entry.Ty = IntPtrTy;
1847 Entry.Node = Trmp; Args.push_back(Entry);
1848
1849 // TrampSize == (isPPC64 ? 48 : 40);
1850 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00001851 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00001852 Args.push_back(Entry);
1853
1854 Entry.Node = FPtr; Args.push_back(Entry);
1855 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001856
Bill Wendling95e1af22008-09-17 00:30:57 +00001857 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Justin Holewinskiaa583972012-05-25 16:35:28 +00001858 TargetLowering::CallLoweringInfo CLI(Chain,
1859 Type::getVoidTy(*DAG.getContext()),
1860 false, false, false, false, 0,
1861 CallingConv::C,
Evan Cheng65f9d192012-02-28 18:51:51 +00001862 /*isTailCall=*/false,
Justin Holewinskiaa583972012-05-25 16:35:28 +00001863 /*doesNotRet=*/false,
1864 /*isReturnValueUsed=*/true,
Bill Wendling95e1af22008-09-17 00:30:57 +00001865 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00001866 Args, DAG, dl);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001867 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bill Wendling95e1af22008-09-17 00:30:57 +00001868
Duncan Sandsa0984362011-09-06 13:37:06 +00001869 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00001870}
1871
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001872SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001873 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001874 MachineFunction &MF = DAG.getMachineFunction();
1875 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1876
Andrew Trickef9de2a2013-05-25 02:42:55 +00001877 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001878
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001879 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001880 // vastart just stores the address of the VarArgsFrameIndex slot into the
1881 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001882 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00001883 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001884 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00001885 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1886 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00001887 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001888 }
1889
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001890 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001891 // We suppose the given va_list is already allocated.
1892 //
1893 // typedef struct {
1894 // char gpr; /* index into the array of 8 GPRs
1895 // * stored in the register save area
1896 // * gpr=0 corresponds to r3,
1897 // * gpr=1 to r4, etc.
1898 // */
1899 // char fpr; /* index into the array of 8 FPRs
1900 // * stored in the register save area
1901 // * fpr=0 corresponds to f1,
1902 // * fpr=1 to f2, etc.
1903 // */
1904 // char *overflow_arg_area;
1905 // /* location on stack that holds
1906 // * the next overflow argument
1907 // */
1908 // char *reg_save_area;
1909 // /* where r3:r10 and f1:f8 (if saved)
1910 // * are stored
1911 // */
1912 // } va_list[1];
1913
1914
Dan Gohman31ae5862010-04-17 14:41:14 +00001915 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1916 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001917
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001918
Owen Anderson53aa7a92009-08-10 22:56:29 +00001919 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001920
Dan Gohman31ae5862010-04-17 14:41:14 +00001921 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1922 PtrVT);
1923 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1924 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001925
Duncan Sands13237ac2008-06-06 12:08:01 +00001926 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001927 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001928
Duncan Sands13237ac2008-06-06 12:08:01 +00001929 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001930 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001931
1932 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001933 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001934
Dan Gohman2d489b52008-02-06 22:27:42 +00001935 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001936
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001937 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001938 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00001939 Op.getOperand(1),
1940 MachinePointerInfo(SV),
1941 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001942 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001943 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001944 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001945
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001946 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001947 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00001948 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1949 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00001950 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001951 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001952 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001953
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001954 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001955 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00001956 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1957 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001958 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001959 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001960 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001961
1962 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00001963 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1964 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001965 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001966
Chris Lattner4211ca92006-04-14 06:01:58 +00001967}
1968
Chris Lattner4f2e4e02007-03-06 00:59:59 +00001969#include "PPCGenCallingConv.inc"
1970
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001971// Function whose sole purpose is to kill compiler warnings
1972// stemming from unused functions included from PPCGenCallingConv.inc.
1973CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001974 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001975}
1976
Bill Schmidt230b4512013-06-12 16:39:22 +00001977bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1978 CCValAssign::LocInfo &LocInfo,
1979 ISD::ArgFlagsTy &ArgFlags,
1980 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001981 return true;
1982}
1983
Bill Schmidt230b4512013-06-12 16:39:22 +00001984bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
1985 MVT &LocVT,
1986 CCValAssign::LocInfo &LocInfo,
1987 ISD::ArgFlagsTy &ArgFlags,
1988 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00001989 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001990 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1991 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1992 };
1993 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00001994
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001995 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1996
1997 // Skip one register if the first unallocated register has an even register
1998 // number and there are still argument registers available which have not been
1999 // allocated yet. RegNum is actually an index into ArgRegs, which means we
2000 // need to skip a register if RegNum is odd.
2001 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
2002 State.AllocateReg(ArgRegs[RegNum]);
2003 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002004
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002005 // Always return false here, as this function only makes sure that the first
2006 // unallocated register has an odd register number and does not actually
2007 // allocate a register for the current argument.
2008 return false;
2009}
2010
Bill Schmidt230b4512013-06-12 16:39:22 +00002011bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
2012 MVT &LocVT,
2013 CCValAssign::LocInfo &LocInfo,
2014 ISD::ArgFlagsTy &ArgFlags,
2015 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00002016 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002017 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2018 PPC::F8
2019 };
2020
2021 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002022
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002023 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
2024
2025 // If there is only one Floating-point register left we need to put both f64
2026 // values of a split ppc_fp128 value on the stack.
2027 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
2028 State.AllocateReg(ArgRegs[RegNum]);
2029 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002030
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002031 // Always return false here, as this function only makes sure that the two f64
2032 // values a ppc_fp128 value is split into are both passed in registers or both
2033 // passed on the stack and does not actually allocate a register for the
2034 // current argument.
2035 return false;
2036}
2037
Chris Lattner43df5b32007-02-25 05:34:32 +00002038/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002039/// on Darwin.
Craig Topperca658c22012-03-11 07:16:55 +00002040static const uint16_t *GetFPR() {
2041 static const uint16_t FPR[] = {
Chris Lattner43df5b32007-02-25 05:34:32 +00002042 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002043 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner43df5b32007-02-25 05:34:32 +00002044 };
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002045
Chris Lattner43df5b32007-02-25 05:34:32 +00002046 return FPR;
2047}
2048
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002049/// CalculateStackSlotSize - Calculates the size reserved for this argument on
2050/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002051static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002052 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00002053 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002054 if (Flags.isByVal())
2055 ArgSize = Flags.getByValSize();
2056 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2057
2058 return ArgSize;
2059}
2060
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002061SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002062PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002063 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002064 const SmallVectorImpl<ISD::InputArg>
2065 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002066 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002067 SmallVectorImpl<SDValue> &InVals)
2068 const {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002069 if (PPCSubTarget.isSVR4ABI()) {
2070 if (PPCSubTarget.isPPC64())
2071 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
2072 dl, DAG, InVals);
2073 else
2074 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2075 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002076 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002077 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2078 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002079 }
2080}
2081
2082SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002083PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002084 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002085 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002086 const SmallVectorImpl<ISD::InputArg>
2087 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002088 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002089 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002090
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002091 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002092 // +-----------------------------------+
2093 // +--> | Back chain |
2094 // | +-----------------------------------+
2095 // | | Floating-point register save area |
2096 // | +-----------------------------------+
2097 // | | General register save area |
2098 // | +-----------------------------------+
2099 // | | CR save word |
2100 // | +-----------------------------------+
2101 // | | VRSAVE save word |
2102 // | +-----------------------------------+
2103 // | | Alignment padding |
2104 // | +-----------------------------------+
2105 // | | Vector register save area |
2106 // | +-----------------------------------+
2107 // | | Local variable space |
2108 // | +-----------------------------------+
2109 // | | Parameter list area |
2110 // | +-----------------------------------+
2111 // | | LR save word |
2112 // | +-----------------------------------+
2113 // SP--> +--- | Back chain |
2114 // +-----------------------------------+
2115 //
2116 // Specifications:
2117 // System V Application Binary Interface PowerPC Processor Supplement
2118 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002119
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002120 MachineFunction &MF = DAG.getMachineFunction();
2121 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002122 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002123
Owen Anderson53aa7a92009-08-10 22:56:29 +00002124 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002125 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002126 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2127 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002128 unsigned PtrByteSize = 4;
2129
2130 // Assign locations to all of the incoming arguments.
2131 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002132 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002133 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002134
2135 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002136 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002137
Bill Schmidtef17c142013-02-06 17:33:58 +00002138 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002139
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002140 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2141 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002142
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002143 // Arguments stored in registers.
2144 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002145 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002146 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002147
Owen Anderson9f944592009-08-11 20:47:22 +00002148 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002149 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002150 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002151 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002152 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002153 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002154 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002155 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00002156 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002157 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002158 case MVT::f64:
Hal Finkel19be5062014-03-29 05:29:01 +00002159 if (PPCSubTarget.hasVSX())
2160 RC = &PPC::VSFRCRegClass;
2161 else
2162 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002163 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002164 case MVT::v16i8:
2165 case MVT::v8i16:
2166 case MVT::v4i32:
2167 case MVT::v4f32:
Hal Finkel7811c612014-03-28 19:58:11 +00002168 RC = &PPC::VRRCRegClass;
2169 break;
Hal Finkel27774d92014-03-13 07:58:58 +00002170 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002171 case MVT::v2i64:
Hal Finkel7811c612014-03-28 19:58:11 +00002172 RC = &PPC::VSHRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002173 break;
2174 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002175
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002176 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002177 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002178 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2179 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2180
2181 if (ValVT == MVT::i1)
2182 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002183
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002184 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002185 } else {
2186 // Argument stored in memory.
2187 assert(VA.isMemLoc());
2188
Hal Finkel940ab932014-02-28 00:27:01 +00002189 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002190 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002191 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002192
2193 // Create load nodes to retrieve arguments from the stack.
2194 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002195 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2196 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002197 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002198 }
2199 }
2200
2201 // Assign locations to all of the incoming aggregate by value arguments.
2202 // Aggregates passed by value are stored in the local variable space of the
2203 // caller's stack frame, right above the parameter list area.
2204 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002205 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002206 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002207
2208 // Reserve stack space for the allocations in CCInfo.
2209 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2210
Bill Schmidtef17c142013-02-06 17:33:58 +00002211 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002212
2213 // Area that is at least reserved in the caller of this function.
2214 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00002215
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002216 // Set the size that is at least reserved in caller of this function. Tail
2217 // call optimized function's reserved stack space needs to be aligned so that
2218 // taking the difference between two stack areas will result in an aligned
2219 // stack.
2220 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2221
2222 MinReservedArea =
2223 std::max(MinReservedArea,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002224 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peck527da1b2010-11-23 03:31:01 +00002225
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002226 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002227 getStackAlignment();
2228 unsigned AlignMask = TargetAlign-1;
2229 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peck527da1b2010-11-23 03:31:01 +00002230
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002231 FI->setMinReservedArea(MinReservedArea);
2232
2233 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002234
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002235 // If the function takes variable number of arguments, make a frame index for
2236 // the start of the first vararg value... for expansion of llvm.va_start.
2237 if (isVarArg) {
Craig Topperbef78fc2012-03-11 07:57:25 +00002238 static const uint16_t GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002239 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2240 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2241 };
2242 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2243
Craig Topperbef78fc2012-03-11 07:57:25 +00002244 static const uint16_t FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002245 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2246 PPC::F8
2247 };
2248 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2249
Dan Gohman31ae5862010-04-17 14:41:14 +00002250 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2251 NumGPArgRegs));
2252 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2253 NumFPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002254
2255 // Make room for NumGPArgRegs and NumFPArgRegs.
2256 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson9f944592009-08-11 20:47:22 +00002257 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002258
Dan Gohman31ae5862010-04-17 14:41:14 +00002259 FuncInfo->setVarArgsStackOffset(
2260 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002261 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002262
Dan Gohman31ae5862010-04-17 14:41:14 +00002263 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2264 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002265
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002266 // The fixed integer arguments of a variadic function are stored to the
2267 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2268 // the result of va_next.
2269 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2270 // Get an existing live-in vreg, or add a new one.
2271 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2272 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002273 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002274
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002275 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002276 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2277 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002278 MemOps.push_back(Store);
2279 // Increment the address by four for the next argument to store
2280 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2281 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2282 }
2283
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002284 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2285 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002286 // The double arguments are stored to the VarArgsFrameIndex
2287 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002288 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2289 // Get an existing live-in vreg, or add a new one.
2290 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2291 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002292 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002293
Owen Anderson9f944592009-08-11 20:47:22 +00002294 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002295 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2296 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002297 MemOps.push_back(Store);
2298 // Increment the address by eight for the next argument to store
Owen Anderson9f944592009-08-11 20:47:22 +00002299 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002300 PtrVT);
2301 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2302 }
2303 }
2304
2305 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002306 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002307 MVT::Other, &MemOps[0], MemOps.size());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002308
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002309 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002310}
2311
Bill Schmidt57d6de52012-10-23 15:51:16 +00002312// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2313// value to MVT::i64 and then truncate to the correct register size.
2314SDValue
2315PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2316 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002317 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002318 if (Flags.isSExt())
2319 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2320 DAG.getValueType(ObjectVT));
2321 else if (Flags.isZExt())
2322 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2323 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002324
Hal Finkel940ab932014-02-28 00:27:01 +00002325 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002326}
2327
2328// Set the size that is at least reserved in caller of this function. Tail
2329// call optimized functions' reserved stack space needs to be aligned so that
2330// taking the difference between two stack areas will result in an aligned
2331// stack.
2332void
2333PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2334 unsigned nAltivecParamsAtEnd,
2335 unsigned MinReservedArea,
2336 bool isPPC64) const {
2337 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2338 // Add the Altivec parameters at the end, if needed.
2339 if (nAltivecParamsAtEnd) {
2340 MinReservedArea = ((MinReservedArea+15)/16)*16;
2341 MinReservedArea += 16*nAltivecParamsAtEnd;
2342 }
2343 MinReservedArea =
2344 std::max(MinReservedArea,
2345 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2346 unsigned TargetAlign
2347 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2348 getStackAlignment();
2349 unsigned AlignMask = TargetAlign-1;
2350 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2351 FI->setMinReservedArea(MinReservedArea);
2352}
2353
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002354SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002355PPCTargetLowering::LowerFormalArguments_64SVR4(
2356 SDValue Chain,
2357 CallingConv::ID CallConv, bool isVarArg,
2358 const SmallVectorImpl<ISD::InputArg>
2359 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002360 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002361 SmallVectorImpl<SDValue> &InVals) const {
2362 // TODO: add description of PPC stack frame format, or at least some docs.
2363 //
2364 MachineFunction &MF = DAG.getMachineFunction();
2365 MachineFrameInfo *MFI = MF.getFrameInfo();
2366 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2367
2368 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2369 // Potential tail calls could cause overwriting of argument stack slots.
2370 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2371 (CallConv == CallingConv::Fast));
2372 unsigned PtrByteSize = 8;
2373
2374 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2375 // Area that is at least reserved in caller of this function.
2376 unsigned MinReservedArea = ArgOffset;
2377
2378 static const uint16_t GPR[] = {
2379 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2380 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2381 };
2382
2383 static const uint16_t *FPR = GetFPR();
2384
2385 static const uint16_t VR[] = {
2386 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2387 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2388 };
Hal Finkel7811c612014-03-28 19:58:11 +00002389 static const uint16_t VSRH[] = {
2390 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
2391 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
2392 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002393
2394 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2395 const unsigned Num_FPR_Regs = 13;
2396 const unsigned Num_VR_Regs = array_lengthof(VR);
2397
2398 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2399
2400 // Add DAG nodes to load the arguments or copy them out of registers. On
2401 // entry to a function on PPC, the arguments start after the linkage area,
2402 // although the first ones are often in registers.
2403
2404 SmallVector<SDValue, 8> MemOps;
2405 unsigned nAltivecParamsAtEnd = 0;
2406 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002407 unsigned CurArgIdx = 0;
2408 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002409 SDValue ArgVal;
2410 bool needsLoad = false;
2411 EVT ObjectVT = Ins[ArgNo].VT;
Hal Finkel940ab932014-02-28 00:27:01 +00002412 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002413 unsigned ArgSize = ObjSize;
2414 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt6631e942013-02-20 17:31:41 +00002415 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2416 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002417
2418 unsigned CurArgOffset = ArgOffset;
2419
2420 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2421 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
Hal Finkel27774d92014-03-13 07:58:58 +00002422 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8 ||
Hal Finkela6c8b512014-03-26 16:12:58 +00002423 ObjectVT==MVT::v2f64 || ObjectVT==MVT::v2i64) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002424 if (isVarArg) {
2425 MinReservedArea = ((MinReservedArea+15)/16)*16;
2426 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2427 Flags,
2428 PtrByteSize);
2429 } else
2430 nAltivecParamsAtEnd++;
2431 } else
2432 // Calculate min reserved area.
2433 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2434 Flags,
2435 PtrByteSize);
2436
2437 // FIXME the codegen can be much improved in some cases.
2438 // We do not have to keep everything in memory.
2439 if (Flags.isByVal()) {
2440 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2441 ObjSize = Flags.getByValSize();
2442 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002443 // Empty aggregate parameters do not take up registers. Examples:
2444 // struct { } a;
2445 // union { } b;
2446 // int c[0];
2447 // etc. However, we have to provide a place-holder in InVals, so
2448 // pretend we have an 8-byte item at the current address for that
2449 // purpose.
2450 if (!ObjSize) {
2451 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2452 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2453 InVals.push_back(FIN);
2454 continue;
2455 }
Hal Finkel262a2242013-09-12 23:20:06 +00002456
2457 unsigned BVAlign = Flags.getByValAlign();
2458 if (BVAlign > 8) {
2459 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
2460 CurArgOffset = ArgOffset;
2461 }
2462
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002463 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt48081ca2012-10-16 13:30:53 +00002464 if (ObjSize < PtrByteSize)
2465 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002466 // The value of the object is its address.
2467 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2468 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2469 InVals.push_back(FIN);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002470
2471 if (ObjSize < 8) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002472 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002473 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002474 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002475 SDValue Store;
2476
2477 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2478 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2479 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2480 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002481 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002482 ObjType, false, false, 0);
2483 } else {
2484 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2485 // store the whole register as-is to the parameter save area
2486 // slot. The address of the parameter was already calculated
2487 // above (InVals.push_back(FIN)) to be the right-justified
2488 // offset within the slot. For this store, we need a new
2489 // frame index that points at the beginning of the slot.
2490 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2491 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2492 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002493 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002494 false, false, 0);
2495 }
2496
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002497 MemOps.push_back(Store);
2498 ++GPR_idx;
2499 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002500 // Whether we copied from a register or not, advance the offset
2501 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002502 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002503 continue;
2504 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002505
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002506 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2507 // Store whatever pieces of the object are in registers
2508 // to memory. ArgOffset will be the address of the beginning
2509 // of the object.
2510 if (GPR_idx != Num_GPR_Regs) {
2511 unsigned VReg;
2512 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2513 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2514 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2515 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002516 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002517 MachinePointerInfo(FuncArg, j),
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002518 false, false, 0);
2519 MemOps.push_back(Store);
2520 ++GPR_idx;
2521 ArgOffset += PtrByteSize;
2522 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00002523 ArgOffset += ArgSize - j;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002524 break;
2525 }
2526 }
2527 continue;
2528 }
2529
2530 switch (ObjectVT.getSimpleVT().SimpleTy) {
2531 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00002532 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002533 case MVT::i32:
2534 case MVT::i64:
2535 if (GPR_idx != Num_GPR_Regs) {
2536 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2537 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2538
Hal Finkel940ab932014-02-28 00:27:01 +00002539 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002540 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2541 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002542 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002543
2544 ++GPR_idx;
2545 } else {
2546 needsLoad = true;
2547 ArgSize = PtrByteSize;
2548 }
2549 ArgOffset += 8;
2550 break;
2551
2552 case MVT::f32:
2553 case MVT::f64:
2554 // Every 8 bytes of argument space consumes one of the GPRs available for
2555 // argument passing.
2556 if (GPR_idx != Num_GPR_Regs) {
2557 ++GPR_idx;
2558 }
2559 if (FPR_idx != Num_FPR_Regs) {
2560 unsigned VReg;
2561
2562 if (ObjectVT == MVT::f32)
2563 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2564 else
Hal Finkel19be5062014-03-29 05:29:01 +00002565 VReg = MF.addLiveIn(FPR[FPR_idx], PPCSubTarget.hasVSX() ?
2566 &PPC::VSFRCRegClass :
2567 &PPC::F8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002568
2569 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2570 ++FPR_idx;
2571 } else {
2572 needsLoad = true;
Bill Schmidt22162472012-10-11 15:38:20 +00002573 ArgSize = PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002574 }
2575
2576 ArgOffset += 8;
2577 break;
2578 case MVT::v4f32:
2579 case MVT::v4i32:
2580 case MVT::v8i16:
2581 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00002582 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002583 case MVT::v2i64:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002584 // Note that vector arguments in registers don't reserve stack space,
2585 // except in varargs functions.
2586 if (VR_idx != Num_VR_Regs) {
Hal Finkel7811c612014-03-28 19:58:11 +00002587 unsigned VReg = (ObjectVT == MVT::v2f64 || ObjectVT == MVT::v2i64) ?
2588 MF.addLiveIn(VSRH[VR_idx], &PPC::VSHRCRegClass) :
2589 MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002590 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2591 if (isVarArg) {
2592 while ((ArgOffset % 16) != 0) {
2593 ArgOffset += PtrByteSize;
2594 if (GPR_idx != Num_GPR_Regs)
2595 GPR_idx++;
2596 }
2597 ArgOffset += 16;
2598 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2599 }
2600 ++VR_idx;
2601 } else {
2602 // Vectors are aligned.
2603 ArgOffset = ((ArgOffset+15)/16)*16;
2604 CurArgOffset = ArgOffset;
2605 ArgOffset += 16;
2606 needsLoad = true;
2607 }
2608 break;
2609 }
2610
2611 // We need to load the argument to a virtual register if we determined
2612 // above that we ran out of physical registers of the appropriate type.
2613 if (needsLoad) {
2614 int FI = MFI->CreateFixedObject(ObjSize,
2615 CurArgOffset + (ArgSize - ObjSize),
2616 isImmutable);
2617 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2618 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2619 false, false, false, 0);
2620 }
2621
2622 InVals.push_back(ArgVal);
2623 }
2624
2625 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002626 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002627 // taking the difference between two stack areas will result in an aligned
2628 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002629 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002630
2631 // If the function takes variable number of arguments, make a frame index for
2632 // the start of the first vararg value... for expansion of llvm.va_start.
2633 if (isVarArg) {
2634 int Depth = ArgOffset;
2635
2636 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00002637 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002638 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2639
2640 // If this function is vararg, store any remaining integer argument regs
2641 // to their spots on the stack so that they may be loaded by deferencing the
2642 // result of va_next.
2643 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2644 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2645 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2646 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2647 MachinePointerInfo(), false, false, 0);
2648 MemOps.push_back(Store);
2649 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00002650 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002651 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2652 }
2653 }
2654
2655 if (!MemOps.empty())
2656 Chain = DAG.getNode(ISD::TokenFactor, dl,
2657 MVT::Other, &MemOps[0], MemOps.size());
2658
2659 return Chain;
2660}
2661
2662SDValue
2663PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002664 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002665 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002666 const SmallVectorImpl<ISD::InputArg>
2667 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002668 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002669 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002670 // TODO: add description of PPC stack frame format, or at least some docs.
2671 //
2672 MachineFunction &MF = DAG.getMachineFunction();
2673 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002674 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002675
Owen Anderson53aa7a92009-08-10 22:56:29 +00002676 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002677 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002678 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002679 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2680 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00002681 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey48850c12006-11-16 22:43:37 +00002682
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002683 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002684 // Area that is at least reserved in caller of this function.
2685 unsigned MinReservedArea = ArgOffset;
2686
Craig Topperca658c22012-03-11 07:16:55 +00002687 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002688 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2689 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2690 };
Craig Topperca658c22012-03-11 07:16:55 +00002691 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00002692 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2693 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2694 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00002695
Craig Topperca658c22012-03-11 07:16:55 +00002696 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002697
Craig Topperca658c22012-03-11 07:16:55 +00002698 static const uint16_t VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002699 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2700 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2701 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00002702
Owen Andersone2f23a32007-09-07 04:06:50 +00002703 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002704 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00002705 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00002706
2707 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002708
Craig Topperca658c22012-03-11 07:16:55 +00002709 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002710
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002711 // In 32-bit non-varargs functions, the stack space for vectors is after the
2712 // stack space for non-vectors. We do not use this space unless we have
2713 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00002714 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002715 // that out...for the pathological case, compute VecArgOffset as the
2716 // start of the vector parameter area. Computing VecArgOffset is the
2717 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002718 unsigned VecArgOffset = ArgOffset;
2719 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002720 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002721 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002722 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002723 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002724
Duncan Sandsd97eea32008-03-21 09:14:45 +00002725 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002726 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00002727 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002728 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002729 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2730 VecArgOffset += ArgSize;
2731 continue;
2732 }
2733
Owen Anderson9f944592009-08-11 20:47:22 +00002734 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002735 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002736 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002737 case MVT::i32:
2738 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002739 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002740 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002741 case MVT::i64: // PPC64
2742 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002743 // FIXME: We are guaranteed to be !isPPC64 at this point.
2744 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002745 VecArgOffset += 8;
2746 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002747 case MVT::v4f32:
2748 case MVT::v4i32:
2749 case MVT::v8i16:
2750 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002751 // Nothing to do, we're only looking at Nonvector args here.
2752 break;
2753 }
2754 }
2755 }
2756 // We've found where the vector parameter area in memory is. Skip the
2757 // first 12 parameters; these don't use that memory.
2758 VecArgOffset = ((VecArgOffset+15)/16)*16;
2759 VecArgOffset += 12*16;
2760
Chris Lattner4302e8f2006-05-16 18:18:50 +00002761 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00002762 // entry to a function on PPC, the arguments start after the linkage area,
2763 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00002764
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002765 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002766 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00002767 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002768 unsigned CurArgIdx = 0;
2769 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002770 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002771 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002772 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00002773 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00002774 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002775 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002776 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2777 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002778
Chris Lattner318f0d22006-05-16 18:51:52 +00002779 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002780
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002781 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002782 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2783 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002784 if (isVarArg || isPPC64) {
2785 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002786 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002787 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002788 PtrByteSize);
2789 } else nAltivecParamsAtEnd++;
2790 } else
2791 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002792 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002793 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002794 PtrByteSize);
2795
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002796 // FIXME the codegen can be much improved in some cases.
2797 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002798 if (Flags.isByVal()) {
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002799 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002800 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002801 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002802 // Objects of size 1 and 2 are right justified, everything else is
2803 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00002804 if (ObjSize==1 || ObjSize==2) {
2805 CurArgOffset = CurArgOffset + (4 - ObjSize);
2806 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002807 // The value of the object is its address.
Evan Cheng0664a672010-07-03 00:40:23 +00002808 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002809 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002810 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002811 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00002812 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002813 unsigned VReg;
2814 if (isPPC64)
2815 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2816 else
2817 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002818 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002819 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002820 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002821 MachinePointerInfo(FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002822 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00002823 MemOps.push_back(Store);
2824 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00002825 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002826
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002827 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00002828
Dale Johannesen21a8f142008-03-08 01:41:42 +00002829 continue;
2830 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002831 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2832 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002833 // to memory. ArgOffset will be the address of the beginning
2834 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002835 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002836 unsigned VReg;
2837 if (isPPC64)
2838 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2839 else
2840 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00002841 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002842 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002843 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002844 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002845 MachinePointerInfo(FuncArg, j),
David Greene87a5abe2010-02-15 16:56:53 +00002846 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002847 MemOps.push_back(Store);
2848 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002849 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002850 } else {
2851 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2852 break;
2853 }
2854 }
2855 continue;
2856 }
2857
Owen Anderson9f944592009-08-11 20:47:22 +00002858 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002859 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002860 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002861 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00002862 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00002863 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002864 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002865 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Hal Finkel7f908e82014-03-06 00:45:19 +00002866
2867 if (ObjectVT == MVT::i1)
2868 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal);
2869
Bill Wendling968f32c2008-03-07 20:49:02 +00002870 ++GPR_idx;
2871 } else {
2872 needsLoad = true;
2873 ArgSize = PtrByteSize;
2874 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002875 // All int arguments reserve stack space in the Darwin ABI.
2876 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00002877 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002878 }
Bill Wendling968f32c2008-03-07 20:49:02 +00002879 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00002880 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00002881 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002882 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002883 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00002884
Hal Finkel940ab932014-02-28 00:27:01 +00002885 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00002886 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00002887 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002888 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00002889
Chris Lattnerec78cad2006-06-26 22:48:35 +00002890 ++GPR_idx;
2891 } else {
2892 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00002893 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002894 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002895 // All int arguments reserve stack space in the Darwin ABI.
2896 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002897 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002898
Owen Anderson9f944592009-08-11 20:47:22 +00002899 case MVT::f32:
2900 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00002901 // Every 4 bytes of argument space consumes one of the GPRs available for
2902 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002903 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002904 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00002905 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002906 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00002907 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002908 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002909 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002910
Owen Anderson9f944592009-08-11 20:47:22 +00002911 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00002912 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002913 else
Devang Patelf3292b22011-02-21 23:21:26 +00002914 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002915
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002916 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002917 ++FPR_idx;
2918 } else {
2919 needsLoad = true;
2920 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002921
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002922 // All FP arguments reserve stack space in the Darwin ABI.
2923 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002924 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002925 case MVT::v4f32:
2926 case MVT::v4i32:
2927 case MVT::v8i16:
2928 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00002929 // Note that vector arguments in registers don't reserve stack space,
2930 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002931 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002932 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002933 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00002934 if (isVarArg) {
2935 while ((ArgOffset % 16) != 0) {
2936 ArgOffset += PtrByteSize;
2937 if (GPR_idx != Num_GPR_Regs)
2938 GPR_idx++;
2939 }
2940 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002941 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00002942 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002943 ++VR_idx;
2944 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002945 if (!isVarArg && !isPPC64) {
2946 // Vectors go after all the nonvectors.
2947 CurArgOffset = VecArgOffset;
2948 VecArgOffset += 16;
2949 } else {
2950 // Vectors are aligned.
2951 ArgOffset = ((ArgOffset+15)/16)*16;
2952 CurArgOffset = ArgOffset;
2953 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00002954 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002955 needsLoad = true;
2956 }
2957 break;
2958 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002959
Chris Lattner4302e8f2006-05-16 18:18:50 +00002960 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002961 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002962 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002963 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002964 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00002965 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002966 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002967 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002968 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002969 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002970
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002971 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002972 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002973
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002974 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002975 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002976 // taking the difference between two stack areas will result in an aligned
2977 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002978 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002979
Chris Lattner4302e8f2006-05-16 18:18:50 +00002980 // If the function takes variable number of arguments, make a frame index for
2981 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002982 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002983 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002984
Dan Gohman31ae5862010-04-17 14:41:14 +00002985 FuncInfo->setVarArgsFrameIndex(
2986 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002987 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00002988 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002989
Chris Lattner4302e8f2006-05-16 18:18:50 +00002990 // If this function is vararg, store any remaining integer argument regs
2991 // to their spots on the stack so that they may be loaded by deferencing the
2992 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002993 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00002994 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00002995
Chris Lattner2cca3852006-11-18 01:57:19 +00002996 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00002997 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00002998 else
Devang Patelf3292b22011-02-21 23:21:26 +00002999 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003000
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003001 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00003002 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3003 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003004 MemOps.push_back(Store);
3005 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003006 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00003007 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003008 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003009 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003010
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003011 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003012 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00003013 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003014
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003015 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003016}
3017
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003018/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
3019/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003020static unsigned
3021CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
3022 bool isPPC64,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003023 bool isVarArg,
3024 unsigned CC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003025 const SmallVectorImpl<ISD::OutputArg>
3026 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003027 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003028 unsigned &nAltivecParamsAtEnd) {
3029 // Count how many bytes are to be pushed on the stack, including the linkage
3030 // area, and parameter passing area. We start with 24/48 bytes, which is
3031 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003032 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003033 unsigned NumOps = Outs.size();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003034 unsigned PtrByteSize = isPPC64 ? 8 : 4;
3035
3036 // Add up all the space actually used.
3037 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
3038 // they all go in registers, but we must reserve stack space for them for
3039 // possible use by the caller. In varargs or 64-bit calls, parameters are
3040 // assigned stack space in order, with padding so Altivec parameters are
3041 // 16-byte aligned.
3042 nAltivecParamsAtEnd = 0;
3043 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003044 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003045 EVT ArgVT = Outs[i].VT;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003046 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00003047 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
Hal Finkel27774d92014-03-13 07:58:58 +00003048 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8 ||
Hal Finkela6c8b512014-03-26 16:12:58 +00003049 ArgVT==MVT::v2f64 || ArgVT==MVT::v2i64) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003050 if (!isVarArg && !isPPC64) {
3051 // Non-varargs Altivec parameters go after all the non-Altivec
3052 // parameters; handle those later so we know how much padding we need.
3053 nAltivecParamsAtEnd++;
3054 continue;
3055 }
3056 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
3057 NumBytes = ((NumBytes+15)/16)*16;
3058 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003059 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003060 }
3061
3062 // Allow for Altivec parameters at the end, if needed.
3063 if (nAltivecParamsAtEnd) {
3064 NumBytes = ((NumBytes+15)/16)*16;
3065 NumBytes += 16*nAltivecParamsAtEnd;
3066 }
3067
3068 // The prolog code of the callee may store up to 8 GPR argument registers to
3069 // the stack, allowing va_start to index over them in memory if its varargs.
3070 // Because we cannot tell if this is needed on the caller side, we have to
3071 // conservatively assume that it is needed. As such, make sure we have at
3072 // least enough stack space for the caller to store the 8 GPRs.
3073 NumBytes = std::max(NumBytes,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003074 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003075
3076 // Tail call needs the stack to be aligned.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003077 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
3078 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
3079 getFrameLowering()->getStackAlignment();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003080 unsigned AlignMask = TargetAlign-1;
3081 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
3082 }
3083
3084 return NumBytes;
3085}
3086
3087/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003088/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00003089static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003090 unsigned ParamSize) {
3091
Dale Johannesen86dcae12009-11-24 01:09:07 +00003092 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003093
3094 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
3095 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3096 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3097 // Remember only if the new adjustement is bigger.
3098 if (SPDiff < FI->getTailCallSPDelta())
3099 FI->setTailCallSPDelta(SPDiff);
3100
3101 return SPDiff;
3102}
3103
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003104/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3105/// for tail call optimization. Targets which want to do tail call
3106/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003107bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003108PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003109 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003110 bool isVarArg,
3111 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003112 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003113 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00003114 return false;
3115
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003116 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003117 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003118 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003119
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003120 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00003121 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003122 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
3123 // Functions containing by val parameters are not supported.
3124 for (unsigned i = 0; i != Ins.size(); i++) {
3125 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3126 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003127 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003128
Alp Tokerf907b892013-12-05 05:44:44 +00003129 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003130 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
3131 return true;
3132
3133 // At the moment we can only do local tail calls (in same module, hidden
3134 // or protected) if we are generating PIC.
3135 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
3136 return G->getGlobal()->hasHiddenVisibility()
3137 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003138 }
3139
3140 return false;
3141}
3142
Chris Lattnereb755fc2006-05-17 19:00:46 +00003143/// isCallCompatibleAddress - Return the immediate to use if the specified
3144/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003145static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00003146 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3147 if (!C) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003148
Dan Gohmaneffb8942008-09-12 16:56:44 +00003149 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003150 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00003151 SignExtend32<26>(Addr) != Addr)
Chris Lattnereb755fc2006-05-17 19:00:46 +00003152 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003153
Dan Gohmaneffb8942008-09-12 16:56:44 +00003154 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00003155 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003156}
3157
Dan Gohmand78c4002008-05-13 00:00:25 +00003158namespace {
3159
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003160struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003161 SDValue Arg;
3162 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003163 int FrameIdx;
3164
3165 TailCallArgumentInfo() : FrameIdx(0) {}
3166};
3167
Dan Gohmand78c4002008-05-13 00:00:25 +00003168}
3169
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003170/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3171static void
3172StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003173 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003174 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3175 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003176 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003177 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003178 SDValue Arg = TailCallArgs[i].Arg;
3179 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003180 int FI = TailCallArgs[i].FrameIdx;
3181 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003182 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003183 MachinePointerInfo::getFixedStack(FI),
3184 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003185 }
3186}
3187
3188/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3189/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003190static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003191 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003192 SDValue Chain,
3193 SDValue OldRetAddr,
3194 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003195 int SPDiff,
3196 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003197 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003198 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003199 if (SPDiff) {
3200 // Calculate the new stack slot for the return address.
3201 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003202 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003203 isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003204 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003205 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003206 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003207 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003208 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003209 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003210 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003211
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003212 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3213 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003214 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003215 int NewFPLoc =
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003216 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene1fbe0542009-11-12 20:49:22 +00003217 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003218 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003219 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3220 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003221 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003222 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003223 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003224 }
3225 return Chain;
3226}
3227
3228/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3229/// the position of the argument.
3230static void
3231CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003232 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003233 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003234 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003235 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003236 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003237 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003238 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003239 TailCallArgumentInfo Info;
3240 Info.Arg = Arg;
3241 Info.FrameIdxOp = FIN;
3242 Info.FrameIdx = FI;
3243 TailCallArguments.push_back(Info);
3244}
3245
3246/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3247/// stack slot. Returns the chain as result and the loaded frame pointers in
3248/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003249SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003250 int SPDiff,
3251 SDValue Chain,
3252 SDValue &LROpOut,
3253 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003254 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003255 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003256 if (SPDiff) {
3257 // Load the LR and FP stack slot for later adjusting.
Owen Anderson9f944592009-08-11 20:47:22 +00003258 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003259 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003260 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003261 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003262 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003263
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003264 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3265 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003266 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003267 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003268 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003269 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003270 Chain = SDValue(FPOpOut.getNode(), 1);
3271 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003272 }
3273 return Chain;
3274}
3275
Dale Johannesen85d41a12008-03-04 23:17:14 +00003276/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003277/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003278/// specified by the specific parameter attribute. The copy will be passed as
3279/// a byval function parameter.
3280/// Sometimes what we are copying is the end of a larger object, the part that
3281/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003282static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003283CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003284 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003285 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003286 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003287 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Chris Lattner2510de22010-09-21 05:40:29 +00003288 false, false, MachinePointerInfo(0),
3289 MachinePointerInfo(0));
Dale Johannesen85d41a12008-03-04 23:17:14 +00003290}
Chris Lattner43df5b32007-02-25 05:34:32 +00003291
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003292/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3293/// tail calls.
3294static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003295LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3296 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003297 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003298 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3299 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003300 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003301 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003302 if (!isTailCall) {
3303 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003304 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003305 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003306 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003307 else
Owen Anderson9f944592009-08-11 20:47:22 +00003308 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003309 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003310 DAG.getConstant(ArgOffset, PtrVT));
3311 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003312 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3313 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003314 // Calculate and remember argument location.
3315 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3316 TailCallArguments);
3317}
3318
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003319static
3320void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003321 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003322 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003323 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003324 MachineFunction &MF = DAG.getMachineFunction();
3325
3326 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3327 // might overwrite each other in case of tail call optimization.
3328 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003329 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003330 InFlag = SDValue();
3331 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3332 MemOpChains2, dl);
3333 if (!MemOpChains2.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003334 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003335 &MemOpChains2[0], MemOpChains2.size());
3336
3337 // Store the return address to the appropriate stack slot.
3338 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3339 isPPC64, isDarwinABI, dl);
3340
3341 // Emit callseq_end just before tailcall node.
3342 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003343 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003344 InFlag = Chain.getValue(1);
3345}
3346
3347static
3348unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003349 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003350 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3351 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003352 const PPCSubtarget &PPCSubTarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003353
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003354 bool isPPC64 = PPCSubTarget.isPPC64();
3355 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3356
Owen Anderson53aa7a92009-08-10 22:56:29 +00003357 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003358 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003359 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003360
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003361 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003362
Torok Edwin31e90d22010-08-04 20:47:44 +00003363 bool needIndirectCall = true;
3364 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003365 // If this is an absolute destination address, use the munged value.
3366 Callee = SDValue(Dest, 0);
Torok Edwin31e90d22010-08-04 20:47:44 +00003367 needIndirectCall = false;
3368 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003369
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003370 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3371 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3372 // Use indirect calls for ALL functions calls in JIT mode, since the
3373 // far-call stubs may be outside relocation limits for a BL instruction.
3374 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3375 unsigned OpFlags = 0;
3376 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003377 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003378 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003379 (G->getGlobal()->isDeclaration() ||
3380 G->getGlobal()->isWeakForLinker())) {
3381 // PC-relative references to external symbols should go through $stub,
3382 // unless we're building with the leopard linker or later, which
3383 // automatically synthesizes these stubs.
3384 OpFlags = PPCII::MO_DARWIN_STUB;
3385 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003386
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003387 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3388 // every direct call is) turn it into a TargetGlobalAddress /
3389 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin31e90d22010-08-04 20:47:44 +00003390 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003391 Callee.getValueType(),
3392 0, OpFlags);
Torok Edwin31e90d22010-08-04 20:47:44 +00003393 needIndirectCall = false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003394 }
Torok Edwin31e90d22010-08-04 20:47:44 +00003395 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003396
Torok Edwin31e90d22010-08-04 20:47:44 +00003397 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003398 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003399
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003400 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003401 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003402 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003403 // PC-relative references to external symbols should go through $stub,
3404 // unless we're building with the leopard linker or later, which
3405 // automatically synthesizes these stubs.
3406 OpFlags = PPCII::MO_DARWIN_STUB;
3407 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003408
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003409 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3410 OpFlags);
3411 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003412 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003413
Torok Edwin31e90d22010-08-04 20:47:44 +00003414 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003415 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3416 // to do the call, we can't use PPCISD::CALL.
3417 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003418
3419 if (isSVR4ABI && isPPC64) {
3420 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3421 // entry point, but to the function descriptor (the function entry point
3422 // address is part of the function descriptor though).
3423 // The function descriptor is a three doubleword structure with the
3424 // following fields: function entry point, TOC base address and
3425 // environment pointer.
3426 // Thus for a call through a function pointer, the following actions need
3427 // to be performed:
3428 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003429 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003430 // 2. Load the address of the function entry point from the function
3431 // descriptor.
3432 // 3. Load the TOC of the callee from the function descriptor into r2.
3433 // 4. Load the environment pointer from the function descriptor into
3434 // r11.
3435 // 5. Branch to the function entry point address.
3436 // 6. On return of the callee, the TOC of the caller needs to be
3437 // restored (this is done in FinishCall()).
3438 //
3439 // All those operations are flagged together to ensure that no other
3440 // operations can be scheduled in between. E.g. without flagging the
3441 // operations together, a TOC access in the caller could be scheduled
3442 // between the load of the callee TOC and the branch to the callee, which
3443 // results in the TOC access going through the TOC of the callee instead
3444 // of going through the TOC of the caller, which leads to incorrect code.
3445
3446 // Load the address of the function entry point from the function
3447 // descriptor.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003448 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003449 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, MTCTROps,
3450 InFlag.getNode() ? 3 : 2);
3451 Chain = LoadFuncPtr.getValue(1);
3452 InFlag = LoadFuncPtr.getValue(2);
3453
3454 // Load environment pointer into r11.
3455 // Offset of the environment pointer within the function descriptor.
3456 SDValue PtrOff = DAG.getIntPtrConstant(16);
3457
3458 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3459 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3460 InFlag);
3461 Chain = LoadEnvPtr.getValue(1);
3462 InFlag = LoadEnvPtr.getValue(2);
3463
3464 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3465 InFlag);
3466 Chain = EnvVal.getValue(0);
3467 InFlag = EnvVal.getValue(1);
3468
3469 // Load TOC of the callee into r2. We are using a target-specific load
3470 // with r2 hard coded, because the result of a target-independent load
3471 // would never go directly into r2, since r2 is a reserved register (which
3472 // prevents the register allocator from allocating it), resulting in an
3473 // additional register being allocated and an unnecessary move instruction
3474 // being generated.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003475 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003476 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3477 Callee, InFlag);
3478 Chain = LoadTOCPtr.getValue(0);
3479 InFlag = LoadTOCPtr.getValue(1);
3480
3481 MTCTROps[0] = Chain;
3482 MTCTROps[1] = LoadFuncPtr;
3483 MTCTROps[2] = InFlag;
3484 }
3485
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003486 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
3487 2 + (InFlag.getNode() != 0));
3488 InFlag = Chain.getValue(1);
3489
3490 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00003491 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003492 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003493 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003494 CallOpc = PPCISD::BCTRL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003495 Callee.setNode(0);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003496 // Add use of X11 (holding environment pointer)
3497 if (isSVR4ABI && isPPC64)
3498 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003499 // Add CTR register as callee so a bctr can be emitted later.
3500 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00003501 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003502 }
3503
3504 // If this is a direct call, pass the chain and the callee.
3505 if (Callee.getNode()) {
3506 Ops.push_back(Chain);
3507 Ops.push_back(Callee);
3508 }
3509 // If this is a tail call add stack pointer delta.
3510 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00003511 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003512
3513 // Add argument registers to the end of the list so that they are known live
3514 // into the call.
3515 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3516 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3517 RegsToPass[i].second.getValueType()));
3518
3519 return CallOpc;
3520}
3521
Roman Divacky76293062012-09-18 16:47:58 +00003522static
3523bool isLocalCall(const SDValue &Callee)
3524{
3525 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00003526 return !G->getGlobal()->isDeclaration() &&
3527 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00003528 return false;
3529}
3530
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003531SDValue
3532PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003533 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003534 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003535 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003536 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003537
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003538 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003539 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003540 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003541 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003542
3543 // Copy all of the result registers out of their specified physreg.
3544 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3545 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003546 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00003547
3548 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3549 VA.getLocReg(), VA.getLocVT(), InFlag);
3550 Chain = Val.getValue(1);
3551 InFlag = Val.getValue(2);
3552
3553 switch (VA.getLocInfo()) {
3554 default: llvm_unreachable("Unknown loc info!");
3555 case CCValAssign::Full: break;
3556 case CCValAssign::AExt:
3557 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3558 break;
3559 case CCValAssign::ZExt:
3560 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3561 DAG.getValueType(VA.getValVT()));
3562 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3563 break;
3564 case CCValAssign::SExt:
3565 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3566 DAG.getValueType(VA.getValVT()));
3567 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3568 break;
3569 }
3570
3571 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003572 }
3573
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003574 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003575}
3576
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003577SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00003578PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003579 bool isTailCall, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003580 SelectionDAG &DAG,
3581 SmallVector<std::pair<unsigned, SDValue>, 8>
3582 &RegsToPass,
3583 SDValue InFlag, SDValue Chain,
3584 SDValue &Callee,
3585 int SPDiff, unsigned NumBytes,
3586 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003587 SmallVectorImpl<SDValue> &InVals) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003588 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003589 SmallVector<SDValue, 8> Ops;
3590 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3591 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003592 PPCSubTarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003593
Hal Finkel5ab37802012-08-28 02:10:27 +00003594 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3595 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3596 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3597
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003598 // When performing tail call optimization the callee pops its arguments off
3599 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00003600 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003601 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003602 (CallConv == CallingConv::Fast &&
3603 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003604
Roman Divackyef21be22012-03-06 16:41:49 +00003605 // Add a register mask operand representing the call-preserved registers.
3606 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3607 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3608 assert(Mask && "Missing call preserved mask for calling convention");
3609 Ops.push_back(DAG.getRegisterMask(Mask));
3610
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003611 if (InFlag.getNode())
3612 Ops.push_back(InFlag);
3613
3614 // Emit tail call.
3615 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003616 assert(((Callee.getOpcode() == ISD::Register &&
3617 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3618 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3619 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3620 isa<ConstantSDNode>(Callee)) &&
3621 "Expecting an global address, external symbol, absolute value or register");
3622
Owen Anderson9f944592009-08-11 20:47:22 +00003623 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003624 }
3625
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003626 // Add a NOP immediately after the branch instruction when using the 64-bit
3627 // SVR4 ABI. At link time, if caller and callee are in a different module and
3628 // thus have a different TOC, the call will be replaced with a call to a stub
3629 // function which saves the current TOC, loads the TOC of the callee and
3630 // branches to the callee. The NOP will be replaced with a load instruction
3631 // which restores the TOC of the caller from the TOC save slot of the current
3632 // stack frame. If caller and callee belong to the same module (and have the
3633 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00003634
3635 bool needsTOCRestore = false;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003636 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003637 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003638 // This is a call through a function pointer.
3639 // Restore the caller TOC from the save area into R2.
3640 // See PrepareCall() for more information about calls through function
3641 // pointers in the 64-bit SVR4 ABI.
3642 // We are using a target-specific load with r2 hard coded, because the
3643 // result of a target-independent load would never go directly into r2,
3644 // since r2 is a reserved register (which prevents the register allocator
3645 // from allocating it), resulting in an additional register being
3646 // allocated and an unnecessary move instruction being generated.
Hal Finkel51861b42012-03-31 14:45:15 +00003647 needsTOCRestore = true;
Bill Schmidtcea15962013-09-26 17:09:28 +00003648 } else if ((CallOpc == PPCISD::CALL) &&
3649 (!isLocalCall(Callee) ||
3650 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Roman Divacky76293062012-09-18 16:47:58 +00003651 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003652 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller79fef932009-12-18 13:00:15 +00003653 }
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003654 }
3655
Hal Finkel51861b42012-03-31 14:45:15 +00003656 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
3657 InFlag = Chain.getValue(1);
3658
3659 if (needsTOCRestore) {
3660 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3661 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3662 InFlag = Chain.getValue(1);
3663 }
3664
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003665 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3666 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003667 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003668 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003669 InFlag = Chain.getValue(1);
3670
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003671 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3672 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003673}
3674
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003675SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00003676PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003677 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00003678 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00003679 SDLoc &dl = CLI.DL;
3680 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3681 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3682 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00003683 SDValue Chain = CLI.Chain;
3684 SDValue Callee = CLI.Callee;
3685 bool &isTailCall = CLI.IsTailCall;
3686 CallingConv::ID CallConv = CLI.CallConv;
3687 bool isVarArg = CLI.IsVarArg;
3688
Evan Cheng67a69dd2010-01-27 00:07:07 +00003689 if (isTailCall)
3690 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3691 Ins, DAG);
3692
Bill Schmidt57d6de52012-10-23 15:51:16 +00003693 if (PPCSubTarget.isSVR4ABI()) {
3694 if (PPCSubTarget.isPPC64())
3695 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3696 isTailCall, Outs, OutVals, Ins,
3697 dl, DAG, InVals);
3698 else
3699 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3700 isTailCall, Outs, OutVals, Ins,
3701 dl, DAG, InVals);
3702 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003703
Bill Schmidt57d6de52012-10-23 15:51:16 +00003704 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3705 isTailCall, Outs, OutVals, Ins,
3706 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003707}
3708
3709SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003710PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3711 CallingConv::ID CallConv, bool isVarArg,
3712 bool isTailCall,
3713 const SmallVectorImpl<ISD::OutputArg> &Outs,
3714 const SmallVectorImpl<SDValue> &OutVals,
3715 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003716 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003717 SmallVectorImpl<SDValue> &InVals) const {
3718 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003719 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003720
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003721 assert((CallConv == CallingConv::C ||
3722 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003723
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003724 unsigned PtrByteSize = 4;
3725
3726 MachineFunction &MF = DAG.getMachineFunction();
3727
3728 // Mark this function as potentially containing a function that contains a
3729 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3730 // and restoring the callers stack pointer in this functions epilog. This is
3731 // done because by tail calling the called function might overwrite the value
3732 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003733 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3734 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003735 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00003736
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003737 // Count how many bytes are to be pushed on the stack, including the linkage
3738 // area, parameter list area and the part of the local variable space which
3739 // contains copies of aggregates which are passed by value.
3740
3741 // Assign locations to all of the outgoing arguments.
3742 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003743 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003744 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003745
3746 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003747 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003748
3749 if (isVarArg) {
3750 // Handle fixed and variable vector arguments differently.
3751 // Fixed vector arguments go into registers as long as registers are
3752 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003753 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00003754
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003755 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003756 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003757 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003758 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00003759
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003760 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00003761 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3762 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003763 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00003764 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3765 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003766 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003767
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003768 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003769#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00003770 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00003771 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003772#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00003773 llvm_unreachable(0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003774 }
3775 }
3776 } else {
3777 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00003778 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003779 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003780
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003781 // Assign locations to all of the outgoing aggregate by value arguments.
3782 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003783 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003784 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003785
3786 // Reserve stack space for the allocations in CCInfo.
3787 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3788
Bill Schmidtef17c142013-02-06 17:33:58 +00003789 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003790
3791 // Size of the linkage area, parameter list area and the part of the local
3792 // space variable where copies of aggregates which are passed by value are
3793 // stored.
3794 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003795
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003796 // Calculate by how many bytes the stack has to be adjusted in case of tail
3797 // call optimization.
3798 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3799
3800 // Adjust the stack pointer for the new arguments...
3801 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003802 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3803 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003804 SDValue CallSeqStart = Chain;
3805
3806 // Load the return address and frame pointer so it can be moved somewhere else
3807 // later.
3808 SDValue LROp, FPOp;
3809 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3810 dl);
3811
3812 // Set up a copy of the stack pointer for use loading and storing any
3813 // arguments that may not fit in the registers available for argument
3814 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00003815 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00003816
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003817 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3818 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3819 SmallVector<SDValue, 8> MemOpChains;
3820
Roman Divacky71038e72011-08-30 17:04:16 +00003821 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003822 // Walk the register/memloc assignments, inserting copies/loads.
3823 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3824 i != e;
3825 ++i) {
3826 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003827 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003828 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00003829
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003830 if (Flags.isByVal()) {
3831 // Argument is an aggregate which is passed by value, thus we need to
3832 // create a copy of it in the local variable space of the current stack
3833 // frame (which is the stack frame of the caller) and pass the address of
3834 // this copy to the callee.
3835 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3836 CCValAssign &ByValVA = ByValArgLocs[j++];
3837 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00003838
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003839 // Memory reserved in the local variable space of the callers stack frame.
3840 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003841
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003842 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3843 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00003844
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003845 // Create a copy of the argument in the local area of the current
3846 // stack frame.
3847 SDValue MemcpyCall =
3848 CreateCopyOfByValArgument(Arg, PtrOff,
3849 CallSeqStart.getNode()->getOperand(0),
3850 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00003851
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003852 // This must go outside the CALLSEQ_START..END.
3853 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003854 CallSeqStart.getNode()->getOperand(1),
3855 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003856 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3857 NewCallSeqStart.getNode());
3858 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00003859
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003860 // Pass the address of the aggregate copy on the stack either in a
3861 // physical register or in the parameter list area of the current stack
3862 // frame to the callee.
3863 Arg = PtrOff;
3864 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003865
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003866 if (VA.isRegLoc()) {
Hal Finkel2a9d3182014-03-06 00:23:33 +00003867 if (Arg.getValueType() == MVT::i1)
3868 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg);
3869
Roman Divacky71038e72011-08-30 17:04:16 +00003870 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003871 // Put argument in a physical register.
3872 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3873 } else {
3874 // Put argument in the parameter list area of the current stack frame.
3875 assert(VA.isMemLoc());
3876 unsigned LocMemOffset = VA.getLocMemOffset();
3877
3878 if (!isTailCall) {
3879 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3880 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3881
3882 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00003883 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00003884 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003885 } else {
3886 // Calculate and remember argument location.
3887 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3888 TailCallArguments);
3889 }
3890 }
3891 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003892
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003893 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003894 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003895 &MemOpChains[0], MemOpChains.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00003896
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003897 // Build a sequence of copy-to-reg nodes chained together with token chain
3898 // and flag operands which copy the outgoing args into the appropriate regs.
3899 SDValue InFlag;
3900 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3901 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3902 RegsToPass[i].second, InFlag);
3903 InFlag = Chain.getValue(1);
3904 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003905
Hal Finkel5ab37802012-08-28 02:10:27 +00003906 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3907 // registers.
3908 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003909 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3910 SDValue Ops[] = { Chain, InFlag };
3911
Hal Finkel5ab37802012-08-28 02:10:27 +00003912 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003913 dl, VTs, Ops, InFlag.getNode() ? 2 : 1);
3914
Hal Finkel5ab37802012-08-28 02:10:27 +00003915 InFlag = Chain.getValue(1);
3916 }
3917
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003918 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003919 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3920 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003921
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003922 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3923 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3924 Ins, InVals);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003925}
3926
Bill Schmidt57d6de52012-10-23 15:51:16 +00003927// Copy an argument into memory, being careful to do this outside the
3928// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003929SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00003930PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3931 SDValue CallSeqStart,
3932 ISD::ArgFlagsTy Flags,
3933 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003934 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003935 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3936 CallSeqStart.getNode()->getOperand(0),
3937 Flags, DAG, dl);
3938 // The MEMCPY must go outside the CALLSEQ_START..END.
3939 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003940 CallSeqStart.getNode()->getOperand(1),
3941 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00003942 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3943 NewCallSeqStart.getNode());
3944 return NewCallSeqStart;
3945}
3946
3947SDValue
3948PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003949 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003950 bool isTailCall,
3951 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003952 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003953 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003954 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003955 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003956
Bill Schmidt57d6de52012-10-23 15:51:16 +00003957 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003958
Bill Schmidt57d6de52012-10-23 15:51:16 +00003959 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3960 unsigned PtrByteSize = 8;
3961
3962 MachineFunction &MF = DAG.getMachineFunction();
3963
3964 // Mark this function as potentially containing a function that contains a
3965 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3966 // and restoring the callers stack pointer in this functions epilog. This is
3967 // done because by tail calling the called function might overwrite the value
3968 // in this function's (MF) stack pointer stack slot 0(SP).
3969 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3970 CallConv == CallingConv::Fast)
3971 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3972
3973 unsigned nAltivecParamsAtEnd = 0;
3974
3975 // Count how many bytes are to be pushed on the stack, including the linkage
3976 // area, and parameter passing area. We start with at least 48 bytes, which
3977 // is reserved space for [SP][CR][LR][3 x unused].
3978 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
3979 // of this call.
3980 unsigned NumBytes =
3981 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
3982 Outs, OutVals, nAltivecParamsAtEnd);
3983
3984 // Calculate by how many bytes the stack has to be adjusted in case of tail
3985 // call optimization.
3986 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3987
3988 // To protect arguments on the stack from being clobbered in a tail call,
3989 // force all the loads to happen before doing any other lowering.
3990 if (isTailCall)
3991 Chain = DAG.getStackArgumentTokenFactor(Chain);
3992
3993 // Adjust the stack pointer for the new arguments...
3994 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003995 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3996 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003997 SDValue CallSeqStart = Chain;
3998
3999 // Load the return address and frame pointer so it can be move somewhere else
4000 // later.
4001 SDValue LROp, FPOp;
4002 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4003 dl);
4004
4005 // Set up a copy of the stack pointer for use loading and storing any
4006 // arguments that may not fit in the registers available for argument
4007 // passing.
4008 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
4009
4010 // Figure out which arguments are going to go in registers, and which in
4011 // memory. Also, if this is a vararg function, floating point operations
4012 // must be stored to our stack, and loaded into integer regs as well, if
4013 // any integer regs are available for argument passing.
4014 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
4015 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
4016
4017 static const uint16_t GPR[] = {
4018 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4019 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4020 };
4021 static const uint16_t *FPR = GetFPR();
4022
4023 static const uint16_t VR[] = {
4024 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4025 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4026 };
Hal Finkel7811c612014-03-28 19:58:11 +00004027 static const uint16_t VSRH[] = {
4028 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
4029 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
4030 };
4031
Bill Schmidt57d6de52012-10-23 15:51:16 +00004032 const unsigned NumGPRs = array_lengthof(GPR);
4033 const unsigned NumFPRs = 13;
4034 const unsigned NumVRs = array_lengthof(VR);
4035
4036 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4037 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4038
4039 SmallVector<SDValue, 8> MemOpChains;
4040 for (unsigned i = 0; i != NumOps; ++i) {
4041 SDValue Arg = OutVals[i];
4042 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4043
4044 // PtrOff will be used to store the current argument to the stack if a
4045 // register cannot be found for it.
4046 SDValue PtrOff;
4047
4048 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
4049
4050 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4051
4052 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00004053 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004054 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4055 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4056 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
4057 }
4058
4059 // FIXME memcpy is used way more than necessary. Correctness first.
4060 // Note: "by value" is code for passing a structure by value, not
4061 // basic types.
4062 if (Flags.isByVal()) {
4063 // Note: Size includes alignment padding, so
4064 // struct x { short a; char b; }
4065 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
4066 // These are the proper values we need for right-justifying the
4067 // aggregate in a parameter register.
4068 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00004069
4070 // An empty aggregate parameter takes up no storage and no
4071 // registers.
4072 if (Size == 0)
4073 continue;
4074
Hal Finkel262a2242013-09-12 23:20:06 +00004075 unsigned BVAlign = Flags.getByValAlign();
4076 if (BVAlign > 8) {
4077 if (BVAlign % PtrByteSize != 0)
4078 llvm_unreachable(
4079 "ByVal alignment is not a multiple of the pointer size");
4080
4081 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
4082 }
4083
Bill Schmidt57d6de52012-10-23 15:51:16 +00004084 // All aggregates smaller than 8 bytes must be passed right-justified.
4085 if (Size==1 || Size==2 || Size==4) {
4086 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
4087 if (GPR_idx != NumGPRs) {
4088 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
4089 MachinePointerInfo(), VT,
4090 false, false, 0);
4091 MemOpChains.push_back(Load.getValue(1));
4092 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4093
4094 ArgOffset += PtrByteSize;
4095 continue;
4096 }
4097 }
4098
4099 if (GPR_idx == NumGPRs && Size < 8) {
4100 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4101 PtrOff.getValueType());
4102 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4103 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4104 CallSeqStart,
4105 Flags, DAG, dl);
4106 ArgOffset += PtrByteSize;
4107 continue;
4108 }
4109 // Copy entire object into memory. There are cases where gcc-generated
4110 // code assumes it is there, even if it could be put entirely into
4111 // registers. (This is not what the doc says.)
4112
4113 // FIXME: The above statement is likely due to a misunderstanding of the
4114 // documents. All arguments must be copied into the parameter area BY
4115 // THE CALLEE in the event that the callee takes the address of any
4116 // formal argument. That has not yet been implemented. However, it is
4117 // reasonable to use the stack area as a staging area for the register
4118 // load.
4119
4120 // Skip this for small aggregates, as we will use the same slot for a
4121 // right-justified copy, below.
4122 if (Size >= 8)
4123 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4124 CallSeqStart,
4125 Flags, DAG, dl);
4126
4127 // When a register is available, pass a small aggregate right-justified.
4128 if (Size < 8 && GPR_idx != NumGPRs) {
4129 // The easiest way to get this right-justified in a register
4130 // is to copy the structure into the rightmost portion of a
4131 // local variable slot, then load the whole slot into the
4132 // register.
4133 // FIXME: The memcpy seems to produce pretty awful code for
4134 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00004135 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00004136 // parameter save area instead of a new local variable.
4137 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
4138 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4139 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4140 CallSeqStart,
4141 Flags, DAG, dl);
4142
4143 // Load the slot into the register.
4144 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
4145 MachinePointerInfo(),
4146 false, false, false, 0);
4147 MemOpChains.push_back(Load.getValue(1));
4148 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4149
4150 // Done with this argument.
4151 ArgOffset += PtrByteSize;
4152 continue;
4153 }
4154
4155 // For aggregates larger than PtrByteSize, copy the pieces of the
4156 // object that fit into registers from the parameter save area.
4157 for (unsigned j=0; j<Size; j+=PtrByteSize) {
4158 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
4159 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
4160 if (GPR_idx != NumGPRs) {
4161 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4162 MachinePointerInfo(),
4163 false, false, false, 0);
4164 MemOpChains.push_back(Load.getValue(1));
4165 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4166 ArgOffset += PtrByteSize;
4167 } else {
4168 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4169 break;
4170 }
4171 }
4172 continue;
4173 }
4174
Craig Topper56710102013-08-15 02:33:50 +00004175 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004176 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00004177 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004178 case MVT::i32:
4179 case MVT::i64:
4180 if (GPR_idx != NumGPRs) {
4181 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
4182 } else {
4183 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4184 true, isTailCall, false, MemOpChains,
4185 TailCallArguments, dl);
4186 }
4187 ArgOffset += PtrByteSize;
4188 break;
4189 case MVT::f32:
4190 case MVT::f64:
4191 if (FPR_idx != NumFPRs) {
4192 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4193
4194 if (isVarArg) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004195 // A single float or an aggregate containing only a single float
4196 // must be passed right-justified in the stack doubleword, and
4197 // in the GPR, if one is available.
4198 SDValue StoreOff;
Craig Topper56710102013-08-15 02:33:50 +00004199 if (Arg.getSimpleValueType().SimpleTy == MVT::f32) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004200 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4201 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4202 } else
4203 StoreOff = PtrOff;
4204
4205 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004206 MachinePointerInfo(), false, false, 0);
4207 MemOpChains.push_back(Store);
4208
4209 // Float varargs are always shadowed in available integer registers
4210 if (GPR_idx != NumGPRs) {
4211 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4212 MachinePointerInfo(), false, false,
4213 false, 0);
4214 MemOpChains.push_back(Load.getValue(1));
4215 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4216 }
4217 } else if (GPR_idx != NumGPRs)
4218 // If we have any FPRs remaining, we may also have GPRs remaining.
4219 ++GPR_idx;
4220 } else {
4221 // Single-precision floating-point values are mapped to the
4222 // second (rightmost) word of the stack doubleword.
4223 if (Arg.getValueType() == MVT::f32) {
4224 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4225 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4226 }
4227
4228 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4229 true, isTailCall, false, MemOpChains,
4230 TailCallArguments, dl);
4231 }
4232 ArgOffset += 8;
4233 break;
4234 case MVT::v4f32:
4235 case MVT::v4i32:
4236 case MVT::v8i16:
4237 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00004238 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00004239 case MVT::v2i64:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004240 if (isVarArg) {
4241 // These go aligned on the stack, or in the corresponding R registers
4242 // when within range. The Darwin PPC ABI doc claims they also go in
4243 // V registers; in fact gcc does this only for arguments that are
4244 // prototyped, not for those that match the ... We do it for all
4245 // arguments, seems to work.
4246 while (ArgOffset % 16 !=0) {
4247 ArgOffset += PtrByteSize;
4248 if (GPR_idx != NumGPRs)
4249 GPR_idx++;
4250 }
4251 // We could elide this store in the case where the object fits
4252 // entirely in R registers. Maybe later.
4253 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
4254 DAG.getConstant(ArgOffset, PtrVT));
4255 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4256 MachinePointerInfo(), false, false, 0);
4257 MemOpChains.push_back(Store);
4258 if (VR_idx != NumVRs) {
4259 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4260 MachinePointerInfo(),
4261 false, false, false, 0);
4262 MemOpChains.push_back(Load.getValue(1));
Hal Finkel7811c612014-03-28 19:58:11 +00004263
4264 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4265 Arg.getSimpleValueType() == MVT::v2i64) ?
4266 VSRH[VR_idx] : VR[VR_idx];
4267 ++VR_idx;
4268
4269 RegsToPass.push_back(std::make_pair(VReg, Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004270 }
4271 ArgOffset += 16;
4272 for (unsigned i=0; i<16; i+=PtrByteSize) {
4273 if (GPR_idx == NumGPRs)
4274 break;
4275 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4276 DAG.getConstant(i, PtrVT));
4277 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4278 false, false, false, 0);
4279 MemOpChains.push_back(Load.getValue(1));
4280 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4281 }
4282 break;
4283 }
4284
4285 // Non-varargs Altivec params generally go in registers, but have
4286 // stack space allocated at the end.
4287 if (VR_idx != NumVRs) {
4288 // Doesn't have GPR space allocated.
Hal Finkel7811c612014-03-28 19:58:11 +00004289 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4290 Arg.getSimpleValueType() == MVT::v2i64) ?
4291 VSRH[VR_idx] : VR[VR_idx];
4292 ++VR_idx;
4293
4294 RegsToPass.push_back(std::make_pair(VReg, Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004295 } else {
4296 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4297 true, isTailCall, true, MemOpChains,
4298 TailCallArguments, dl);
4299 ArgOffset += 16;
4300 }
4301 break;
4302 }
4303 }
4304
4305 if (!MemOpChains.empty())
4306 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4307 &MemOpChains[0], MemOpChains.size());
4308
4309 // Check if this is an indirect call (MTCTR/BCTRL).
4310 // See PrepareCall() for more information about calls through function
4311 // pointers in the 64-bit SVR4 ABI.
4312 if (!isTailCall &&
4313 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4314 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4315 !isBLACompatibleAddress(Callee, DAG)) {
4316 // Load r2 into a virtual register and store it to the TOC save area.
4317 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4318 // TOC save area offset.
4319 SDValue PtrOff = DAG.getIntPtrConstant(40);
4320 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4321 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4322 false, false, 0);
4323 // R12 must contain the address of an indirect callee. This does not
4324 // mean the MTCTR instruction must use R12; it's easier to model this
4325 // as an extra parameter, so do that.
4326 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4327 }
4328
4329 // Build a sequence of copy-to-reg nodes chained together with token chain
4330 // and flag operands which copy the outgoing args into the appropriate regs.
4331 SDValue InFlag;
4332 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4333 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4334 RegsToPass[i].second, InFlag);
4335 InFlag = Chain.getValue(1);
4336 }
4337
4338 if (isTailCall)
4339 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4340 FPOp, true, TailCallArguments);
4341
4342 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4343 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4344 Ins, InVals);
4345}
4346
4347SDValue
4348PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4349 CallingConv::ID CallConv, bool isVarArg,
4350 bool isTailCall,
4351 const SmallVectorImpl<ISD::OutputArg> &Outs,
4352 const SmallVectorImpl<SDValue> &OutVals,
4353 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004354 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004355 SmallVectorImpl<SDValue> &InVals) const {
4356
4357 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004358
Owen Anderson53aa7a92009-08-10 22:56:29 +00004359 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00004360 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004361 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004362
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004363 MachineFunction &MF = DAG.getMachineFunction();
4364
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004365 // Mark this function as potentially containing a function that contains a
4366 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4367 // and restoring the callers stack pointer in this functions epilog. This is
4368 // done because by tail calling the called function might overwrite the value
4369 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004370 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4371 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004372 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4373
4374 unsigned nAltivecParamsAtEnd = 0;
4375
Chris Lattneraa40ec12006-05-16 22:56:08 +00004376 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00004377 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00004378 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004379 unsigned NumBytes =
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004380 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004381 Outs, OutVals,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004382 nAltivecParamsAtEnd);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004383
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004384 // Calculate by how many bytes the stack has to be adjusted in case of tail
4385 // call optimization.
4386 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004387
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004388 // To protect arguments on the stack from being clobbered in a tail call,
4389 // force all the loads to happen before doing any other lowering.
4390 if (isTailCall)
4391 Chain = DAG.getStackArgumentTokenFactor(Chain);
4392
Chris Lattnerb7552a82006-05-17 00:15:40 +00004393 // Adjust the stack pointer for the new arguments...
4394 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004395 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4396 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004397 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004398
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004399 // Load the return address and frame pointer so it can be move somewhere else
4400 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004401 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004402 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4403 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004404
Chris Lattnerb7552a82006-05-17 00:15:40 +00004405 // Set up a copy of the stack pointer for use loading and storing any
4406 // arguments that may not fit in the registers available for argument
4407 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004408 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004409 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004410 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004411 else
Owen Anderson9f944592009-08-11 20:47:22 +00004412 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004413
Chris Lattnerb7552a82006-05-17 00:15:40 +00004414 // Figure out which arguments are going to go in registers, and which in
4415 // memory. Also, if this is a vararg function, floating point operations
4416 // must be stored to our stack, and loaded into integer regs as well, if
4417 // any integer regs are available for argument passing.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004418 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004419 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004420
Craig Topperca658c22012-03-11 07:16:55 +00004421 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004422 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4423 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4424 };
Craig Topperca658c22012-03-11 07:16:55 +00004425 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00004426 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4427 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4428 };
Craig Topperca658c22012-03-11 07:16:55 +00004429 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004430
Craig Topperca658c22012-03-11 07:16:55 +00004431 static const uint16_t VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004432 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4433 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4434 };
Owen Andersone2f23a32007-09-07 04:06:50 +00004435 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004436 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00004437 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004438
Craig Topperca658c22012-03-11 07:16:55 +00004439 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004440
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004441 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004442 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4443
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004444 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00004445 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004446 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004447 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004448
Chris Lattnerb7552a82006-05-17 00:15:40 +00004449 // PtrOff will be used to store the current argument to the stack if a
4450 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004451 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004452
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004453 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004454
Dale Johannesen679073b2009-02-04 02:34:38 +00004455 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004456
4457 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00004458 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00004459 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4460 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00004461 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004462 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00004463
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004464 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004465 // Note: "by value" is code for passing a structure by value, not
4466 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00004467 if (Flags.isByVal()) {
4468 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004469 // Very small objects are passed right-justified. Everything else is
4470 // passed left-justified.
4471 if (Size==1 || Size==2) {
4472 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004473 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00004474 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00004475 MachinePointerInfo(), VT,
4476 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004477 MemOpChains.push_back(Load.getValue(1));
4478 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004479
4480 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004481 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00004482 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4483 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004484 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004485 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4486 CallSeqStart,
4487 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004488 ArgOffset += PtrByteSize;
4489 }
4490 continue;
4491 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004492 // Copy entire object into memory. There are cases where gcc-generated
4493 // code assumes it is there, even if it could be put entirely into
4494 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004495 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4496 CallSeqStart,
4497 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004498
4499 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4500 // copy the pieces of the object that fit into registers from the
4501 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00004502 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004503 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004504 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00004505 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004506 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4507 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004508 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00004509 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00004510 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004511 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004512 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004513 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004514 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004515 }
4516 }
4517 continue;
4518 }
4519
Craig Topper56710102013-08-15 02:33:50 +00004520 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004521 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00004522 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00004523 case MVT::i32:
4524 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004525 if (GPR_idx != NumGPRs) {
Hal Finkel7f908e82014-03-06 00:45:19 +00004526 if (Arg.getValueType() == MVT::i1)
4527 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg);
4528
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004529 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004530 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004531 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4532 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004533 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00004534 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004535 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004536 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004537 case MVT::f32:
4538 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004539 if (FPR_idx != NumFPRs) {
4540 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4541
Chris Lattnerb7552a82006-05-17 00:15:40 +00004542 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00004543 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4544 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004545 MemOpChains.push_back(Store);
4546
Chris Lattnerb7552a82006-05-17 00:15:40 +00004547 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004548 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004549 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004550 MachinePointerInfo(), false, false,
4551 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004552 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004553 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004554 }
Owen Anderson9f944592009-08-11 20:47:22 +00004555 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004556 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004557 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00004558 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4559 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004560 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004561 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004562 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00004563 }
4564 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00004565 // If we have any FPRs remaining, we may also have GPRs remaining.
4566 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4567 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004568 if (GPR_idx != NumGPRs)
4569 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00004570 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004571 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4572 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004573 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004574 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004575 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4576 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004577 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004578 if (isPPC64)
4579 ArgOffset += 8;
4580 else
Owen Anderson9f944592009-08-11 20:47:22 +00004581 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004582 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004583 case MVT::v4f32:
4584 case MVT::v4i32:
4585 case MVT::v8i16:
4586 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00004587 if (isVarArg) {
4588 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00004589 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00004590 // V registers; in fact gcc does this only for arguments that are
4591 // prototyped, not for those that match the ... We do it for all
4592 // arguments, seems to work.
4593 while (ArgOffset % 16 !=0) {
4594 ArgOffset += PtrByteSize;
4595 if (GPR_idx != NumGPRs)
4596 GPR_idx++;
4597 }
4598 // We could elide this store in the case where the object fits
4599 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004600 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004601 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00004602 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4603 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004604 MemOpChains.push_back(Store);
4605 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004606 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00004607 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004608 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004609 MemOpChains.push_back(Load.getValue(1));
4610 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4611 }
4612 ArgOffset += 16;
4613 for (unsigned i=0; i<16; i+=PtrByteSize) {
4614 if (GPR_idx == NumGPRs)
4615 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00004616 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004617 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00004618 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004619 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004620 MemOpChains.push_back(Load.getValue(1));
4621 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4622 }
4623 break;
4624 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004625
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004626 // Non-varargs Altivec params generally go in registers, but have
4627 // stack space allocated at the end.
4628 if (VR_idx != NumVRs) {
4629 // Doesn't have GPR space allocated.
4630 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4631 } else if (nAltivecParamsAtEnd==0) {
4632 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004633 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4634 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004635 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004636 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00004637 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00004638 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004639 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00004640 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004641 // If all Altivec parameters fit in registers, as they usually do,
4642 // they get stack space following the non-Altivec parameters. We
4643 // don't track this here because nobody below needs it.
4644 // If there are more Altivec parameters than fit in registers emit
4645 // the stores here.
4646 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4647 unsigned j = 0;
4648 // Offset is aligned; skip 1st 12 params which go in V registers.
4649 ArgOffset = ((ArgOffset+15)/16)*16;
4650 ArgOffset += 12*16;
4651 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004652 SDValue Arg = OutVals[i];
4653 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00004654 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4655 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004656 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004657 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004658 // We are emitting Altivec params in order.
4659 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4660 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004661 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004662 ArgOffset += 16;
4663 }
4664 }
4665 }
4666 }
4667
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004668 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00004669 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnered728e82006-08-11 17:38:39 +00004670 &MemOpChains[0], MemOpChains.size());
Scott Michelcf0da6c2009-02-17 22:15:04 +00004671
Dale Johannesen90eab672010-03-09 20:15:42 +00004672 // On Darwin, R12 must contain the address of an indirect callee. This does
4673 // not mean the MTCTR instruction must use R12; it's easier to model this as
4674 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004675 if (!isTailCall &&
Dale Johannesen90eab672010-03-09 20:15:42 +00004676 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4677 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4678 !isBLACompatibleAddress(Callee, DAG))
4679 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4680 PPC::R12), Callee));
4681
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004682 // Build a sequence of copy-to-reg nodes chained together with token chain
4683 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004684 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004685 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00004686 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00004687 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004688 InFlag = Chain.getValue(1);
4689 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004690
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004691 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004692 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4693 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004694
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004695 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4696 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4697 Ins, InVals);
Chris Lattneraa40ec12006-05-16 22:56:08 +00004698}
4699
Hal Finkel450128a2011-10-14 19:51:36 +00004700bool
4701PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4702 MachineFunction &MF, bool isVarArg,
4703 const SmallVectorImpl<ISD::OutputArg> &Outs,
4704 LLVMContext &Context) const {
4705 SmallVector<CCValAssign, 16> RVLocs;
4706 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4707 RVLocs, Context);
4708 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4709}
4710
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004711SDValue
4712PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004713 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004714 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004715 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004716 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004717
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004718 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004719 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00004720 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004721 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004722
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004723 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004724 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004725
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004726 // Copy the result values into the output registers.
4727 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4728 CCValAssign &VA = RVLocs[i];
4729 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004730
4731 SDValue Arg = OutVals[i];
4732
4733 switch (VA.getLocInfo()) {
4734 default: llvm_unreachable("Unknown loc info!");
4735 case CCValAssign::Full: break;
4736 case CCValAssign::AExt:
4737 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4738 break;
4739 case CCValAssign::ZExt:
4740 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4741 break;
4742 case CCValAssign::SExt:
4743 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4744 break;
4745 }
4746
4747 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004748 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004749 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004750 }
4751
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004752 RetOps[0] = Chain; // Update chain.
4753
4754 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00004755 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004756 RetOps.push_back(Flag);
4757
4758 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other,
4759 &RetOps[0], RetOps.size());
Chris Lattner4211ca92006-04-14 06:01:58 +00004760}
4761
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004762SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004763 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00004764 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004765 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004766
Jim Laskeye4f4d042006-12-04 22:04:42 +00004767 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004768 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00004769
4770 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00004771 bool isPPC64 = Subtarget.isPPC64();
4772 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004773 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004774
4775 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004776 SDValue Chain = Op.getOperand(0);
4777 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004778
Jim Laskeye4f4d042006-12-04 22:04:42 +00004779 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00004780 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4781 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004782 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004783
Jim Laskeye4f4d042006-12-04 22:04:42 +00004784 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00004785 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004786
Jim Laskeye4f4d042006-12-04 22:04:42 +00004787 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00004788 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004789 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004790}
4791
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004792
4793
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004794SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004795PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004796 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004797 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004798 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004799 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004800
4801 // Get current frame pointer save index. The users of this index will be
4802 // primarily DYNALLOC instructions.
4803 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4804 int RASI = FI->getReturnAddrSaveIndex();
4805
4806 // If the frame pointer save index hasn't been defined yet.
4807 if (!RASI) {
4808 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004809 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004810 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004811 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004812 // Save the result.
4813 FI->setReturnAddrSaveIndex(RASI);
4814 }
4815 return DAG.getFrameIndex(RASI, PtrVT);
4816}
4817
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004818SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004819PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4820 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004821 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004822 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004823 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004824
4825 // Get current frame pointer save index. The users of this index will be
4826 // primarily DYNALLOC instructions.
4827 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4828 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004829
Jim Laskey48850c12006-11-16 22:43:37 +00004830 // If the frame pointer save index hasn't been defined yet.
4831 if (!FPSI) {
4832 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004833 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004834 isDarwinABI);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004835
Jim Laskey48850c12006-11-16 22:43:37 +00004836 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004837 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00004838 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004839 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00004840 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004841 return DAG.getFrameIndex(FPSI, PtrVT);
4842}
Jim Laskey48850c12006-11-16 22:43:37 +00004843
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004844SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004845 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004846 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004847 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004848 SDValue Chain = Op.getOperand(0);
4849 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004850 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004851
Jim Laskey48850c12006-11-16 22:43:37 +00004852 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004853 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004854 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004855 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00004856 DAG.getConstant(0, PtrVT), Size);
4857 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004858 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00004859 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004860 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00004861 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004862 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey48850c12006-11-16 22:43:37 +00004863}
4864
Hal Finkel756810f2013-03-21 21:37:52 +00004865SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
4866 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004867 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004868 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
4869 DAG.getVTList(MVT::i32, MVT::Other),
4870 Op.getOperand(0), Op.getOperand(1));
4871}
4872
4873SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
4874 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004875 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004876 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
4877 Op.getOperand(0), Op.getOperand(1));
4878}
4879
Hal Finkel940ab932014-02-28 00:27:01 +00004880SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
4881 assert(Op.getValueType() == MVT::i1 &&
4882 "Custom lowering only for i1 loads");
4883
4884 // First, load 8 bits into 32 bits, then truncate to 1 bit.
4885
4886 SDLoc dl(Op);
4887 LoadSDNode *LD = cast<LoadSDNode>(Op);
4888
4889 SDValue Chain = LD->getChain();
4890 SDValue BasePtr = LD->getBasePtr();
4891 MachineMemOperand *MMO = LD->getMemOperand();
4892
4893 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(), Chain,
4894 BasePtr, MVT::i8, MMO);
4895 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
4896
4897 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
4898 return DAG.getMergeValues(Ops, 2, dl);
4899}
4900
4901SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
4902 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
4903 "Custom lowering only for i1 stores");
4904
4905 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
4906
4907 SDLoc dl(Op);
4908 StoreSDNode *ST = cast<StoreSDNode>(Op);
4909
4910 SDValue Chain = ST->getChain();
4911 SDValue BasePtr = ST->getBasePtr();
4912 SDValue Value = ST->getValue();
4913 MachineMemOperand *MMO = ST->getMemOperand();
4914
4915 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(), Value);
4916 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
4917}
4918
4919// FIXME: Remove this once the ANDI glue bug is fixed:
4920SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
4921 assert(Op.getValueType() == MVT::i1 &&
4922 "Custom lowering only for i1 results");
4923
4924 SDLoc DL(Op);
4925 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
4926 Op.getOperand(0));
4927}
4928
Chris Lattner4211ca92006-04-14 06:01:58 +00004929/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4930/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004931SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00004932 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00004933 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4934 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00004935 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004936
Hal Finkel81f87992013-04-07 22:11:09 +00004937 // We might be able to do better than this under some circumstances, but in
4938 // general, fsel-based lowering of select is a finite-math-only optimization.
4939 // For more information, see section F.3 of the 2.06 ISA specification.
4940 if (!DAG.getTarget().Options.NoInfsFPMath ||
4941 !DAG.getTarget().Options.NoNaNsFPMath)
4942 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004943
Hal Finkel81f87992013-04-07 22:11:09 +00004944 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004945
Owen Anderson53aa7a92009-08-10 22:56:29 +00004946 EVT ResVT = Op.getValueType();
4947 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004948 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4949 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004950 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004951
Chris Lattner4211ca92006-04-14 06:01:58 +00004952 // If the RHS of the comparison is a 0.0, we don't need to do the
4953 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00004954 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00004955 if (isFloatingPointZero(RHS))
4956 switch (CC) {
4957 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004958 case ISD::SETNE:
4959 std::swap(TV, FV);
4960 case ISD::SETEQ:
4961 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4962 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
4963 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
4964 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4965 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4966 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4967 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004968 case ISD::SETULT:
4969 case ISD::SETLT:
4970 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004971 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004972 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00004973 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4974 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004975 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004976 case ISD::SETUGT:
4977 case ISD::SETGT:
4978 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004979 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004980 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00004981 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4982 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004983 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00004984 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004985 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004986
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004987 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00004988 switch (CC) {
4989 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004990 case ISD::SETNE:
4991 std::swap(TV, FV);
4992 case ISD::SETEQ:
4993 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
4994 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4995 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
4996 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
4997 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4998 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4999 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5000 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005001 case ISD::SETULT:
5002 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005003 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005004 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5005 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005006 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005007 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005008 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005009 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005010 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5011 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005012 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005013 case ISD::SETUGT:
5014 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005015 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005016 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5017 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005018 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005019 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005020 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005021 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005022 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5023 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005024 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005025 }
Eli Friedman5806e182009-05-28 04:31:08 +00005026 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00005027}
5028
Chris Lattner57ee7c62007-11-28 18:44:47 +00005029// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005030SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005031 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00005032 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005033 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00005034 if (Src.getValueType() == MVT::f32)
5035 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00005036
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005037 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00005038 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005039 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00005040 case MVT::i32:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005041 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Hal Finkelf6d45f22013-04-01 17:52:07 +00005042 (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ :
5043 PPCISD::FCTIDZ),
Owen Anderson9f944592009-08-11 20:47:22 +00005044 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005045 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005046 case MVT::i64:
Hal Finkel3f88d082013-04-01 18:42:58 +00005047 assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) &&
5048 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00005049 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
5050 PPCISD::FCTIDUZ,
5051 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005052 break;
5053 }
Duncan Sands2a287912008-07-19 16:26:02 +00005054
Chris Lattner4211ca92006-04-14 06:01:58 +00005055 // Convert the FP value to an int value through memory.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005056 bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() &&
5057 (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT());
5058 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
5059 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
5060 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00005061
Chris Lattner06a49542007-10-15 20:14:52 +00005062 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005063 SDValue Chain;
5064 if (i32Stack) {
5065 MachineFunction &MF = DAG.getMachineFunction();
5066 MachineMemOperand *MMO =
5067 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
5068 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
5069 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
5070 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
5071 MVT::i32, MMO);
5072 } else
5073 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
5074 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00005075
5076 // Result is a load from the stack slot. If loading 4 bytes, make sure to
5077 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005078 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00005079 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00005080 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkelf6d45f22013-04-01 17:52:07 +00005081 MPI = MachinePointerInfo();
5082 }
5083
5084 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooper82cd9e82011-11-08 18:42:53 +00005085 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005086}
5087
Hal Finkelf6d45f22013-04-01 17:52:07 +00005088SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005089 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005090 SDLoc dl(Op);
Dan Gohmand6819da2008-03-11 01:59:03 +00005091 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00005092 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005093 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00005094
Hal Finkel6a56b212014-03-05 22:14:00 +00005095 if (Op.getOperand(0).getValueType() == MVT::i1)
5096 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0),
5097 DAG.getConstantFP(1.0, Op.getValueType()),
5098 DAG.getConstantFP(0.0, Op.getValueType()));
5099
Hal Finkelf6d45f22013-04-01 17:52:07 +00005100 assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) &&
5101 "UINT_TO_FP is supported only with FPCVT");
5102
5103 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00005104 // Otherwise, convert to double-precision and then round.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005105 unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
5106 (Op.getOpcode() == ISD::UINT_TO_FP ?
5107 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
5108 (Op.getOpcode() == ISD::UINT_TO_FP ?
5109 PPCISD::FCFIDU : PPCISD::FCFID);
5110 MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
5111 MVT::f32 : MVT::f64;
5112
Owen Anderson9f944592009-08-11 20:47:22 +00005113 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005114 SDValue SINT = Op.getOperand(0);
5115 // When converting to single-precision, we actually need to convert
5116 // to double-precision first and then round to single-precision.
5117 // To avoid double-rounding effects during that operation, we have
5118 // to prepare the input operand. Bits that might be truncated when
5119 // converting to double-precision are replaced by a bit that won't
5120 // be lost at this stage, but is below the single-precision rounding
5121 // position.
5122 //
5123 // However, if -enable-unsafe-fp-math is in effect, accept double
5124 // rounding to avoid the extra overhead.
5125 if (Op.getValueType() == MVT::f32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005126 !PPCSubTarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005127 !DAG.getTarget().Options.UnsafeFPMath) {
5128
5129 // Twiddle input to make sure the low 11 bits are zero. (If this
5130 // is the case, we are guaranteed the value will fit into the 53 bit
5131 // mantissa of an IEEE double-precision value without rounding.)
5132 // If any of those low 11 bits were not zero originally, make sure
5133 // bit 12 (value 2048) is set instead, so that the final rounding
5134 // to single-precision gets the correct result.
5135 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5136 SINT, DAG.getConstant(2047, MVT::i64));
5137 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
5138 Round, DAG.getConstant(2047, MVT::i64));
5139 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
5140 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5141 Round, DAG.getConstant(-2048, MVT::i64));
5142
5143 // However, we cannot use that value unconditionally: if the magnitude
5144 // of the input value is small, the bit-twiddling we did above might
5145 // end up visibly changing the output. Fortunately, in that case, we
5146 // don't need to twiddle bits since the original input will convert
5147 // exactly to double-precision floating-point already. Therefore,
5148 // construct a conditional to use the original value if the top 11
5149 // bits are all sign-bit copies, and use the rounded value computed
5150 // above otherwise.
5151 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
5152 SINT, DAG.getConstant(53, MVT::i32));
5153 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
5154 Cond, DAG.getConstant(1, MVT::i64));
5155 Cond = DAG.getSetCC(dl, MVT::i32,
5156 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
5157
5158 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
5159 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00005160
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005161 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005162 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
5163
5164 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00005165 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005166 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005167 return FP;
5168 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005169
Owen Anderson9f944592009-08-11 20:47:22 +00005170 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005171 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005172 // Since we only generate this in 64-bit mode, we can take advantage of
5173 // 64-bit registers. In particular, sign extend the input value into the
5174 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
5175 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00005176 MachineFunction &MF = DAG.getMachineFunction();
5177 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005178 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005179
Hal Finkelbeb296b2013-03-31 10:12:51 +00005180 SDValue Ld;
Hal Finkelf6d45f22013-04-01 17:52:07 +00005181 if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) {
Hal Finkelbeb296b2013-03-31 10:12:51 +00005182 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
5183 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005184
Hal Finkelbeb296b2013-03-31 10:12:51 +00005185 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
5186 MachinePointerInfo::getFixedStack(FrameIdx),
5187 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00005188
Hal Finkelbeb296b2013-03-31 10:12:51 +00005189 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
5190 "Expected an i32 store");
5191 MachineMemOperand *MMO =
5192 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
5193 MachineMemOperand::MOLoad, 4, 4);
5194 SDValue Ops[] = { Store, FIdx };
Hal Finkelf6d45f22013-04-01 17:52:07 +00005195 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
5196 PPCISD::LFIWZX : PPCISD::LFIWAX,
5197 dl, DAG.getVTList(MVT::f64, MVT::Other),
5198 Ops, 2, MVT::i32, MMO);
Hal Finkelbeb296b2013-03-31 10:12:51 +00005199 } else {
Hal Finkelf6d45f22013-04-01 17:52:07 +00005200 assert(PPCSubTarget.isPPC64() &&
5201 "i32->FP without LFIWAX supported only on PPC64");
5202
Hal Finkelbeb296b2013-03-31 10:12:51 +00005203 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
5204 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
5205
5206 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
5207 Op.getOperand(0));
5208
5209 // STD the extended value into the stack slot.
5210 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
5211 MachinePointerInfo::getFixedStack(FrameIdx),
5212 false, false, 0);
5213
5214 // Load the value as a double.
5215 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
5216 MachinePointerInfo::getFixedStack(FrameIdx),
5217 false, false, false, 0);
5218 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005219
Chris Lattner4211ca92006-04-14 06:01:58 +00005220 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005221 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
5222 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00005223 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005224 return FP;
5225}
5226
Dan Gohman21cea8a2010-04-17 15:26:15 +00005227SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
5228 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005229 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005230 /*
5231 The rounding mode is in bits 30:31 of FPSR, and has the following
5232 settings:
5233 00 Round to nearest
5234 01 Round to 0
5235 10 Round to +inf
5236 11 Round to -inf
5237
5238 FLT_ROUNDS, on the other hand, expects the following:
5239 -1 Undefined
5240 0 Round to 0
5241 1 Round to nearest
5242 2 Round to +inf
5243 3 Round to -inf
5244
5245 To perform the conversion, we do:
5246 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
5247 */
5248
5249 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005250 EVT VT = Op.getValueType();
5251 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005252 SDValue MFFSreg, InFlag;
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005253
5254 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005255 EVT NodeTys[] = {
5256 MVT::f64, // return register
5257 MVT::Glue // unused in this context
5258 };
Dale Johannesen021052a2009-02-04 20:06:27 +00005259 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005260
5261 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00005262 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005263 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005264 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00005265 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005266
5267 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005268 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005269 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00005270 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005271 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005272
5273 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005274 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00005275 DAG.getNode(ISD::AND, dl, MVT::i32,
5276 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005277 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00005278 DAG.getNode(ISD::SRL, dl, MVT::i32,
5279 DAG.getNode(ISD::AND, dl, MVT::i32,
5280 DAG.getNode(ISD::XOR, dl, MVT::i32,
5281 CWD, DAG.getConstant(3, MVT::i32)),
5282 DAG.getConstant(3, MVT::i32)),
5283 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005284
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005285 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00005286 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005287
Duncan Sands13237ac2008-06-06 12:08:01 +00005288 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00005289 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005290}
5291
Dan Gohman21cea8a2010-04-17 15:26:15 +00005292SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005293 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005294 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005295 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00005296 assert(Op.getNumOperands() == 3 &&
5297 VT == Op.getOperand(1).getValueType() &&
5298 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005299
Chris Lattner601b8652006-09-20 03:47:40 +00005300 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005301 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005302 SDValue Lo = Op.getOperand(0);
5303 SDValue Hi = Op.getOperand(1);
5304 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005305 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005306
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005307 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005308 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005309 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5310 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5311 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5312 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005313 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005314 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5315 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5316 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005317 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005318 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005319}
5320
Dan Gohman21cea8a2010-04-17 15:26:15 +00005321SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005322 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005323 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00005324 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005325 assert(Op.getNumOperands() == 3 &&
5326 VT == Op.getOperand(1).getValueType() &&
5327 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005328
Dan Gohman8d2ead22008-03-07 20:36:53 +00005329 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005330 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005331 SDValue Lo = Op.getOperand(0);
5332 SDValue Hi = Op.getOperand(1);
5333 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005334 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005335
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005336 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005337 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005338 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5339 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5340 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5341 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005342 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005343 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5344 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5345 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005346 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005347 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005348}
5349
Dan Gohman21cea8a2010-04-17 15:26:15 +00005350SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005351 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005352 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005353 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005354 assert(Op.getNumOperands() == 3 &&
5355 VT == Op.getOperand(1).getValueType() &&
5356 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005357
Dan Gohman8d2ead22008-03-07 20:36:53 +00005358 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005359 SDValue Lo = Op.getOperand(0);
5360 SDValue Hi = Op.getOperand(1);
5361 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005362 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005363
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005364 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005365 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005366 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5367 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5368 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5369 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005370 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005371 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5372 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5373 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00005374 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005375 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005376 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005377}
5378
5379//===----------------------------------------------------------------------===//
5380// Vector related lowering.
5381//
5382
Chris Lattner2a099c02006-04-17 06:00:21 +00005383/// BuildSplatI - Build a canonical splati of Val with an element size of
5384/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005385static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005386 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00005387 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005388
Owen Anderson53aa7a92009-08-10 22:56:29 +00005389 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00005390 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00005391 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005392
Owen Anderson9f944592009-08-11 20:47:22 +00005393 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005394
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005395 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5396 if (Val == -1)
5397 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005398
Owen Anderson53aa7a92009-08-10 22:56:29 +00005399 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005400
Chris Lattner2a099c02006-04-17 06:00:21 +00005401 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00005402 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005403 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00005404 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga49de9d2009-02-25 22:49:59 +00005405 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
5406 &Ops[0], Ops.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00005407 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005408}
5409
Hal Finkelcf2e9082013-05-24 23:00:14 +00005410/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5411/// specified intrinsic ID.
5412static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005413 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00005414 EVT DestVT = MVT::Other) {
5415 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5416 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5417 DAG.getConstant(IID, MVT::i32), Op);
5418}
5419
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005420/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00005421/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005422static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005423 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005424 EVT DestVT = MVT::Other) {
5425 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005426 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005427 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005428}
5429
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005430/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5431/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005432static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005433 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005434 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00005435 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005436 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005437 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005438}
5439
5440
Chris Lattner264c9082006-04-17 17:55:10 +00005441/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5442/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005443static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005444 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00005445 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00005446 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5447 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005448
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005449 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00005450 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005451 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00005452 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005453 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00005454}
5455
Chris Lattner19e90552006-04-14 05:19:18 +00005456// If this is a case we can't handle, return null and let the default
5457// expansion code take care of it. If we CAN select this case, and if it
5458// selects to a single instruction, return Op. Otherwise, if we can codegen
5459// this case more efficiently than a constant pool load, lower it to the
5460// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005461SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5462 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005463 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005464 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5465 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00005466
Bob Wilson85cefe82009-03-02 23:24:16 +00005467 // Check if this is a splat of a constant value.
5468 APInt APSplatBits, APSplatUndef;
5469 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005470 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00005471 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00005472 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00005473 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00005474
Bob Wilson530e0382009-03-03 19:26:27 +00005475 unsigned SplatBits = APSplatBits.getZExtValue();
5476 unsigned SplatUndef = APSplatUndef.getZExtValue();
5477 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005478
Bob Wilson530e0382009-03-03 19:26:27 +00005479 // First, handle single instruction cases.
5480
5481 // All zeros?
5482 if (SplatBits == 0) {
5483 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00005484 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5485 SDValue Z = DAG.getConstant(0, MVT::i32);
5486 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00005487 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00005488 }
Bob Wilson530e0382009-03-03 19:26:27 +00005489 return Op;
5490 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00005491
Bob Wilson530e0382009-03-03 19:26:27 +00005492 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5493 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5494 (32-SplatBitSize));
5495 if (SextVal >= -16 && SextVal <= 15)
5496 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005497
5498
Bob Wilson530e0382009-03-03 19:26:27 +00005499 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005500
Bob Wilson530e0382009-03-03 19:26:27 +00005501 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00005502 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5503 // If this value is in the range [17,31] and is odd, use:
5504 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5505 // If this value is in the range [-31,-17] and is odd, use:
5506 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5507 // Note the last two are three-instruction sequences.
5508 if (SextVal >= -32 && SextVal <= 31) {
5509 // To avoid having these optimizations undone by constant folding,
5510 // we convert to a pseudo that will be expanded later into one of
5511 // the above forms.
5512 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt51e79512013-02-20 15:50:31 +00005513 EVT VT = Op.getValueType();
5514 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5515 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5516 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilson530e0382009-03-03 19:26:27 +00005517 }
5518
5519 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5520 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5521 // for fneg/fabs.
5522 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5523 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00005524 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005525
5526 // Make the VSLW intrinsic, computing 0x8000_0000.
5527 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5528 OnesV, DAG, dl);
5529
5530 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00005531 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00005532 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005533 }
5534
5535 // Check to see if this is a wide variety of vsplti*, binop self cases.
5536 static const signed char SplatCsts[] = {
5537 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5538 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5539 };
5540
5541 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5542 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5543 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5544 int i = SplatCsts[idx];
5545
5546 // Figure out what shift amount will be used by altivec if shifted by i in
5547 // this splat size.
5548 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5549
5550 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00005551 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005552 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005553 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5554 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5555 Intrinsic::ppc_altivec_vslw
5556 };
5557 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005558 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005559 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005560
Bob Wilson530e0382009-03-03 19:26:27 +00005561 // vsplti + srl self.
5562 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005563 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005564 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5565 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5566 Intrinsic::ppc_altivec_vsrw
5567 };
5568 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005569 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005570 }
5571
Bob Wilson530e0382009-03-03 19:26:27 +00005572 // vsplti + sra self.
5573 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005574 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005575 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5576 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5577 Intrinsic::ppc_altivec_vsraw
5578 };
5579 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005580 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005581 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005582
Bob Wilson530e0382009-03-03 19:26:27 +00005583 // vsplti + rol self.
5584 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5585 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005586 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005587 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5588 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5589 Intrinsic::ppc_altivec_vrlw
5590 };
5591 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005592 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005593 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005594
Bob Wilson530e0382009-03-03 19:26:27 +00005595 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00005596 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005597 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005598 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00005599 }
Bob Wilson530e0382009-03-03 19:26:27 +00005600 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00005601 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005602 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005603 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00005604 }
Bob Wilson530e0382009-03-03 19:26:27 +00005605 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00005606 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005607 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005608 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5609 }
5610 }
5611
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005612 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00005613}
5614
Chris Lattner071ad012006-04-17 05:28:54 +00005615/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5616/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005617static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005618 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005619 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00005620 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00005621 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00005622 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005623
Chris Lattner071ad012006-04-17 05:28:54 +00005624 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00005625 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
Chris Lattner071ad012006-04-17 05:28:54 +00005626 OP_VMRGHW,
5627 OP_VMRGLW,
5628 OP_VSPLTISW0,
5629 OP_VSPLTISW1,
5630 OP_VSPLTISW2,
5631 OP_VSPLTISW3,
5632 OP_VSLDOI4,
5633 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00005634 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00005635 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00005636
Chris Lattner071ad012006-04-17 05:28:54 +00005637 if (OpNum == OP_COPY) {
5638 if (LHSID == (1*9+2)*9+3) return LHS;
5639 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5640 return RHS;
5641 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005642
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005643 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005644 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5645 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005646
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005647 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00005648 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005649 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00005650 case OP_VMRGHW:
5651 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5652 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5653 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5654 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5655 break;
5656 case OP_VMRGLW:
5657 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5658 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5659 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5660 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5661 break;
5662 case OP_VSPLTISW0:
5663 for (unsigned i = 0; i != 16; ++i)
5664 ShufIdxs[i] = (i&3)+0;
5665 break;
5666 case OP_VSPLTISW1:
5667 for (unsigned i = 0; i != 16; ++i)
5668 ShufIdxs[i] = (i&3)+4;
5669 break;
5670 case OP_VSPLTISW2:
5671 for (unsigned i = 0; i != 16; ++i)
5672 ShufIdxs[i] = (i&3)+8;
5673 break;
5674 case OP_VSPLTISW3:
5675 for (unsigned i = 0; i != 16; ++i)
5676 ShufIdxs[i] = (i&3)+12;
5677 break;
5678 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005679 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005680 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005681 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005682 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005683 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005684 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00005685 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00005686 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5687 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005688 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00005689 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00005690}
5691
Chris Lattner19e90552006-04-14 05:19:18 +00005692/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5693/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5694/// return the code it can be lowered into. Worst case, it can always be
5695/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005696SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005697 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005698 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005699 SDValue V1 = Op.getOperand(0);
5700 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005701 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005702 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005703
Chris Lattner19e90552006-04-14 05:19:18 +00005704 // Cases that are handled by instructions that take permute immediates
5705 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5706 // selected by the instruction selector.
5707 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005708 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5709 PPC::isSplatShuffleMask(SVOp, 2) ||
5710 PPC::isSplatShuffleMask(SVOp, 4) ||
5711 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5712 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5713 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5714 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5715 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5716 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5717 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5718 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5719 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattner19e90552006-04-14 05:19:18 +00005720 return Op;
5721 }
5722 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005723
Chris Lattner19e90552006-04-14 05:19:18 +00005724 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5725 // and produce a fixed permutation. If any of these match, do not lower to
5726 // VPERM.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005727 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5728 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5729 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5730 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5731 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5732 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5733 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5734 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5735 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattner19e90552006-04-14 05:19:18 +00005736 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005737
Chris Lattner071ad012006-04-17 05:28:54 +00005738 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5739 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005740 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00005741
Chris Lattner071ad012006-04-17 05:28:54 +00005742 unsigned PFIndexes[4];
5743 bool isFourElementShuffle = true;
5744 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5745 unsigned EltNo = 8; // Start out undef.
5746 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005747 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00005748 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005749
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005750 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00005751 if ((ByteSource & 3) != j) {
5752 isFourElementShuffle = false;
5753 break;
5754 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005755
Chris Lattner071ad012006-04-17 05:28:54 +00005756 if (EltNo == 8) {
5757 EltNo = ByteSource/4;
5758 } else if (EltNo != ByteSource/4) {
5759 isFourElementShuffle = false;
5760 break;
5761 }
5762 }
5763 PFIndexes[i] = EltNo;
5764 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005765
5766 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00005767 // perfect shuffle vector to determine if it is cost effective to do this as
5768 // discrete instructions, or whether we should use a vperm.
5769 if (isFourElementShuffle) {
5770 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005771 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00005772 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005773
Chris Lattner071ad012006-04-17 05:28:54 +00005774 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5775 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005776
Chris Lattner071ad012006-04-17 05:28:54 +00005777 // Determining when to avoid vperm is tricky. Many things affect the cost
5778 // of vperm, particularly how many times the perm mask needs to be computed.
5779 // For example, if the perm mask can be hoisted out of a loop or is already
5780 // used (perhaps because there are multiple permutes with the same shuffle
5781 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5782 // the loop requires an extra register.
5783 //
5784 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00005785 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00005786 // available, if this block is within a loop, we should avoid using vperm
5787 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005788 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005789 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005790 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005791
Chris Lattner19e90552006-04-14 05:19:18 +00005792 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5793 // vector that will get spilled to the constant pool.
5794 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005795
Chris Lattner19e90552006-04-14 05:19:18 +00005796 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5797 // that it is in input element units, not in bytes. Convert now.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005798 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005799 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005800
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005801 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005802 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5803 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005804
Chris Lattner19e90552006-04-14 05:19:18 +00005805 for (unsigned j = 0; j != BytesPerElement; ++j)
5806 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson9f944592009-08-11 20:47:22 +00005807 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00005808 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005809
Owen Anderson9f944592009-08-11 20:47:22 +00005810 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga49de9d2009-02-25 22:49:59 +00005811 &ResultMask[0], ResultMask.size());
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005812 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00005813}
5814
Chris Lattner9754d142006-04-18 17:59:36 +00005815/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5816/// altivec comparison. If it is, return true and fill in Opc/isDot with
5817/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005818static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner9754d142006-04-18 17:59:36 +00005819 bool &isDot) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005820 unsigned IntrinsicID =
5821 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00005822 CompareOpc = -1;
5823 isDot = false;
5824 switch (IntrinsicID) {
5825 default: return false;
5826 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00005827 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5828 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5829 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5830 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5831 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5832 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5833 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5834 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5835 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5836 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5837 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5838 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5839 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005840
Chris Lattner4211ca92006-04-14 06:01:58 +00005841 // Normal Comparisons.
5842 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5843 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5844 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5845 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5846 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5847 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5848 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5849 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5850 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5851 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5852 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5853 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5854 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5855 }
Chris Lattner9754d142006-04-18 17:59:36 +00005856 return true;
5857}
5858
5859/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5860/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005861SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005862 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00005863 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5864 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005865 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00005866 int CompareOpc;
5867 bool isDot;
5868 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005869 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005870
Chris Lattner9754d142006-04-18 17:59:36 +00005871 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00005872 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00005873 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00005874 Op.getOperand(1), Op.getOperand(2),
5875 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00005876 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00005877 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005878
Chris Lattner4211ca92006-04-14 06:01:58 +00005879 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005880 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005881 Op.getOperand(2), // LHS
5882 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00005883 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005884 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005885 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00005886 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005887
Chris Lattner4211ca92006-04-14 06:01:58 +00005888 // Now that we have the comparison, emit a copy from the CR to a GPR.
5889 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00005890 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00005891 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00005892 CompNode.getValue(1));
5893
Chris Lattner4211ca92006-04-14 06:01:58 +00005894 // Unpack the result based on how the target uses it.
5895 unsigned BitNo; // Bit # of CR6.
5896 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00005897 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00005898 default: // Can't happen, don't crash on invalid number though.
5899 case 0: // Return the value of the EQ bit of CR6.
5900 BitNo = 0; InvertBit = false;
5901 break;
5902 case 1: // Return the inverted value of the EQ bit of CR6.
5903 BitNo = 0; InvertBit = true;
5904 break;
5905 case 2: // Return the value of the LT bit of CR6.
5906 BitNo = 2; InvertBit = false;
5907 break;
5908 case 3: // Return the inverted value of the LT bit of CR6.
5909 BitNo = 2; InvertBit = true;
5910 break;
5911 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005912
Chris Lattner4211ca92006-04-14 06:01:58 +00005913 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00005914 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5915 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005916 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00005917 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5918 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00005919
Chris Lattner4211ca92006-04-14 06:01:58 +00005920 // If we are supposed to, toggle the bit.
5921 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00005922 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5923 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005924 return Flags;
5925}
5926
Scott Michelcf0da6c2009-02-17 22:15:04 +00005927SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005928 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005929 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00005930 // Create a stack slot that is 16-byte aligned.
5931 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00005932 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00005933 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005934 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005935
Chris Lattner4211ca92006-04-14 06:01:58 +00005936 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00005937 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00005938 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005939 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005940 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00005941 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005942 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005943}
5944
Dan Gohman21cea8a2010-04-17 15:26:15 +00005945SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005946 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005947 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005948 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005949
Owen Anderson9f944592009-08-11 20:47:22 +00005950 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
5951 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005952
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005953 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005954 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005955
Chris Lattner7e4398742006-04-18 03:43:48 +00005956 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00005957 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
5958 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
5959 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005960
Chris Lattner7e4398742006-04-18 03:43:48 +00005961 // Low parts multiplied together, generating 32-bit results (we ignore the
5962 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005963 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00005964 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005965
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005966 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00005967 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00005968 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005969 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005970 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005971 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
5972 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005973 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005974
Owen Anderson9f944592009-08-11 20:47:22 +00005975 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00005976
Chris Lattner96d50482006-04-18 04:28:57 +00005977 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005978 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005979 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005980 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005981
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005982 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005983 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00005984 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005985 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005986
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005987 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005988 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00005989 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005990 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005991
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005992 // Merge the results together.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005993 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005994 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005995 Ops[i*2 ] = 2*i+1;
5996 Ops[i*2+1] = 2*i+1+16;
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005997 }
Owen Anderson9f944592009-08-11 20:47:22 +00005998 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00005999 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006000 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00006001 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006002}
6003
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006004/// LowerOperation - Provide custom lowering hooks for some operations.
6005///
Dan Gohman21cea8a2010-04-17 15:26:15 +00006006SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006007 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006008 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00006009 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00006010 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006011 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00006012 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00006013 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006014 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00006015 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
6016 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006017 case ISD::VASTART:
Dan Gohman31ae5862010-04-17 14:41:14 +00006018 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006019
6020 case ISD::VAARG:
Dan Gohman31ae5862010-04-17 14:41:14 +00006021 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00006022
Roman Divackyc3825df2013-07-25 21:36:47 +00006023 case ISD::VACOPY:
6024 return LowerVACOPY(Op, DAG, PPCSubTarget);
6025
Jim Laskeye4f4d042006-12-04 22:04:42 +00006026 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00006027 case ISD::DYNAMIC_STACKALLOC:
6028 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng51096af2008-04-19 01:30:48 +00006029
Hal Finkel756810f2013-03-21 21:37:52 +00006030 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
6031 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
6032
Hal Finkel940ab932014-02-28 00:27:01 +00006033 case ISD::LOAD: return LowerLOAD(Op, DAG);
6034 case ISD::STORE: return LowerSTORE(Op, DAG);
6035 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006036 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006037 case ISD::FP_TO_UINT:
6038 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006039 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00006040 case ISD::UINT_TO_FP:
6041 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00006042 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006043
Chris Lattner4211ca92006-04-14 06:01:58 +00006044 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00006045 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
6046 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
6047 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006048
Chris Lattner4211ca92006-04-14 06:01:58 +00006049 // Vector-related lowering.
6050 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6051 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6052 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6053 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006054 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006055
Hal Finkel25c19922013-05-15 21:37:41 +00006056 // For counter-based loop handling.
6057 case ISD::INTRINSIC_W_CHAIN: return SDValue();
6058
Chris Lattnerf6a81562007-12-08 06:59:59 +00006059 // Frame & Return address.
6060 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00006061 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00006062 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006063}
6064
Duncan Sands6ed40142008-12-01 11:39:25 +00006065void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
6066 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006067 SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00006068 const TargetMachine &TM = getTargetMachine();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006069 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00006070 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00006071 default:
Craig Toppere55c5562012-02-07 02:50:20 +00006072 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkel25c19922013-05-15 21:37:41 +00006073 case ISD::INTRINSIC_W_CHAIN: {
6074 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
6075 Intrinsic::ppc_is_decremented_ctr_nonzero)
6076 break;
6077
6078 assert(N->getValueType(0) == MVT::i1 &&
6079 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00006080 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00006081 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
6082 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
6083 N->getOperand(1));
6084
6085 Results.push_back(NewInt);
6086 Results.push_back(NewInt.getValue(1));
6087 break;
6088 }
Roman Divacky4394e682011-06-28 15:30:42 +00006089 case ISD::VAARG: {
6090 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
6091 || TM.getSubtarget<PPCSubtarget>().isPPC64())
6092 return;
6093
6094 EVT VT = N->getValueType(0);
6095
6096 if (VT == MVT::i64) {
6097 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
6098
6099 Results.push_back(NewNode);
6100 Results.push_back(NewNode.getValue(1));
6101 }
6102 return;
6103 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006104 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00006105 assert(N->getValueType(0) == MVT::ppcf128);
6106 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006107 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006108 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006109 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00006110 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006111 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006112 DAG.getIntPtrConstant(1));
6113
Ulrich Weigand874fc622013-03-26 10:56:22 +00006114 // Add the two halves of the long double in round-to-zero mode.
6115 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00006116
6117 // We know the low half is about to be thrown away, so just use something
6118 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00006119 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00006120 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00006121 return;
Duncan Sands2a287912008-07-19 16:26:02 +00006122 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006123 case ISD::FP_TO_SINT:
Bill Schmidt41221692013-07-09 18:50:20 +00006124 // LowerFP_TO_INT() can only handle f32 and f64.
6125 if (N->getOperand(0).getValueType() == MVT::ppcf128)
6126 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006127 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00006128 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00006129 }
6130}
6131
6132
Chris Lattner4211ca92006-04-14 06:01:58 +00006133//===----------------------------------------------------------------------===//
6134// Other Lowering Code
6135//===----------------------------------------------------------------------===//
6136
Chris Lattner9b577f12005-08-26 21:23:58 +00006137MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00006138PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman747e55b2009-02-07 16:15:20 +00006139 bool is64bit, unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006140 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesend4eb0522008-08-25 22:34:37 +00006141 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6142
6143 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6144 MachineFunction *F = BB->getParent();
6145 MachineFunction::iterator It = BB;
6146 ++It;
6147
6148 unsigned dest = MI->getOperand(0).getReg();
6149 unsigned ptrA = MI->getOperand(1).getReg();
6150 unsigned ptrB = MI->getOperand(2).getReg();
6151 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006152 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00006153
6154 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6155 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6156 F->insert(It, loopMBB);
6157 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006158 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006159 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006160 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006161
6162 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006163 unsigned TmpReg = (!BinOpcode) ? incr :
6164 RegInfo.createVirtualRegister(
Dale Johannesenbc698292008-09-02 20:30:23 +00006165 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6166 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006167
6168 // thisMBB:
6169 // ...
6170 // fallthrough --> loopMBB
6171 BB->addSuccessor(loopMBB);
6172
6173 // loopMBB:
6174 // l[wd]arx dest, ptr
6175 // add r0, dest, incr
6176 // st[wd]cx. r0, ptr
6177 // bne- loopMBB
6178 // fallthrough --> exitMBB
6179 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006180 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00006181 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006182 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006183 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
6184 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesend4eb0522008-08-25 22:34:37 +00006185 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006186 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006187 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006188 BB->addSuccessor(loopMBB);
6189 BB->addSuccessor(exitMBB);
6190
6191 // exitMBB:
6192 // ...
6193 BB = exitMBB;
6194 return BB;
6195}
6196
6197MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00006198PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00006199 MachineBasicBlock *BB,
6200 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00006201 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006202 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesena32affb2008-08-28 17:53:09 +00006203 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6204 // In 64 bit mode we have to use 64 bits for addresses, even though the
6205 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
6206 // registers without caring whether they're 32 or 64, but here we're
6207 // doing actual arithmetic on the addresses.
6208 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00006209 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00006210
6211 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6212 MachineFunction *F = BB->getParent();
6213 MachineFunction::iterator It = BB;
6214 ++It;
6215
6216 unsigned dest = MI->getOperand(0).getReg();
6217 unsigned ptrA = MI->getOperand(1).getReg();
6218 unsigned ptrB = MI->getOperand(2).getReg();
6219 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006220 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00006221
6222 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6223 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6224 F->insert(It, loopMBB);
6225 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006226 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006227 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006228 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006229
6230 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006231 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006232 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6233 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00006234 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6235 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6236 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6237 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
6238 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6239 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6240 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6241 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6242 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
6243 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006244 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006245 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006246 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006247
6248 // thisMBB:
6249 // ...
6250 // fallthrough --> loopMBB
6251 BB->addSuccessor(loopMBB);
6252
6253 // The 4-byte load must be aligned, while a char or short may be
6254 // anywhere in the word. Hence all this nasty bookkeeping code.
6255 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6256 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006257 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00006258 // rlwinm ptr, ptr1, 0, 0, 29
6259 // slw incr2, incr, shift
6260 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6261 // slw mask, mask2, shift
6262 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00006263 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006264 // add tmp, tmpDest, incr2
6265 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00006266 // and tmp3, tmp, mask
6267 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00006268 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00006269 // bne- loopMBB
6270 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006271 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006272 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00006273 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006274 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006275 .addReg(ptrA).addReg(ptrB);
6276 } else {
6277 Ptr1Reg = ptrB;
6278 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006279 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006280 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006281 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006282 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6283 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006284 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006285 .addReg(Ptr1Reg).addImm(0).addImm(61);
6286 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006287 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006288 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006289 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006290 .addReg(incr).addReg(ShiftReg);
6291 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006292 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00006293 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006294 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6295 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00006296 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006297 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006298 .addReg(Mask2Reg).addReg(ShiftReg);
6299
6300 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006301 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006302 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006303 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006304 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006305 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006306 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006307 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006308 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006309 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006310 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006311 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00006312 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006313 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006314 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006315 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006316 BB->addSuccessor(loopMBB);
6317 BB->addSuccessor(exitMBB);
6318
6319 // exitMBB:
6320 // ...
6321 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006322 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6323 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00006324 return BB;
6325}
6326
Hal Finkel756810f2013-03-21 21:37:52 +00006327llvm::MachineBasicBlock*
6328PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6329 MachineBasicBlock *MBB) const {
6330 DebugLoc DL = MI->getDebugLoc();
6331 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6332
6333 MachineFunction *MF = MBB->getParent();
6334 MachineRegisterInfo &MRI = MF->getRegInfo();
6335
6336 const BasicBlock *BB = MBB->getBasicBlock();
6337 MachineFunction::iterator I = MBB;
6338 ++I;
6339
6340 // Memory Reference
6341 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6342 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6343
6344 unsigned DstReg = MI->getOperand(0).getReg();
6345 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6346 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6347 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6348 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6349
6350 MVT PVT = getPointerTy();
6351 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6352 "Invalid Pointer Size!");
6353 // For v = setjmp(buf), we generate
6354 //
6355 // thisMBB:
6356 // SjLjSetup mainMBB
6357 // bl mainMBB
6358 // v_restore = 1
6359 // b sinkMBB
6360 //
6361 // mainMBB:
6362 // buf[LabelOffset] = LR
6363 // v_main = 0
6364 //
6365 // sinkMBB:
6366 // v = phi(main, restore)
6367 //
6368
6369 MachineBasicBlock *thisMBB = MBB;
6370 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6371 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6372 MF->insert(I, mainMBB);
6373 MF->insert(I, sinkMBB);
6374
6375 MachineInstrBuilder MIB;
6376
6377 // Transfer the remainder of BB and its successor edges to sinkMBB.
6378 sinkMBB->splice(sinkMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006379 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Hal Finkel756810f2013-03-21 21:37:52 +00006380 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6381
6382 // Note that the structure of the jmp_buf used here is not compatible
6383 // with that used by libc, and is not designed to be. Specifically, it
6384 // stores only those 'reserved' registers that LLVM does not otherwise
6385 // understand how to spill. Also, by convention, by the time this
6386 // intrinsic is called, Clang has already stored the frame address in the
6387 // first slot of the buffer and stack address in the third. Following the
6388 // X86 target code, we'll store the jump address in the second slot. We also
6389 // need to save the TOC pointer (R2) to handle jumps between shared
6390 // libraries, and that will be stored in the fourth slot. The thread
6391 // identifier (R13) is not affected.
6392
6393 // thisMBB:
6394 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6395 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006396 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006397
6398 // Prepare IP either in reg.
6399 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6400 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6401 unsigned BufReg = MI->getOperand(1).getReg();
6402
6403 if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) {
6404 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6405 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006406 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006407 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006408 MIB.setMemRefs(MMOBegin, MMOEnd);
6409 }
6410
Hal Finkelf05d6c72013-07-17 23:50:51 +00006411 // Naked functions never have a base pointer, and so we use r1. For all
6412 // other functions, this decision must be delayed until during PEI.
6413 unsigned BaseReg;
6414 if (MF->getFunction()->getAttributes().hasAttribute(
6415 AttributeSet::FunctionIndex, Attribute::Naked))
6416 BaseReg = PPCSubTarget.isPPC64() ? PPC::X1 : PPC::R1;
6417 else
6418 BaseReg = PPCSubTarget.isPPC64() ? PPC::BP8 : PPC::BP;
6419
6420 MIB = BuildMI(*thisMBB, MI, DL,
6421 TII->get(PPCSubTarget.isPPC64() ? PPC::STD : PPC::STW))
6422 .addReg(BaseReg)
6423 .addImm(BPOffset)
6424 .addReg(BufReg);
6425 MIB.setMemRefs(MMOBegin, MMOEnd);
6426
Hal Finkel756810f2013-03-21 21:37:52 +00006427 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00006428 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling5e7656b2013-06-07 07:55:53 +00006429 const PPCRegisterInfo *TRI =
6430 static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo());
6431 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00006432
6433 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6434
6435 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6436 .addMBB(mainMBB);
6437 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6438
6439 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6440 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6441
6442 // mainMBB:
6443 // mainDstReg = 0
6444 MIB = BuildMI(mainMBB, DL,
6445 TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
6446
6447 // Store IP
6448 if (PPCSubTarget.isPPC64()) {
6449 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6450 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006451 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006452 .addReg(BufReg);
6453 } else {
6454 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6455 .addReg(LabelReg)
6456 .addImm(LabelOffset)
6457 .addReg(BufReg);
6458 }
6459
6460 MIB.setMemRefs(MMOBegin, MMOEnd);
6461
6462 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6463 mainMBB->addSuccessor(sinkMBB);
6464
6465 // sinkMBB:
6466 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6467 TII->get(PPC::PHI), DstReg)
6468 .addReg(mainDstReg).addMBB(mainMBB)
6469 .addReg(restoreDstReg).addMBB(thisMBB);
6470
6471 MI->eraseFromParent();
6472 return sinkMBB;
6473}
6474
6475MachineBasicBlock *
6476PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6477 MachineBasicBlock *MBB) const {
6478 DebugLoc DL = MI->getDebugLoc();
6479 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6480
6481 MachineFunction *MF = MBB->getParent();
6482 MachineRegisterInfo &MRI = MF->getRegInfo();
6483
6484 // Memory Reference
6485 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6486 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6487
6488 MVT PVT = getPointerTy();
6489 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6490 "Invalid Pointer Size!");
6491
6492 const TargetRegisterClass *RC =
6493 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6494 unsigned Tmp = MRI.createVirtualRegister(RC);
6495 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6496 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6497 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006498 unsigned BP = (PVT == MVT::i64) ? PPC::X30 : PPC::R30;
Hal Finkel756810f2013-03-21 21:37:52 +00006499
6500 MachineInstrBuilder MIB;
6501
6502 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6503 const int64_t SPOffset = 2 * PVT.getStoreSize();
6504 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006505 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006506
6507 unsigned BufReg = MI->getOperand(0).getReg();
6508
6509 // Reload FP (the jumped-to function may not have had a
6510 // frame pointer, and if so, then its r31 will be restored
6511 // as necessary).
6512 if (PVT == MVT::i64) {
6513 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6514 .addImm(0)
6515 .addReg(BufReg);
6516 } else {
6517 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6518 .addImm(0)
6519 .addReg(BufReg);
6520 }
6521 MIB.setMemRefs(MMOBegin, MMOEnd);
6522
6523 // Reload IP
6524 if (PVT == MVT::i64) {
6525 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006526 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006527 .addReg(BufReg);
6528 } else {
6529 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6530 .addImm(LabelOffset)
6531 .addReg(BufReg);
6532 }
6533 MIB.setMemRefs(MMOBegin, MMOEnd);
6534
6535 // Reload SP
6536 if (PVT == MVT::i64) {
6537 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006538 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006539 .addReg(BufReg);
6540 } else {
6541 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6542 .addImm(SPOffset)
6543 .addReg(BufReg);
6544 }
6545 MIB.setMemRefs(MMOBegin, MMOEnd);
6546
Hal Finkelf05d6c72013-07-17 23:50:51 +00006547 // Reload BP
6548 if (PVT == MVT::i64) {
6549 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
6550 .addImm(BPOffset)
6551 .addReg(BufReg);
6552 } else {
6553 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
6554 .addImm(BPOffset)
6555 .addReg(BufReg);
6556 }
6557 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00006558
6559 // Reload TOC
6560 if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) {
6561 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006562 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006563 .addReg(BufReg);
6564
6565 MIB.setMemRefs(MMOBegin, MMOEnd);
6566 }
6567
6568 // Jump
6569 BuildMI(*MBB, MI, DL,
6570 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6571 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6572
6573 MI->eraseFromParent();
6574 return MBB;
6575}
6576
Dale Johannesena32affb2008-08-28 17:53:09 +00006577MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00006578PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00006579 MachineBasicBlock *BB) const {
Hal Finkel756810f2013-03-21 21:37:52 +00006580 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6581 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6582 return emitEHSjLjSetJmp(MI, BB);
6583 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6584 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6585 return emitEHSjLjLongJmp(MI, BB);
6586 }
6587
Evan Cheng20350c42006-11-27 23:37:22 +00006588 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00006589
6590 // To "insert" these instructions we actually have to insert their
6591 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00006592 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00006593 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00006594 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00006595
Dan Gohman3b460302008-07-07 23:14:23 +00006596 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00006597
Hal Finkel460e94d2012-06-22 23:10:08 +00006598 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006599 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6600 MI->getOpcode() == PPC::SELECT_I4 ||
6601 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00006602 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00006603 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6604 MI->getOpcode() == PPC::SELECT_CC_I8)
6605 Cond.push_back(MI->getOperand(4));
6606 else
6607 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00006608 Cond.push_back(MI->getOperand(1));
6609
Hal Finkel460e94d2012-06-22 23:10:08 +00006610 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00006611 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6612 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
6613 Cond, MI->getOperand(2).getReg(),
6614 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00006615 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6616 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6617 MI->getOpcode() == PPC::SELECT_CC_F4 ||
6618 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006619 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
6620 MI->getOpcode() == PPC::SELECT_I4 ||
6621 MI->getOpcode() == PPC::SELECT_I8 ||
6622 MI->getOpcode() == PPC::SELECT_F4 ||
6623 MI->getOpcode() == PPC::SELECT_F8 ||
6624 MI->getOpcode() == PPC::SELECT_VRRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00006625 // The incoming instruction knows the destination vreg to set, the
6626 // condition code register to branch on, the true/false values to
6627 // select between, and a branch opcode to use.
6628
6629 // thisMBB:
6630 // ...
6631 // TrueVal = ...
6632 // cmpTY ccX, r1, r2
6633 // bCC copy1MBB
6634 // fallthrough --> copy0MBB
6635 MachineBasicBlock *thisMBB = BB;
6636 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6637 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006638 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006639 F->insert(It, copy0MBB);
6640 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006641
6642 // Transfer the remainder of BB and its successor edges to sinkMBB.
6643 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006644 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006645 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6646
Evan Cheng32e376f2008-07-12 02:23:19 +00006647 // Next, add the true and fallthrough blocks as its successors.
6648 BB->addSuccessor(copy0MBB);
6649 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006650
Hal Finkel940ab932014-02-28 00:27:01 +00006651 if (MI->getOpcode() == PPC::SELECT_I4 ||
6652 MI->getOpcode() == PPC::SELECT_I8 ||
6653 MI->getOpcode() == PPC::SELECT_F4 ||
6654 MI->getOpcode() == PPC::SELECT_F8 ||
6655 MI->getOpcode() == PPC::SELECT_VRRC) {
6656 BuildMI(BB, dl, TII->get(PPC::BC))
6657 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6658 } else {
6659 unsigned SelectPred = MI->getOperand(4).getImm();
6660 BuildMI(BB, dl, TII->get(PPC::BCC))
6661 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6662 }
Dan Gohman34396292010-07-06 20:24:04 +00006663
Evan Cheng32e376f2008-07-12 02:23:19 +00006664 // copy0MBB:
6665 // %FalseValue = ...
6666 // # fallthrough to sinkMBB
6667 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006668
Evan Cheng32e376f2008-07-12 02:23:19 +00006669 // Update machine-CFG edges
6670 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006671
Evan Cheng32e376f2008-07-12 02:23:19 +00006672 // sinkMBB:
6673 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6674 // ...
6675 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00006676 BuildMI(*BB, BB->begin(), dl,
6677 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00006678 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
6679 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6680 }
Dale Johannesena32affb2008-08-28 17:53:09 +00006681 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
6682 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
6683 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
6684 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006685 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
6686 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
6687 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
6688 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006689
6690 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
6691 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
6692 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
6693 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006694 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
6695 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
6696 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
6697 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006698
6699 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
6700 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
6701 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
6702 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006703 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
6704 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
6705 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
6706 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006707
6708 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
6709 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
6710 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
6711 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006712 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
6713 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
6714 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
6715 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006716
6717 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006718 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006719 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006720 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006721 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006722 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006723 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006724 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006725
6726 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6727 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6728 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6729 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006730 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6731 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6732 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6733 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006734
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006735 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6736 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6737 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6738 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6739 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6740 BB = EmitAtomicBinary(MI, BB, false, 0);
6741 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6742 BB = EmitAtomicBinary(MI, BB, true, 0);
6743
Evan Cheng32e376f2008-07-12 02:23:19 +00006744 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6745 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6746 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6747
6748 unsigned dest = MI->getOperand(0).getReg();
6749 unsigned ptrA = MI->getOperand(1).getReg();
6750 unsigned ptrB = MI->getOperand(2).getReg();
6751 unsigned oldval = MI->getOperand(3).getReg();
6752 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006753 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006754
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006755 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6756 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6757 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006758 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006759 F->insert(It, loop1MBB);
6760 F->insert(It, loop2MBB);
6761 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006762 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006763 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006764 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006765 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006766
6767 // thisMBB:
6768 // ...
6769 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006770 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006771
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006772 // loop1MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006773 // l[wd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006774 // cmp[wd] dest, oldval
6775 // bne- midMBB
6776 // loop2MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006777 // st[wd]cx. newval, ptr
6778 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006779 // b exitBB
6780 // midMBB:
6781 // st[wd]cx. dest, ptr
6782 // exitBB:
6783 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006784 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00006785 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006786 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00006787 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006788 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006789 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6790 BB->addSuccessor(loop2MBB);
6791 BB->addSuccessor(midMBB);
6792
6793 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006794 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng32e376f2008-07-12 02:23:19 +00006795 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006796 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006797 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006798 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006799 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006800 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006801
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006802 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006803 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006804 .addReg(dest).addReg(ptrA).addReg(ptrB);
6805 BB->addSuccessor(exitMBB);
6806
Evan Cheng32e376f2008-07-12 02:23:19 +00006807 // exitMBB:
6808 // ...
6809 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00006810 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6811 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6812 // We must use 64-bit registers for addresses when targeting 64-bit,
6813 // since we're actually doing arithmetic on them. Other registers
6814 // can be 32-bit.
6815 bool is64bit = PPCSubTarget.isPPC64();
6816 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6817
6818 unsigned dest = MI->getOperand(0).getReg();
6819 unsigned ptrA = MI->getOperand(1).getReg();
6820 unsigned ptrB = MI->getOperand(2).getReg();
6821 unsigned oldval = MI->getOperand(3).getReg();
6822 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006823 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00006824
6825 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6826 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6827 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6828 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6829 F->insert(It, loop1MBB);
6830 F->insert(It, loop2MBB);
6831 F->insert(It, midMBB);
6832 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006833 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006834 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006835 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006836
6837 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006838 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006839 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6840 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00006841 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6842 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6843 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6844 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6845 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6846 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6847 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6848 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6849 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6850 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6851 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6852 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6853 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6854 unsigned Ptr1Reg;
6855 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00006856 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00006857 // thisMBB:
6858 // ...
6859 // fallthrough --> loopMBB
6860 BB->addSuccessor(loop1MBB);
6861
6862 // The 4-byte load must be aligned, while a char or short may be
6863 // anywhere in the word. Hence all this nasty bookkeeping code.
6864 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6865 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006866 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00006867 // rlwinm ptr, ptr1, 0, 0, 29
6868 // slw newval2, newval, shift
6869 // slw oldval2, oldval,shift
6870 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6871 // slw mask, mask2, shift
6872 // and newval3, newval2, mask
6873 // and oldval3, oldval2, mask
6874 // loop1MBB:
6875 // lwarx tmpDest, ptr
6876 // and tmp, tmpDest, mask
6877 // cmpw tmp, oldval3
6878 // bne- midMBB
6879 // loop2MBB:
6880 // andc tmp2, tmpDest, mask
6881 // or tmp4, tmp2, newval3
6882 // stwcx. tmp4, ptr
6883 // bne- loop1MBB
6884 // b exitBB
6885 // midMBB:
6886 // stwcx. tmpDest, ptr
6887 // exitBB:
6888 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006889 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00006890 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006891 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006892 .addReg(ptrA).addReg(ptrB);
6893 } else {
6894 Ptr1Reg = ptrB;
6895 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006896 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006897 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006898 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006899 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6900 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006901 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006902 .addReg(Ptr1Reg).addImm(0).addImm(61);
6903 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006904 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006905 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006906 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006907 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006908 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006909 .addReg(oldval).addReg(ShiftReg);
6910 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006911 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00006912 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006913 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6914 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6915 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00006916 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006917 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006918 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006919 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006920 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006921 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006922 .addReg(OldVal2Reg).addReg(MaskReg);
6923
6924 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006925 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006926 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006927 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6928 .addReg(TmpDestReg).addReg(MaskReg);
6929 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00006930 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006931 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006932 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6933 BB->addSuccessor(loop2MBB);
6934 BB->addSuccessor(midMBB);
6935
6936 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006937 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6938 .addReg(TmpDestReg).addReg(MaskReg);
6939 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6940 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6941 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006942 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006943 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006944 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006945 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006946 BB->addSuccessor(loop1MBB);
6947 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006948
Dale Johannesen340d2642008-08-30 00:08:53 +00006949 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006950 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006951 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00006952 BB->addSuccessor(exitMBB);
6953
6954 // exitMBB:
6955 // ...
6956 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006957 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
6958 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00006959 } else if (MI->getOpcode() == PPC::FADDrtz) {
6960 // This pseudo performs an FADD with rounding mode temporarily forced
6961 // to round-to-zero. We emit this via custom inserter since the FPSCR
6962 // is not modeled at the SelectionDAG level.
6963 unsigned Dest = MI->getOperand(0).getReg();
6964 unsigned Src1 = MI->getOperand(1).getReg();
6965 unsigned Src2 = MI->getOperand(2).getReg();
6966 DebugLoc dl = MI->getDebugLoc();
6967
6968 MachineRegisterInfo &RegInfo = F->getRegInfo();
6969 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
6970
6971 // Save FPSCR value.
6972 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
6973
6974 // Set rounding mode to round-to-zero.
6975 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
6976 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
6977
6978 // Perform addition.
6979 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
6980
6981 // Restore FPSCR value.
6982 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00006983 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
6984 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
6985 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
6986 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
6987 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
6988 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
6989 PPC::ANDIo8 : PPC::ANDIo;
6990 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
6991 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
6992
6993 MachineRegisterInfo &RegInfo = F->getRegInfo();
6994 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
6995 &PPC::GPRCRegClass :
6996 &PPC::G8RCRegClass);
6997
6998 DebugLoc dl = MI->getDebugLoc();
6999 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
7000 .addReg(MI->getOperand(1).getReg()).addImm(1);
7001 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
7002 MI->getOperand(0).getReg())
7003 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Dale Johannesen340d2642008-08-30 00:08:53 +00007004 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007005 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00007006 }
Chris Lattner9b577f12005-08-26 21:23:58 +00007007
Dan Gohman34396292010-07-06 20:24:04 +00007008 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00007009 return BB;
7010}
7011
Chris Lattner4211ca92006-04-14 06:01:58 +00007012//===----------------------------------------------------------------------===//
7013// Target Optimization Hooks
7014//===----------------------------------------------------------------------===//
7015
Hal Finkelb0c810f2013-04-03 17:44:56 +00007016SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
7017 DAGCombinerInfo &DCI) const {
Hal Finkel2e103312013-04-03 04:01:11 +00007018 if (DCI.isAfterLegalizeVectorOps())
7019 return SDValue();
7020
Hal Finkelb0c810f2013-04-03 17:44:56 +00007021 EVT VT = Op.getValueType();
7022
7023 if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) ||
7024 (VT == MVT::f64 && PPCSubTarget.hasFRE()) ||
Hal Finkel27774d92014-03-13 07:58:58 +00007025 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec()) ||
7026 (VT == MVT::v2f64 && PPCSubTarget.hasVSX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00007027
7028 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
7029 // For the reciprocal, we need to find the zero of the function:
7030 // F(X) = A X - 1 [which has a zero at X = 1/A]
7031 // =>
7032 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
7033 // does not require additional intermediate precision]
7034
7035 // Convergence is quadratic, so we essentially double the number of digits
7036 // correct after every iteration. The minimum architected relative
7037 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
7038 // 23 digits and double has 52 digits.
7039 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00007040 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00007041 ++Iterations;
7042
7043 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007044 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007045
7046 SDValue FPOne =
Hal Finkelb0c810f2013-04-03 17:44:56 +00007047 DAG.getConstantFP(1.0, VT.getScalarType());
7048 if (VT.isVector()) {
7049 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00007050 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00007051 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel2e103312013-04-03 04:01:11 +00007052 FPOne, FPOne, FPOne, FPOne);
7053 }
7054
Hal Finkelb0c810f2013-04-03 17:44:56 +00007055 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007056 DCI.AddToWorklist(Est.getNode());
7057
7058 // Newton iterations: Est = Est + Est (1 - Arg * Est)
7059 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007060 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00007061 DCI.AddToWorklist(NewEst.getNode());
7062
Hal Finkelb0c810f2013-04-03 17:44:56 +00007063 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007064 DCI.AddToWorklist(NewEst.getNode());
7065
Hal Finkelb0c810f2013-04-03 17:44:56 +00007066 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007067 DCI.AddToWorklist(NewEst.getNode());
7068
Hal Finkelb0c810f2013-04-03 17:44:56 +00007069 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007070 DCI.AddToWorklist(Est.getNode());
7071 }
7072
7073 return Est;
7074 }
7075
7076 return SDValue();
7077}
7078
Hal Finkelb0c810f2013-04-03 17:44:56 +00007079SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel2e103312013-04-03 04:01:11 +00007080 DAGCombinerInfo &DCI) const {
7081 if (DCI.isAfterLegalizeVectorOps())
7082 return SDValue();
7083
Hal Finkelb0c810f2013-04-03 17:44:56 +00007084 EVT VT = Op.getValueType();
7085
7086 if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) ||
7087 (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) ||
Hal Finkel27774d92014-03-13 07:58:58 +00007088 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec()) ||
7089 (VT == MVT::v2f64 && PPCSubTarget.hasVSX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00007090
7091 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
7092 // For the reciprocal sqrt, we need to find the zero of the function:
7093 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
7094 // =>
7095 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
7096 // As a result, we precompute A/2 prior to the iteration loop.
7097
7098 // Convergence is quadratic, so we essentially double the number of digits
7099 // correct after every iteration. The minimum architected relative
7100 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
7101 // 23 digits and double has 52 digits.
7102 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00007103 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00007104 ++Iterations;
7105
7106 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007107 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007108
Hal Finkelb0c810f2013-04-03 17:44:56 +00007109 SDValue FPThreeHalves =
7110 DAG.getConstantFP(1.5, VT.getScalarType());
7111 if (VT.isVector()) {
7112 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00007113 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00007114 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
7115 FPThreeHalves, FPThreeHalves,
7116 FPThreeHalves, FPThreeHalves);
Hal Finkel2e103312013-04-03 04:01:11 +00007117 }
7118
Hal Finkelb0c810f2013-04-03 17:44:56 +00007119 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007120 DCI.AddToWorklist(Est.getNode());
7121
7122 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
7123 // this entire sequence requires only one FP constant.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007124 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007125 DCI.AddToWorklist(HalfArg.getNode());
7126
Hal Finkelb0c810f2013-04-03 17:44:56 +00007127 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007128 DCI.AddToWorklist(HalfArg.getNode());
7129
7130 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
7131 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007132 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00007133 DCI.AddToWorklist(NewEst.getNode());
7134
Hal Finkelb0c810f2013-04-03 17:44:56 +00007135 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007136 DCI.AddToWorklist(NewEst.getNode());
7137
Hal Finkelb0c810f2013-04-03 17:44:56 +00007138 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007139 DCI.AddToWorklist(NewEst.getNode());
7140
Hal Finkelb0c810f2013-04-03 17:44:56 +00007141 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007142 DCI.AddToWorklist(Est.getNode());
7143 }
7144
7145 return Est;
7146 }
7147
7148 return SDValue();
7149}
7150
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007151// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
7152// not enforce equality of the chain operands.
7153static bool isConsecutiveLS(LSBaseSDNode *LS, LSBaseSDNode *Base,
7154 unsigned Bytes, int Dist,
7155 SelectionDAG &DAG) {
7156 EVT VT = LS->getMemoryVT();
7157 if (VT.getSizeInBits() / 8 != Bytes)
7158 return false;
7159
7160 SDValue Loc = LS->getBasePtr();
7161 SDValue BaseLoc = Base->getBasePtr();
7162 if (Loc.getOpcode() == ISD::FrameIndex) {
7163 if (BaseLoc.getOpcode() != ISD::FrameIndex)
7164 return false;
7165 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7166 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
7167 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
7168 int FS = MFI->getObjectSize(FI);
7169 int BFS = MFI->getObjectSize(BFI);
7170 if (FS != BFS || FS != (int)Bytes) return false;
7171 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
7172 }
7173
7174 // Handle X+C
7175 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
7176 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
7177 return true;
7178
7179 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7180 const GlobalValue *GV1 = NULL;
7181 const GlobalValue *GV2 = NULL;
7182 int64_t Offset1 = 0;
7183 int64_t Offset2 = 0;
7184 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
7185 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
7186 if (isGA1 && isGA2 && GV1 == GV2)
7187 return Offset1 == (Offset2 + Dist*Bytes);
7188 return false;
7189}
7190
Hal Finkel7d8a6912013-05-26 18:08:30 +00007191// Return true is there is a nearyby consecutive load to the one provided
7192// (regardless of alignment). We search up and down the chain, looking though
7193// token factors and other loads (but nothing else). As a result, a true
7194// results indicates that it is safe to create a new consecutive load adjacent
7195// to the load provided.
7196static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
7197 SDValue Chain = LD->getChain();
7198 EVT VT = LD->getMemoryVT();
7199
7200 SmallSet<SDNode *, 16> LoadRoots;
7201 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
7202 SmallSet<SDNode *, 16> Visited;
7203
7204 // First, search up the chain, branching to follow all token-factor operands.
7205 // If we find a consecutive load, then we're done, otherwise, record all
7206 // nodes just above the top-level loads and token factors.
7207 while (!Queue.empty()) {
7208 SDNode *ChainNext = Queue.pop_back_val();
7209 if (!Visited.insert(ChainNext))
7210 continue;
7211
7212 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007213 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007214 return true;
7215
7216 if (!Visited.count(ChainLD->getChain().getNode()))
7217 Queue.push_back(ChainLD->getChain().getNode());
7218 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
7219 for (SDNode::op_iterator O = ChainNext->op_begin(),
7220 OE = ChainNext->op_end(); O != OE; ++O)
7221 if (!Visited.count(O->getNode()))
7222 Queue.push_back(O->getNode());
7223 } else
7224 LoadRoots.insert(ChainNext);
7225 }
7226
7227 // Second, search down the chain, starting from the top-level nodes recorded
7228 // in the first phase. These top-level nodes are the nodes just above all
7229 // loads and token factors. Starting with their uses, recursively look though
7230 // all loads (just the chain uses) and token factors to find a consecutive
7231 // load.
7232 Visited.clear();
7233 Queue.clear();
7234
7235 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
7236 IE = LoadRoots.end(); I != IE; ++I) {
7237 Queue.push_back(*I);
7238
7239 while (!Queue.empty()) {
7240 SDNode *LoadRoot = Queue.pop_back_val();
7241 if (!Visited.insert(LoadRoot))
7242 continue;
7243
7244 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007245 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007246 return true;
7247
7248 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
7249 UE = LoadRoot->use_end(); UI != UE; ++UI)
7250 if (((isa<LoadSDNode>(*UI) &&
7251 cast<LoadSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
7252 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
7253 Queue.push_back(*UI);
7254 }
7255 }
7256
7257 return false;
7258}
7259
Hal Finkel940ab932014-02-28 00:27:01 +00007260SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
7261 DAGCombinerInfo &DCI) const {
7262 SelectionDAG &DAG = DCI.DAG;
7263 SDLoc dl(N);
7264
7265 assert(PPCSubTarget.useCRBits() &&
7266 "Expecting to be tracking CR bits");
7267 // If we're tracking CR bits, we need to be careful that we don't have:
7268 // trunc(binary-ops(zext(x), zext(y)))
7269 // or
7270 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
7271 // such that we're unnecessarily moving things into GPRs when it would be
7272 // better to keep them in CR bits.
7273
7274 // Note that trunc here can be an actual i1 trunc, or can be the effective
7275 // truncation that comes from a setcc or select_cc.
7276 if (N->getOpcode() == ISD::TRUNCATE &&
7277 N->getValueType(0) != MVT::i1)
7278 return SDValue();
7279
7280 if (N->getOperand(0).getValueType() != MVT::i32 &&
7281 N->getOperand(0).getValueType() != MVT::i64)
7282 return SDValue();
7283
7284 if (N->getOpcode() == ISD::SETCC ||
7285 N->getOpcode() == ISD::SELECT_CC) {
7286 // If we're looking at a comparison, then we need to make sure that the
7287 // high bits (all except for the first) don't matter the result.
7288 ISD::CondCode CC =
7289 cast<CondCodeSDNode>(N->getOperand(
7290 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
7291 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
7292
7293 if (ISD::isSignedIntSetCC(CC)) {
7294 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
7295 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
7296 return SDValue();
7297 } else if (ISD::isUnsignedIntSetCC(CC)) {
7298 if (!DAG.MaskedValueIsZero(N->getOperand(0),
7299 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
7300 !DAG.MaskedValueIsZero(N->getOperand(1),
7301 APInt::getHighBitsSet(OpBits, OpBits-1)))
7302 return SDValue();
7303 } else {
7304 // This is neither a signed nor an unsigned comparison, just make sure
7305 // that the high bits are equal.
7306 APInt Op1Zero, Op1One;
7307 APInt Op2Zero, Op2One;
7308 DAG.ComputeMaskedBits(N->getOperand(0), Op1Zero, Op1One);
7309 DAG.ComputeMaskedBits(N->getOperand(1), Op2Zero, Op2One);
7310
7311 // We don't really care about what is known about the first bit (if
7312 // anything), so clear it in all masks prior to comparing them.
7313 Op1Zero.clearBit(0); Op1One.clearBit(0);
7314 Op2Zero.clearBit(0); Op2One.clearBit(0);
7315
7316 if (Op1Zero != Op2Zero || Op1One != Op2One)
7317 return SDValue();
7318 }
7319 }
7320
7321 // We now know that the higher-order bits are irrelevant, we just need to
7322 // make sure that all of the intermediate operations are bit operations, and
7323 // all inputs are extensions.
7324 if (N->getOperand(0).getOpcode() != ISD::AND &&
7325 N->getOperand(0).getOpcode() != ISD::OR &&
7326 N->getOperand(0).getOpcode() != ISD::XOR &&
7327 N->getOperand(0).getOpcode() != ISD::SELECT &&
7328 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
7329 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
7330 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
7331 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
7332 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
7333 return SDValue();
7334
7335 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
7336 N->getOperand(1).getOpcode() != ISD::AND &&
7337 N->getOperand(1).getOpcode() != ISD::OR &&
7338 N->getOperand(1).getOpcode() != ISD::XOR &&
7339 N->getOperand(1).getOpcode() != ISD::SELECT &&
7340 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
7341 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
7342 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
7343 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
7344 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
7345 return SDValue();
7346
7347 SmallVector<SDValue, 4> Inputs;
7348 SmallVector<SDValue, 8> BinOps, PromOps;
7349 SmallPtrSet<SDNode *, 16> Visited;
7350
7351 for (unsigned i = 0; i < 2; ++i) {
7352 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7353 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7354 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7355 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7356 isa<ConstantSDNode>(N->getOperand(i)))
7357 Inputs.push_back(N->getOperand(i));
7358 else
7359 BinOps.push_back(N->getOperand(i));
7360
7361 if (N->getOpcode() == ISD::TRUNCATE)
7362 break;
7363 }
7364
7365 // Visit all inputs, collect all binary operations (and, or, xor and
7366 // select) that are all fed by extensions.
7367 while (!BinOps.empty()) {
7368 SDValue BinOp = BinOps.back();
7369 BinOps.pop_back();
7370
7371 if (!Visited.insert(BinOp.getNode()))
7372 continue;
7373
7374 PromOps.push_back(BinOp);
7375
7376 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7377 // The condition of the select is not promoted.
7378 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7379 continue;
7380 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7381 continue;
7382
7383 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7384 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7385 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7386 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7387 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7388 Inputs.push_back(BinOp.getOperand(i));
7389 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7390 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7391 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7392 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7393 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
7394 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7395 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7396 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7397 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
7398 BinOps.push_back(BinOp.getOperand(i));
7399 } else {
7400 // We have an input that is not an extension or another binary
7401 // operation; we'll abort this transformation.
7402 return SDValue();
7403 }
7404 }
7405 }
7406
7407 // Make sure that this is a self-contained cluster of operations (which
7408 // is not quite the same thing as saying that everything has only one
7409 // use).
7410 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7411 if (isa<ConstantSDNode>(Inputs[i]))
7412 continue;
7413
7414 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7415 UE = Inputs[i].getNode()->use_end();
7416 UI != UE; ++UI) {
7417 SDNode *User = *UI;
7418 if (User != N && !Visited.count(User))
7419 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007420
7421 // Make sure that we're not going to promote the non-output-value
7422 // operand(s) or SELECT or SELECT_CC.
7423 // FIXME: Although we could sometimes handle this, and it does occur in
7424 // practice that one of the condition inputs to the select is also one of
7425 // the outputs, we currently can't deal with this.
7426 if (User->getOpcode() == ISD::SELECT) {
7427 if (User->getOperand(0) == Inputs[i])
7428 return SDValue();
7429 } else if (User->getOpcode() == ISD::SELECT_CC) {
7430 if (User->getOperand(0) == Inputs[i] ||
7431 User->getOperand(1) == Inputs[i])
7432 return SDValue();
7433 }
Hal Finkel940ab932014-02-28 00:27:01 +00007434 }
7435 }
7436
7437 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7438 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7439 UE = PromOps[i].getNode()->use_end();
7440 UI != UE; ++UI) {
7441 SDNode *User = *UI;
7442 if (User != N && !Visited.count(User))
7443 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007444
7445 // Make sure that we're not going to promote the non-output-value
7446 // operand(s) or SELECT or SELECT_CC.
7447 // FIXME: Although we could sometimes handle this, and it does occur in
7448 // practice that one of the condition inputs to the select is also one of
7449 // the outputs, we currently can't deal with this.
7450 if (User->getOpcode() == ISD::SELECT) {
7451 if (User->getOperand(0) == PromOps[i])
7452 return SDValue();
7453 } else if (User->getOpcode() == ISD::SELECT_CC) {
7454 if (User->getOperand(0) == PromOps[i] ||
7455 User->getOperand(1) == PromOps[i])
7456 return SDValue();
7457 }
Hal Finkel940ab932014-02-28 00:27:01 +00007458 }
7459 }
7460
7461 // Replace all inputs with the extension operand.
7462 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7463 // Constants may have users outside the cluster of to-be-promoted nodes,
7464 // and so we need to replace those as we do the promotions.
7465 if (isa<ConstantSDNode>(Inputs[i]))
7466 continue;
7467 else
7468 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
7469 }
7470
7471 // Replace all operations (these are all the same, but have a different
7472 // (i1) return type). DAG.getNode will validate that the types of
7473 // a binary operator match, so go through the list in reverse so that
7474 // we've likely promoted both operands first. Any intermediate truncations or
7475 // extensions disappear.
7476 while (!PromOps.empty()) {
7477 SDValue PromOp = PromOps.back();
7478 PromOps.pop_back();
7479
7480 if (PromOp.getOpcode() == ISD::TRUNCATE ||
7481 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
7482 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
7483 PromOp.getOpcode() == ISD::ANY_EXTEND) {
7484 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
7485 PromOp.getOperand(0).getValueType() != MVT::i1) {
7486 // The operand is not yet ready (see comment below).
7487 PromOps.insert(PromOps.begin(), PromOp);
7488 continue;
7489 }
7490
7491 SDValue RepValue = PromOp.getOperand(0);
7492 if (isa<ConstantSDNode>(RepValue))
7493 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
7494
7495 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
7496 continue;
7497 }
7498
7499 unsigned C;
7500 switch (PromOp.getOpcode()) {
7501 default: C = 0; break;
7502 case ISD::SELECT: C = 1; break;
7503 case ISD::SELECT_CC: C = 2; break;
7504 }
7505
7506 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7507 PromOp.getOperand(C).getValueType() != MVT::i1) ||
7508 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7509 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
7510 // The to-be-promoted operands of this node have not yet been
7511 // promoted (this should be rare because we're going through the
7512 // list backward, but if one of the operands has several users in
7513 // this cluster of to-be-promoted nodes, it is possible).
7514 PromOps.insert(PromOps.begin(), PromOp);
7515 continue;
7516 }
7517
7518 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7519 PromOp.getNode()->op_end());
7520
7521 // If there are any constant inputs, make sure they're replaced now.
7522 for (unsigned i = 0; i < 2; ++i)
7523 if (isa<ConstantSDNode>(Ops[C+i]))
7524 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
7525
7526 DAG.ReplaceAllUsesOfValueWith(PromOp,
7527 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1,
7528 Ops.data(), Ops.size()));
7529 }
7530
7531 // Now we're left with the initial truncation itself.
7532 if (N->getOpcode() == ISD::TRUNCATE)
7533 return N->getOperand(0);
7534
7535 // Otherwise, this is a comparison. The operands to be compared have just
7536 // changed type (to i1), but everything else is the same.
7537 return SDValue(N, 0);
7538}
7539
7540SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
7541 DAGCombinerInfo &DCI) const {
7542 SelectionDAG &DAG = DCI.DAG;
7543 SDLoc dl(N);
7544
Hal Finkel940ab932014-02-28 00:27:01 +00007545 // If we're tracking CR bits, we need to be careful that we don't have:
7546 // zext(binary-ops(trunc(x), trunc(y)))
7547 // or
7548 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
7549 // such that we're unnecessarily moving things into CR bits that can more
7550 // efficiently stay in GPRs. Note that if we're not certain that the high
7551 // bits are set as required by the final extension, we still may need to do
7552 // some masking to get the proper behavior.
7553
Hal Finkel46043ed2014-03-01 21:36:57 +00007554 // This same functionality is important on PPC64 when dealing with
7555 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
7556 // the return values of functions. Because it is so similar, it is handled
7557 // here as well.
7558
Hal Finkel940ab932014-02-28 00:27:01 +00007559 if (N->getValueType(0) != MVT::i32 &&
7560 N->getValueType(0) != MVT::i64)
7561 return SDValue();
7562
Hal Finkel46043ed2014-03-01 21:36:57 +00007563 if (!((N->getOperand(0).getValueType() == MVT::i1 &&
7564 PPCSubTarget.useCRBits()) ||
7565 (N->getOperand(0).getValueType() == MVT::i32 &&
7566 PPCSubTarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +00007567 return SDValue();
7568
7569 if (N->getOperand(0).getOpcode() != ISD::AND &&
7570 N->getOperand(0).getOpcode() != ISD::OR &&
7571 N->getOperand(0).getOpcode() != ISD::XOR &&
7572 N->getOperand(0).getOpcode() != ISD::SELECT &&
7573 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
7574 return SDValue();
7575
7576 SmallVector<SDValue, 4> Inputs;
7577 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
7578 SmallPtrSet<SDNode *, 16> Visited;
7579
7580 // Visit all inputs, collect all binary operations (and, or, xor and
7581 // select) that are all fed by truncations.
7582 while (!BinOps.empty()) {
7583 SDValue BinOp = BinOps.back();
7584 BinOps.pop_back();
7585
7586 if (!Visited.insert(BinOp.getNode()))
7587 continue;
7588
7589 PromOps.push_back(BinOp);
7590
7591 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7592 // The condition of the select is not promoted.
7593 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7594 continue;
7595 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7596 continue;
7597
7598 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7599 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7600 Inputs.push_back(BinOp.getOperand(i));
7601 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7602 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7603 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7604 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7605 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
7606 BinOps.push_back(BinOp.getOperand(i));
7607 } else {
7608 // We have an input that is not a truncation or another binary
7609 // operation; we'll abort this transformation.
7610 return SDValue();
7611 }
7612 }
7613 }
7614
7615 // Make sure that this is a self-contained cluster of operations (which
7616 // is not quite the same thing as saying that everything has only one
7617 // use).
7618 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7619 if (isa<ConstantSDNode>(Inputs[i]))
7620 continue;
7621
7622 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7623 UE = Inputs[i].getNode()->use_end();
7624 UI != UE; ++UI) {
7625 SDNode *User = *UI;
7626 if (User != N && !Visited.count(User))
7627 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007628
7629 // Make sure that we're not going to promote the non-output-value
7630 // operand(s) or SELECT or SELECT_CC.
7631 // FIXME: Although we could sometimes handle this, and it does occur in
7632 // practice that one of the condition inputs to the select is also one of
7633 // the outputs, we currently can't deal with this.
7634 if (User->getOpcode() == ISD::SELECT) {
7635 if (User->getOperand(0) == Inputs[i])
7636 return SDValue();
7637 } else if (User->getOpcode() == ISD::SELECT_CC) {
7638 if (User->getOperand(0) == Inputs[i] ||
7639 User->getOperand(1) == Inputs[i])
7640 return SDValue();
7641 }
Hal Finkel940ab932014-02-28 00:27:01 +00007642 }
7643 }
7644
7645 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7646 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7647 UE = PromOps[i].getNode()->use_end();
7648 UI != UE; ++UI) {
7649 SDNode *User = *UI;
7650 if (User != N && !Visited.count(User))
7651 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007652
7653 // Make sure that we're not going to promote the non-output-value
7654 // operand(s) or SELECT or SELECT_CC.
7655 // FIXME: Although we could sometimes handle this, and it does occur in
7656 // practice that one of the condition inputs to the select is also one of
7657 // the outputs, we currently can't deal with this.
7658 if (User->getOpcode() == ISD::SELECT) {
7659 if (User->getOperand(0) == PromOps[i])
7660 return SDValue();
7661 } else if (User->getOpcode() == ISD::SELECT_CC) {
7662 if (User->getOperand(0) == PromOps[i] ||
7663 User->getOperand(1) == PromOps[i])
7664 return SDValue();
7665 }
Hal Finkel940ab932014-02-28 00:27:01 +00007666 }
7667 }
7668
Hal Finkel46043ed2014-03-01 21:36:57 +00007669 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +00007670 bool ReallyNeedsExt = false;
7671 if (N->getOpcode() != ISD::ANY_EXTEND) {
7672 // If all of the inputs are not already sign/zero extended, then
7673 // we'll still need to do that at the end.
7674 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7675 if (isa<ConstantSDNode>(Inputs[i]))
7676 continue;
7677
7678 unsigned OpBits =
7679 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +00007680 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
7681
Hal Finkel940ab932014-02-28 00:27:01 +00007682 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
7683 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00007684 APInt::getHighBitsSet(OpBits,
7685 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +00007686 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +00007687 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
7688 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +00007689 ReallyNeedsExt = true;
7690 break;
7691 }
7692 }
7693 }
7694
7695 // Replace all inputs, either with the truncation operand, or a
7696 // truncation or extension to the final output type.
7697 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7698 // Constant inputs need to be replaced with the to-be-promoted nodes that
7699 // use them because they might have users outside of the cluster of
7700 // promoted nodes.
7701 if (isa<ConstantSDNode>(Inputs[i]))
7702 continue;
7703
7704 SDValue InSrc = Inputs[i].getOperand(0);
7705 if (Inputs[i].getValueType() == N->getValueType(0))
7706 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
7707 else if (N->getOpcode() == ISD::SIGN_EXTEND)
7708 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7709 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
7710 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7711 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7712 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
7713 else
7714 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7715 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
7716 }
7717
7718 // Replace all operations (these are all the same, but have a different
7719 // (promoted) return type). DAG.getNode will validate that the types of
7720 // a binary operator match, so go through the list in reverse so that
7721 // we've likely promoted both operands first.
7722 while (!PromOps.empty()) {
7723 SDValue PromOp = PromOps.back();
7724 PromOps.pop_back();
7725
7726 unsigned C;
7727 switch (PromOp.getOpcode()) {
7728 default: C = 0; break;
7729 case ISD::SELECT: C = 1; break;
7730 case ISD::SELECT_CC: C = 2; break;
7731 }
7732
7733 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7734 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
7735 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7736 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
7737 // The to-be-promoted operands of this node have not yet been
7738 // promoted (this should be rare because we're going through the
7739 // list backward, but if one of the operands has several users in
7740 // this cluster of to-be-promoted nodes, it is possible).
7741 PromOps.insert(PromOps.begin(), PromOp);
7742 continue;
7743 }
7744
7745 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7746 PromOp.getNode()->op_end());
7747
7748 // If this node has constant inputs, then they'll need to be promoted here.
7749 for (unsigned i = 0; i < 2; ++i) {
7750 if (!isa<ConstantSDNode>(Ops[C+i]))
7751 continue;
7752 if (Ops[C+i].getValueType() == N->getValueType(0))
7753 continue;
7754
7755 if (N->getOpcode() == ISD::SIGN_EXTEND)
7756 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7757 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7758 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7759 else
7760 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7761 }
7762
7763 DAG.ReplaceAllUsesOfValueWith(PromOp,
7764 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0),
7765 Ops.data(), Ops.size()));
7766 }
7767
7768 // Now we're left with the initial extension itself.
7769 if (!ReallyNeedsExt)
7770 return N->getOperand(0);
7771
Hal Finkel46043ed2014-03-01 21:36:57 +00007772 // To zero extend, just mask off everything except for the first bit (in the
7773 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +00007774 if (N->getOpcode() == ISD::ZERO_EXTEND)
7775 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00007776 DAG.getConstant(APInt::getLowBitsSet(
7777 N->getValueSizeInBits(0), PromBits),
7778 N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +00007779
7780 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
7781 "Invalid extension type");
7782 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0));
7783 SDValue ShiftCst =
Hal Finkel46043ed2014-03-01 21:36:57 +00007784 DAG.getConstant(N->getValueSizeInBits(0)-PromBits, ShiftAmountTy);
Hal Finkel940ab932014-02-28 00:27:01 +00007785 return DAG.getNode(ISD::SRA, dl, N->getValueType(0),
7786 DAG.getNode(ISD::SHL, dl, N->getValueType(0),
7787 N->getOperand(0), ShiftCst), ShiftCst);
7788}
7789
Duncan Sandsdc2dac12008-11-24 14:53:14 +00007790SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
7791 DAGCombinerInfo &DCI) const {
Dan Gohman57c732b2010-04-21 01:34:56 +00007792 const TargetMachine &TM = getTargetMachine();
Chris Lattnerf4184352006-03-01 04:57:39 +00007793 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007794 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00007795 switch (N->getOpcode()) {
7796 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00007797 case PPCISD::SHL:
7798 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007799 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007800 return N->getOperand(0);
7801 }
7802 break;
7803 case PPCISD::SRL:
7804 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007805 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007806 return N->getOperand(0);
7807 }
7808 break;
7809 case PPCISD::SRA:
7810 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007811 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007812 C->isAllOnesValue()) // -1 >>s V -> -1.
7813 return N->getOperand(0);
7814 }
7815 break;
Hal Finkel940ab932014-02-28 00:27:01 +00007816 case ISD::SIGN_EXTEND:
7817 case ISD::ZERO_EXTEND:
7818 case ISD::ANY_EXTEND:
7819 return DAGCombineExtBoolTrunc(N, DCI);
7820 case ISD::TRUNCATE:
7821 case ISD::SETCC:
7822 case ISD::SELECT_CC:
7823 return DAGCombineTruncBoolExt(N, DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007824 case ISD::FDIV: {
7825 assert(TM.Options.UnsafeFPMath &&
7826 "Reciprocal estimates require UnsafeFPMath");
Scott Michelcf0da6c2009-02-17 22:15:04 +00007827
Hal Finkel2e103312013-04-03 04:01:11 +00007828 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007829 SDValue RV =
7830 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007831 if (RV.getNode() != 0) {
7832 DCI.AddToWorklist(RV.getNode());
7833 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7834 N->getOperand(0), RV);
7835 }
Hal Finkelf96c18e2013-04-04 22:44:12 +00007836 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
7837 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7838 SDValue RV =
7839 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7840 DCI);
7841 if (RV.getNode() != 0) {
7842 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007843 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007844 N->getValueType(0), RV);
7845 DCI.AddToWorklist(RV.getNode());
7846 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7847 N->getOperand(0), RV);
7848 }
7849 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
7850 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7851 SDValue RV =
7852 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7853 DCI);
7854 if (RV.getNode() != 0) {
7855 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007856 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007857 N->getValueType(0), RV,
7858 N->getOperand(1).getOperand(1));
7859 DCI.AddToWorklist(RV.getNode());
7860 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7861 N->getOperand(0), RV);
7862 }
Hal Finkel2e103312013-04-03 04:01:11 +00007863 }
7864
Hal Finkelb0c810f2013-04-03 17:44:56 +00007865 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007866 if (RV.getNode() != 0) {
7867 DCI.AddToWorklist(RV.getNode());
7868 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7869 N->getOperand(0), RV);
7870 }
7871
7872 }
7873 break;
7874 case ISD::FSQRT: {
7875 assert(TM.Options.UnsafeFPMath &&
7876 "Reciprocal estimates require UnsafeFPMath");
7877
7878 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
7879 // reciprocal sqrt.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007880 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007881 if (RV.getNode() != 0) {
7882 DCI.AddToWorklist(RV.getNode());
Hal Finkelb0c810f2013-04-03 17:44:56 +00007883 RV = DAGCombineFastRecip(RV, DCI);
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007884 if (RV.getNode() != 0) {
7885 // Unfortunately, RV is now NaN if the input was exactly 0. Select out
7886 // this case and force the answer to 0.
7887
7888 EVT VT = RV.getValueType();
7889
7890 SDValue Zero = DAG.getConstantFP(0.0, VT.getScalarType());
7891 if (VT.isVector()) {
7892 assert(VT.getVectorNumElements() == 4 && "Unknown vector type");
7893 Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Zero, Zero, Zero, Zero);
7894 }
7895
7896 SDValue ZeroCmp =
7897 DAG.getSetCC(dl, getSetCCResultType(*DAG.getContext(), VT),
7898 N->getOperand(0), Zero, ISD::SETEQ);
7899 DCI.AddToWorklist(ZeroCmp.getNode());
7900 DCI.AddToWorklist(RV.getNode());
7901
7902 RV = DAG.getNode(VT.isVector() ? ISD::VSELECT : ISD::SELECT, dl, VT,
7903 ZeroCmp, Zero, RV);
Hal Finkel2e103312013-04-03 04:01:11 +00007904 return RV;
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007905 }
Hal Finkel2e103312013-04-03 04:01:11 +00007906 }
7907
7908 }
7909 break;
Chris Lattnerf4184352006-03-01 04:57:39 +00007910 case ISD::SINT_TO_FP:
Chris Lattnera35f3062006-06-16 17:34:12 +00007911 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007912 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
7913 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
7914 // We allow the src/dst to be either f32/f64, but the intermediate
7915 // type must be i64.
Owen Anderson9f944592009-08-11 20:47:22 +00007916 if (N->getOperand(0).getValueType() == MVT::i64 &&
7917 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007918 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007919 if (Val.getValueType() == MVT::f32) {
7920 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007921 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007922 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007923
Owen Anderson9f944592009-08-11 20:47:22 +00007924 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007925 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007926 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007927 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007928 if (N->getValueType(0) == MVT::f32) {
7929 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner72733e52008-01-17 07:00:52 +00007930 DAG.getIntPtrConstant(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007931 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007932 }
7933 return Val;
Owen Anderson9f944592009-08-11 20:47:22 +00007934 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007935 // If the intermediate type is i32, we can avoid the load/store here
7936 // too.
Chris Lattnerf4184352006-03-01 04:57:39 +00007937 }
Chris Lattnerf4184352006-03-01 04:57:39 +00007938 }
7939 }
7940 break;
Chris Lattner27f53452006-03-01 05:50:56 +00007941 case ISD::STORE:
7942 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
7943 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnerf5b46f72008-01-18 16:54:56 +00007944 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00007945 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00007946 N->getOperand(1).getValueType() == MVT::i32 &&
7947 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007948 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007949 if (Val.getValueType() == MVT::f32) {
7950 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007951 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007952 }
Owen Anderson9f944592009-08-11 20:47:22 +00007953 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007954 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007955
Hal Finkel60c75102013-04-01 15:37:53 +00007956 SDValue Ops[] = {
7957 N->getOperand(0), Val, N->getOperand(2),
7958 DAG.getValueType(N->getOperand(1).getValueType())
7959 };
7960
7961 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
7962 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
7963 cast<StoreSDNode>(N)->getMemoryVT(),
7964 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00007965 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007966 return Val;
7967 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007968
Chris Lattnera7976d32006-07-10 20:56:58 +00007969 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00007970 if (cast<StoreSDNode>(N)->isUnindexed() &&
7971 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00007972 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00007973 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00007974 N->getOperand(1).getValueType() == MVT::i16 ||
7975 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00007976 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007977 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007978 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00007979 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00007980 if (BSwapOp.getValueType() == MVT::i16)
7981 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00007982
Dan Gohman48b185d2009-09-25 20:36:54 +00007983 SDValue Ops[] = {
7984 N->getOperand(0), BSwapOp, N->getOperand(2),
7985 DAG.getValueType(N->getOperand(1).getValueType())
7986 };
7987 return
7988 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
7989 Ops, array_lengthof(Ops),
7990 cast<StoreSDNode>(N)->getMemoryVT(),
7991 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00007992 }
7993 break;
Hal Finkelcf2e9082013-05-24 23:00:14 +00007994 case ISD::LOAD: {
7995 LoadSDNode *LD = cast<LoadSDNode>(N);
7996 EVT VT = LD->getValueType(0);
7997 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
7998 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
7999 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
8000 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
Hal Finkel40c34782013-09-15 22:09:58 +00008001 (VT == MVT::v16i8 || VT == MVT::v8i16 ||
8002 VT == MVT::v4i32 || VT == MVT::v4f32) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +00008003 LD->getAlignment() < ABIAlignment) {
8004 // This is a type-legal unaligned Altivec load.
8005 SDValue Chain = LD->getChain();
8006 SDValue Ptr = LD->getBasePtr();
8007
8008 // This implements the loading of unaligned vectors as described in
8009 // the venerable Apple Velocity Engine overview. Specifically:
8010 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
8011 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
8012 //
8013 // The general idea is to expand a sequence of one or more unaligned
8014 // loads into a alignment-based permutation-control instruction (lvsl),
8015 // a series of regular vector loads (which always truncate their
8016 // input address to an aligned address), and a series of permutations.
8017 // The results of these permutations are the requested loaded values.
8018 // The trick is that the last "extra" load is not taken from the address
8019 // you might suspect (sizeof(vector) bytes after the last requested
8020 // load), but rather sizeof(vector) - 1 bytes after the last
8021 // requested vector. The point of this is to avoid a page fault if the
Alp Tokercb402912014-01-24 17:20:08 +00008022 // base address happened to be aligned. This works because if the base
Hal Finkelcf2e9082013-05-24 23:00:14 +00008023 // address is aligned, then adding less than a full vector length will
8024 // cause the last vector in the sequence to be (re)loaded. Otherwise,
8025 // the next vector will be fetched as you might suspect was necessary.
8026
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008027 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00008028 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008029 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
8030 // optimization later.
Hal Finkelcf2e9082013-05-24 23:00:14 +00008031 SDValue PermCntl = BuildIntrinsicOp(Intrinsic::ppc_altivec_lvsl, Ptr,
8032 DAG, dl, MVT::v16i8);
8033
8034 // Refine the alignment of the original load (a "new" load created here
8035 // which was identical to the first except for the alignment would be
8036 // merged with the existing node regardless).
8037 MachineFunction &MF = DAG.getMachineFunction();
8038 MachineMemOperand *MMO =
8039 MF.getMachineMemOperand(LD->getPointerInfo(),
8040 LD->getMemOperand()->getFlags(),
8041 LD->getMemoryVT().getStoreSize(),
8042 ABIAlignment);
8043 LD->refineAlignment(MMO);
8044 SDValue BaseLoad = SDValue(LD, 0);
8045
8046 // Note that the value of IncOffset (which is provided to the next
8047 // load's pointer info offset value, and thus used to calculate the
8048 // alignment), and the value of IncValue (which is actually used to
8049 // increment the pointer value) are different! This is because we
8050 // require the next load to appear to be aligned, even though it
8051 // is actually offset from the base pointer by a lesser amount.
8052 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +00008053 int IncValue = IncOffset;
8054
8055 // Walk (both up and down) the chain looking for another load at the real
8056 // (aligned) offset (the alignment of the other load does not matter in
8057 // this case). If found, then do not use the offset reduction trick, as
8058 // that will prevent the loads from being later combined (as they would
8059 // otherwise be duplicates).
8060 if (!findConsecutiveLoad(LD, DAG))
8061 --IncValue;
8062
Hal Finkelcf2e9082013-05-24 23:00:14 +00008063 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
8064 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
8065
Hal Finkelcf2e9082013-05-24 23:00:14 +00008066 SDValue ExtraLoad =
8067 DAG.getLoad(VT, dl, Chain, Ptr,
8068 LD->getPointerInfo().getWithOffset(IncOffset),
8069 LD->isVolatile(), LD->isNonTemporal(),
8070 LD->isInvariant(), ABIAlignment);
8071
8072 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8073 BaseLoad.getValue(1), ExtraLoad.getValue(1));
8074
8075 if (BaseLoad.getValueType() != MVT::v4i32)
8076 BaseLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, BaseLoad);
8077
8078 if (ExtraLoad.getValueType() != MVT::v4i32)
8079 ExtraLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ExtraLoad);
8080
8081 SDValue Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
8082 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
8083
8084 if (VT != MVT::v4i32)
8085 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
8086
8087 // Now we need to be really careful about how we update the users of the
8088 // original load. We cannot just call DCI.CombineTo (or
8089 // DAG.ReplaceAllUsesWith for that matter), because the load still has
8090 // uses created here (the permutation for example) that need to stay.
8091 SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
8092 while (UI != UE) {
8093 SDUse &Use = UI.getUse();
8094 SDNode *User = *UI;
8095 // Note: BaseLoad is checked here because it might not be N, but a
8096 // bitcast of N.
8097 if (User == Perm.getNode() || User == BaseLoad.getNode() ||
8098 User == TF.getNode() || Use.getResNo() > 1) {
8099 ++UI;
8100 continue;
8101 }
8102
8103 SDValue To = Use.getResNo() ? TF : Perm;
8104 ++UI;
8105
8106 SmallVector<SDValue, 8> Ops;
8107 for (SDNode::op_iterator O = User->op_begin(),
8108 OE = User->op_end(); O != OE; ++O) {
8109 if (*O == Use)
8110 Ops.push_back(To);
8111 else
8112 Ops.push_back(*O);
8113 }
8114
8115 DAG.UpdateNodeOperands(User, Ops.data(), Ops.size());
8116 }
8117
8118 return SDValue(N, 0);
8119 }
8120 }
8121 break;
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008122 case ISD::INTRINSIC_WO_CHAIN:
8123 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() ==
8124 Intrinsic::ppc_altivec_lvsl &&
8125 N->getOperand(1)->getOpcode() == ISD::ADD) {
8126 SDValue Add = N->getOperand(1);
8127
8128 if (DAG.MaskedValueIsZero(Add->getOperand(1),
8129 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
8130 Add.getValueType().getScalarType().getSizeInBits()))) {
8131 SDNode *BasePtr = Add->getOperand(0).getNode();
8132 for (SDNode::use_iterator UI = BasePtr->use_begin(),
8133 UE = BasePtr->use_end(); UI != UE; ++UI) {
8134 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8135 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
8136 Intrinsic::ppc_altivec_lvsl) {
8137 // We've found another LVSL, and this address if an aligned
8138 // multiple of that one. The results will be the same, so use the
8139 // one we've just found instead.
8140
8141 return SDValue(*UI, 0);
8142 }
8143 }
8144 }
8145 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +00008146
8147 break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008148 case ISD::BSWAP:
8149 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008150 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +00008151 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008152 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
8153 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008154 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008155 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008156 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00008157 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +00008158 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008159 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +00008160 LD->getChain(), // Chain
8161 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008162 DAG.getValueType(N->getValueType(0)) // VT
8163 };
Dan Gohman48b185d2009-09-25 20:36:54 +00008164 SDValue BSLoad =
8165 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +00008166 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
8167 MVT::i64 : MVT::i32, MVT::Other),
Hal Finkel93492fa2013-03-28 19:43:12 +00008168 Ops, 3, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008169
Scott Michelcf0da6c2009-02-17 22:15:04 +00008170 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008171 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +00008172 if (N->getValueType(0) == MVT::i16)
8173 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008174
Chris Lattnera7976d32006-07-10 20:56:58 +00008175 // First, combine the bswap away. This makes the value produced by the
8176 // load dead.
8177 DCI.CombineTo(N, ResVal);
8178
8179 // Next, combine the load away, we give it a bogus result value but a real
8180 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008181 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00008182
Chris Lattnera7976d32006-07-10 20:56:58 +00008183 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008184 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008185 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008186
Chris Lattner27f53452006-03-01 05:50:56 +00008187 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008188 case PPCISD::VCMP: {
8189 // If a VCMPo node already exists with exactly the same operands as this
8190 // node, use its result instead of this node (VCMPo computes both a CR6 and
8191 // a normal output).
8192 //
8193 if (!N->getOperand(0).hasOneUse() &&
8194 !N->getOperand(1).hasOneUse() &&
8195 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00008196
Chris Lattnerd4058a52006-03-31 06:02:07 +00008197 // Scan all of the users of the LHS, looking for VCMPo's that match.
8198 SDNode *VCMPoNode = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008199
Gabor Greiff304a7a2008-08-28 21:40:38 +00008200 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +00008201 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
8202 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008203 if (UI->getOpcode() == PPCISD::VCMPo &&
8204 UI->getOperand(1) == N->getOperand(1) &&
8205 UI->getOperand(2) == N->getOperand(2) &&
8206 UI->getOperand(0) == N->getOperand(0)) {
8207 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008208 break;
8209 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008210
Chris Lattner518834c2006-04-18 18:28:22 +00008211 // If there is no VCMPo node, or if the flag value has a single use, don't
8212 // transform this.
8213 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
8214 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008215
8216 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +00008217 // chain, this transformation is more complex. Note that multiple things
8218 // could use the value result, which we should ignore.
8219 SDNode *FlagUser = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008220 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner518834c2006-04-18 18:28:22 +00008221 FlagUser == 0; ++UI) {
8222 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008223 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +00008224 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008225 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +00008226 FlagUser = User;
8227 break;
8228 }
8229 }
8230 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008231
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008232 // If the user is a MFOCRF instruction, we know this is safe.
8233 // Otherwise we give up for right now.
8234 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008235 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +00008236 }
8237 break;
8238 }
Hal Finkel940ab932014-02-28 00:27:01 +00008239 case ISD::BRCOND: {
8240 SDValue Cond = N->getOperand(1);
8241 SDValue Target = N->getOperand(2);
8242
8243 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8244 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
8245 Intrinsic::ppc_is_decremented_ctr_nonzero) {
8246
8247 // We now need to make the intrinsic dead (it cannot be instruction
8248 // selected).
8249 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
8250 assert(Cond.getNode()->hasOneUse() &&
8251 "Counter decrement has more than one use");
8252
8253 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
8254 N->getOperand(0), Target);
8255 }
8256 }
8257 break;
Chris Lattner9754d142006-04-18 17:59:36 +00008258 case ISD::BR_CC: {
8259 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008260 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +00008261 // lowering is done pre-legalize, because the legalizer lowers the predicate
8262 // compare down to code that is difficult to reassemble.
8263 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008264 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +00008265
8266 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
8267 // value. If so, pass-through the AND to get to the intrinsic.
8268 if (LHS.getOpcode() == ISD::AND &&
8269 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8270 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
8271 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8272 isa<ConstantSDNode>(LHS.getOperand(1)) &&
8273 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
8274 isZero())
8275 LHS = LHS.getOperand(0);
8276
8277 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8278 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
8279 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8280 isa<ConstantSDNode>(RHS)) {
8281 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
8282 "Counter decrement comparison is not EQ or NE");
8283
8284 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
8285 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
8286 (CC == ISD::SETNE && !Val);
8287
8288 // We now need to make the intrinsic dead (it cannot be instruction
8289 // selected).
8290 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
8291 assert(LHS.getNode()->hasOneUse() &&
8292 "Counter decrement has more than one use");
8293
8294 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
8295 N->getOperand(0), N->getOperand(4));
8296 }
8297
Chris Lattner9754d142006-04-18 17:59:36 +00008298 int CompareOpc;
8299 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008300
Chris Lattner9754d142006-04-18 17:59:36 +00008301 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8302 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
8303 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
8304 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00008305
Chris Lattner9754d142006-04-18 17:59:36 +00008306 // If this is a comparison against something other than 0/1, then we know
8307 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008308 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00008309 if (Val != 0 && Val != 1) {
8310 if (CC == ISD::SETEQ) // Cond never true, remove branch.
8311 return N->getOperand(0);
8312 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +00008313 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +00008314 N->getOperand(0), N->getOperand(4));
8315 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008316
Chris Lattner9754d142006-04-18 17:59:36 +00008317 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008318
Chris Lattner9754d142006-04-18 17:59:36 +00008319 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008320 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008321 LHS.getOperand(2), // LHS of compare
8322 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +00008323 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008324 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00008325 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00008326 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008327
Chris Lattner9754d142006-04-18 17:59:36 +00008328 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008329 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +00008330 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +00008331 default: // Can't happen, don't crash on invalid number though.
8332 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008333 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +00008334 break;
8335 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008336 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +00008337 break;
8338 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008339 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +00008340 break;
8341 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008342 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +00008343 break;
8344 }
8345
Owen Anderson9f944592009-08-11 20:47:22 +00008346 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
8347 DAG.getConstant(CompOpc, MVT::i32),
8348 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +00008349 N->getOperand(4), CompNode.getValue(1));
8350 }
8351 break;
8352 }
Chris Lattnerf4184352006-03-01 04:57:39 +00008353 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008354
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008355 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +00008356}
8357
Chris Lattner4211ca92006-04-14 06:01:58 +00008358//===----------------------------------------------------------------------===//
8359// Inline Assembly Support
8360//===----------------------------------------------------------------------===//
8361
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008362void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Scott Michelcf0da6c2009-02-17 22:15:04 +00008363 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +00008364 APInt &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +00008365 const SelectionDAG &DAG,
Chris Lattnerc5287c02006-04-02 06:26:07 +00008366 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00008367 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +00008368 switch (Op.getOpcode()) {
8369 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008370 case PPCISD::LBRX: {
8371 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +00008372 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +00008373 KnownZero = 0xFFFF0000;
8374 break;
8375 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008376 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00008377 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +00008378 default: break;
8379 case Intrinsic::ppc_altivec_vcmpbfp_p:
8380 case Intrinsic::ppc_altivec_vcmpeqfp_p:
8381 case Intrinsic::ppc_altivec_vcmpequb_p:
8382 case Intrinsic::ppc_altivec_vcmpequh_p:
8383 case Intrinsic::ppc_altivec_vcmpequw_p:
8384 case Intrinsic::ppc_altivec_vcmpgefp_p:
8385 case Intrinsic::ppc_altivec_vcmpgtfp_p:
8386 case Intrinsic::ppc_altivec_vcmpgtsb_p:
8387 case Intrinsic::ppc_altivec_vcmpgtsh_p:
8388 case Intrinsic::ppc_altivec_vcmpgtsw_p:
8389 case Intrinsic::ppc_altivec_vcmpgtub_p:
8390 case Intrinsic::ppc_altivec_vcmpgtuh_p:
8391 case Intrinsic::ppc_altivec_vcmpgtuw_p:
8392 KnownZero = ~1U; // All bits but the low one are known to be zero.
8393 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008394 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008395 }
8396 }
8397}
8398
8399
Chris Lattnerd6855142007-03-25 02:14:49 +00008400/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +00008401/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008402PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00008403PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
8404 if (Constraint.size() == 1) {
8405 switch (Constraint[0]) {
8406 default: break;
8407 case 'b':
8408 case 'r':
8409 case 'f':
8410 case 'v':
8411 case 'y':
8412 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +00008413 case 'Z':
8414 // FIXME: While Z does indicate a memory constraint, it specifically
8415 // indicates an r+r address (used in conjunction with the 'y' modifier
8416 // in the replacement string). Currently, we're forcing the base
8417 // register to be r0 in the asm printer (which is interpreted as zero)
8418 // and forming the complete address in the second register. This is
8419 // suboptimal.
8420 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +00008421 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008422 } else if (Constraint == "wc") { // individual CR bits.
8423 return C_RegisterClass;
Hal Finkel27774d92014-03-13 07:58:58 +00008424 } else if (Constraint == "wa" || Constraint == "wd" ||
8425 Constraint == "wf" || Constraint == "ws") {
8426 return C_RegisterClass; // VSX registers.
Chris Lattnerd6855142007-03-25 02:14:49 +00008427 }
8428 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +00008429}
8430
John Thompsone8360b72010-10-29 17:29:13 +00008431/// Examine constraint type and operand type and determine a weight value.
8432/// This object must already have been set up with the operand type
8433/// and the current alternative constraint selected.
8434TargetLowering::ConstraintWeight
8435PPCTargetLowering::getSingleConstraintMatchWeight(
8436 AsmOperandInfo &info, const char *constraint) const {
8437 ConstraintWeight weight = CW_Invalid;
8438 Value *CallOperandVal = info.CallOperandVal;
8439 // If we don't have a value, we can't do a match,
8440 // but allow it at the lowest weight.
8441 if (CallOperandVal == NULL)
8442 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00008443 Type *type = CallOperandVal->getType();
Hal Finkel6aca2372014-03-02 18:23:39 +00008444
John Thompsone8360b72010-10-29 17:29:13 +00008445 // Look at the constraint type.
Hal Finkel6aca2372014-03-02 18:23:39 +00008446 if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
8447 return CW_Register; // an individual CR bit.
Hal Finkel27774d92014-03-13 07:58:58 +00008448 else if ((StringRef(constraint) == "wa" ||
8449 StringRef(constraint) == "wd" ||
8450 StringRef(constraint) == "wf") &&
8451 type->isVectorTy())
8452 return CW_Register;
8453 else if (StringRef(constraint) == "ws" && type->isDoubleTy())
8454 return CW_Register;
Hal Finkel6aca2372014-03-02 18:23:39 +00008455
John Thompsone8360b72010-10-29 17:29:13 +00008456 switch (*constraint) {
8457 default:
8458 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8459 break;
8460 case 'b':
8461 if (type->isIntegerTy())
8462 weight = CW_Register;
8463 break;
8464 case 'f':
8465 if (type->isFloatTy())
8466 weight = CW_Register;
8467 break;
8468 case 'd':
8469 if (type->isDoubleTy())
8470 weight = CW_Register;
8471 break;
8472 case 'v':
8473 if (type->isVectorTy())
8474 weight = CW_Register;
8475 break;
8476 case 'y':
8477 weight = CW_Register;
8478 break;
Hal Finkel4f24c622012-11-05 18:18:42 +00008479 case 'Z':
8480 weight = CW_Memory;
8481 break;
John Thompsone8360b72010-10-29 17:29:13 +00008482 }
8483 return weight;
8484}
8485
Scott Michelcf0da6c2009-02-17 22:15:04 +00008486std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner584a11a2006-11-02 01:44:04 +00008487PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00008488 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +00008489 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +00008490 // GCC RS6000 Constraint Letters
8491 switch (Constraint[0]) {
8492 case 'b': // R1-R31
Hal Finkel638a9fa2013-03-19 18:51:05 +00008493 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
8494 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
8495 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008496 case 'r': // R0-R31
Owen Anderson9f944592009-08-11 20:47:22 +00008497 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +00008498 return std::make_pair(0U, &PPC::G8RCRegClass);
8499 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008500 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008501 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +00008502 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008503 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +00008504 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008505 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008506 case 'v':
Craig Topperabadc662012-04-20 06:31:50 +00008507 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008508 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +00008509 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008510 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008511 } else if (Constraint == "wc") { // an individual CR bit.
8512 return std::make_pair(0U, &PPC::CRBITRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +00008513 } else if (Constraint == "wa" || Constraint == "wd" ||
Hal Finkel19be5062014-03-29 05:29:01 +00008514 Constraint == "wf") {
Hal Finkel27774d92014-03-13 07:58:58 +00008515 return std::make_pair(0U, &PPC::VSRCRegClass);
Hal Finkel19be5062014-03-29 05:29:01 +00008516 } else if (Constraint == "ws") {
8517 return std::make_pair(0U, &PPC::VSFRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008518 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008519
Hal Finkelb176acb2013-08-03 12:25:10 +00008520 std::pair<unsigned, const TargetRegisterClass*> R =
8521 TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8522
8523 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
8524 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
8525 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
8526 // register.
8527 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
8528 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
8529 if (R.first && VT == MVT::i64 && PPCSubTarget.isPPC64() &&
8530 PPC::GPRCRegClass.contains(R.first)) {
8531 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
8532 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +00008533 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +00008534 &PPC::G8RCRegClass);
8535 }
8536
8537 return R;
Chris Lattner01513612006-01-31 19:20:21 +00008538}
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008539
Chris Lattner584a11a2006-11-02 01:44:04 +00008540
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008541/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +00008542/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +00008543void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00008544 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008545 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00008546 SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008547 SDValue Result(0,0);
Eric Christopher0713a9d2011-06-08 23:55:35 +00008548
Eric Christopherde9399b2011-06-02 23:16:42 +00008549 // Only support length 1 constraints.
8550 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008551
Eric Christopherde9399b2011-06-02 23:16:42 +00008552 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008553 switch (Letter) {
8554 default: break;
8555 case 'I':
8556 case 'J':
8557 case 'K':
8558 case 'L':
8559 case 'M':
8560 case 'N':
8561 case 'O':
8562 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +00008563 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008564 if (!CST) return; // Must be an immediate to match.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008565 unsigned Value = CST->getZExtValue();
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008566 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008567 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008568 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008569 if ((short)Value == (int)Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008570 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008571 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008572 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
8573 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008574 if ((short)Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008575 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008576 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008577 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008578 if ((Value >> 16) == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008579 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008580 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008581 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008582 if (Value > 31)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008583 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008584 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008585 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008586 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008587 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008588 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008589 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008590 if (Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008591 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008592 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008593 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008594 if ((short)-Value == (int)-Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008595 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008596 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008597 }
8598 break;
8599 }
8600 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008601
Gabor Greiff304a7a2008-08-28 21:40:38 +00008602 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008603 Ops.push_back(Result);
8604 return;
8605 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008606
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008607 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +00008608 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008609}
Evan Cheng2dd2c652006-03-13 23:20:37 +00008610
Chris Lattner1eb94d92007-03-30 23:15:24 +00008611// isLegalAddressingMode - Return true if the addressing mode represented
8612// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008613bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00008614 Type *Ty) const {
Chris Lattner1eb94d92007-03-30 23:15:24 +00008615 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelcf0da6c2009-02-17 22:15:04 +00008616
Chris Lattner1eb94d92007-03-30 23:15:24 +00008617 // PPC allows a sign-extended 16-bit immediate field.
8618 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
8619 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008620
Chris Lattner1eb94d92007-03-30 23:15:24 +00008621 // No global is ever allowed as a base.
8622 if (AM.BaseGV)
8623 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008624
8625 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +00008626 switch (AM.Scale) {
8627 case 0: // "r+i" or just "i", depending on HasBaseReg.
8628 break;
8629 case 1:
8630 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
8631 return false;
8632 // Otherwise we have r+r or r+i.
8633 break;
8634 case 2:
8635 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
8636 return false;
8637 // Allow 2*r as r+r.
8638 break;
Chris Lattner19ccd622007-04-09 22:10:05 +00008639 default:
8640 // No other scales are supported.
8641 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +00008642 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008643
Chris Lattner1eb94d92007-03-30 23:15:24 +00008644 return true;
8645}
8646
Dan Gohman21cea8a2010-04-17 15:26:15 +00008647SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
8648 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00008649 MachineFunction &MF = DAG.getMachineFunction();
8650 MachineFrameInfo *MFI = MF.getFrameInfo();
8651 MFI->setReturnAddressIsTaken(true);
8652
Bill Wendling908bf812014-01-06 00:43:20 +00008653 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008654 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008655
Andrew Trickef9de2a2013-05-25 02:42:55 +00008656 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008657 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +00008658
Dale Johannesen81bfca72010-05-03 22:59:34 +00008659 // Make sure the function does not optimize away the store of the RA to
8660 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +00008661 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008662 FuncInfo->setLRStoreRequired();
8663 bool isPPC64 = PPCSubTarget.isPPC64();
8664 bool isDarwinABI = PPCSubTarget.isDarwinABI();
8665
8666 if (Depth > 0) {
8667 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8668 SDValue Offset =
Wesley Peck527da1b2010-11-23 03:31:01 +00008669
Anton Korobeynikov2f931282011-01-10 12:39:04 +00008670 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen81bfca72010-05-03 22:59:34 +00008671 isPPC64? MVT::i64 : MVT::i32);
8672 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
8673 DAG.getNode(ISD::ADD, dl, getPointerTy(),
8674 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008675 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008676 }
Chris Lattnerf6a81562007-12-08 06:59:59 +00008677
Chris Lattnerf6a81562007-12-08 06:59:59 +00008678 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008679 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008680 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008681 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +00008682}
8683
Dan Gohman21cea8a2010-04-17 15:26:15 +00008684SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
8685 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008686 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008687 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00008688
Owen Anderson53aa7a92009-08-10 22:56:29 +00008689 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00008690 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008691
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008692 MachineFunction &MF = DAG.getMachineFunction();
8693 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008694 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +00008695
8696 // Naked functions never have a frame pointer, and so we use r1. For all
8697 // other functions, this decision must be delayed until during PEI.
8698 unsigned FrameReg;
8699 if (MF.getFunction()->getAttributes().hasAttribute(
8700 AttributeSet::FunctionIndex, Attribute::Naked))
8701 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
8702 else
8703 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
8704
Dale Johannesen81bfca72010-05-03 22:59:34 +00008705 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
8706 PtrVT);
8707 while (Depth--)
8708 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008709 FrameAddr, MachinePointerInfo(), false, false,
8710 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008711 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008712}
Dan Gohmanc14e5222008-10-21 03:41:46 +00008713
8714bool
8715PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8716 // The PowerPC target isn't yet aware of offsets.
8717 return false;
8718}
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008719
Evan Chengd9929f02010-04-01 20:10:42 +00008720/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +00008721/// and store operations as a result of memset, memcpy, and memmove
8722/// lowering. If DstAlign is zero that means it's safe to destination
8723/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
8724/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +00008725/// probably because the source does not need to be loaded. If 'IsMemset' is
8726/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
8727/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
8728/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +00008729/// It returns EVT::Other if the type should be determined using generic
8730/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +00008731EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
8732 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00008733 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00008734 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +00008735 MachineFunction &MF) const {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008736 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +00008737 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008738 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00008739 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008740 }
8741}
Hal Finkel88ed4e32012-04-01 19:23:08 +00008742
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008743bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +00008744 unsigned,
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008745 bool *Fast) const {
8746 if (DisablePPCUnaligned)
8747 return false;
8748
8749 // PowerPC supports unaligned memory access for simple non-vector types.
8750 // Although accessing unaligned addresses is not as efficient as accessing
8751 // aligned addresses, it is generally more efficient than manual expansion,
8752 // and generally only traps for software emulation when crossing page
8753 // boundaries.
8754
8755 if (!VT.isSimple())
8756 return false;
8757
Hal Finkel6e28e6a2014-03-26 19:39:09 +00008758 if (VT.getSimpleVT().isVector()) {
8759 if (PPCSubTarget.hasVSX()) {
8760 if (VT != MVT::v2f64 && VT != MVT::v2i64)
8761 return false;
8762 } else {
8763 return false;
8764 }
8765 }
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008766
8767 if (VT == MVT::ppcf128)
8768 return false;
8769
8770 if (Fast)
8771 *Fast = true;
8772
8773 return true;
8774}
8775
Stephen Lin73de7bf2013-07-09 18:16:56 +00008776bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
8777 VT = VT.getScalarType();
8778
Hal Finkel0a479ae2012-06-22 00:49:52 +00008779 if (!VT.isSimple())
8780 return false;
8781
8782 switch (VT.getSimpleVT().SimpleTy) {
8783 case MVT::f32:
8784 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +00008785 return true;
8786 default:
8787 break;
8788 }
8789
8790 return false;
8791}
8792
Hal Finkel88ed4e32012-04-01 19:23:08 +00008793Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel21442b22013-09-11 23:05:25 +00008794 if (DisableILPPref || PPCSubTarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008795 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +00008796
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008797 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +00008798}
8799
Bill Schmidt0cf702f2013-07-30 00:50:39 +00008800// Create a fast isel object.
8801FastISel *
8802PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
8803 const TargetLibraryInfo *LibInfo) const {
8804 return PPC::createFastISel(FuncInfo, LibInfo);
8805}