blob: 05e74fb049fa6984730901c4a7b7c38a4d365f61 [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
508 if (TM.Options.UnsafeFPMath) {
509 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);
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000535 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000536
Hal Finkel70381a72012-08-04 14:10:46 +0000537 if (Subtarget->has64BitSupport()) {
Hal Finkel322e41a2012-04-01 20:08:17 +0000538 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel70381a72012-08-04 14:10:46 +0000539 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
540 }
Hal Finkel322e41a2012-04-01 20:08:17 +0000541
Eli Friedman7dfa7912011-08-29 18:23:02 +0000542 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
543 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkel1b5ff082012-12-25 17:22:53 +0000544 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
545 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000546
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000547 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidta76bf5a2013-04-23 18:49:44 +0000548 // Altivec instructions set fields to all zeros or all ones.
549 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000550
Evan Cheng39e90022012-07-02 22:39:56 +0000551 if (isPPC64) {
Chris Lattner454436d2006-10-18 01:20:43 +0000552 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000553 setExceptionPointerRegister(PPC::X3);
554 setExceptionSelectorRegister(PPC::X4);
555 } else {
Chris Lattner454436d2006-10-18 01:20:43 +0000556 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000557 setExceptionPointerRegister(PPC::R3);
558 setExceptionSelectorRegister(PPC::R4);
559 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000560
Chris Lattnerf4184352006-03-01 04:57:39 +0000561 // We have target-specific dag combine patterns for the following nodes:
562 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000563 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000564 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000565 setTargetDAGCombine(ISD::BR_CC);
Hal Finkel940ab932014-02-28 00:27:01 +0000566 if (Subtarget->useCRBits())
567 setTargetDAGCombine(ISD::BRCOND);
Chris Lattnera7976d32006-07-10 20:56:58 +0000568 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000569 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000570
Hal Finkel46043ed2014-03-01 21:36:57 +0000571 setTargetDAGCombine(ISD::SIGN_EXTEND);
572 setTargetDAGCombine(ISD::ZERO_EXTEND);
573 setTargetDAGCombine(ISD::ANY_EXTEND);
574
Hal Finkel940ab932014-02-28 00:27:01 +0000575 if (Subtarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000576 setTargetDAGCombine(ISD::TRUNCATE);
577 setTargetDAGCombine(ISD::SETCC);
578 setTargetDAGCombine(ISD::SELECT_CC);
579 }
580
Hal Finkel2e103312013-04-03 04:01:11 +0000581 // Use reciprocal estimates.
582 if (TM.Options.UnsafeFPMath) {
583 setTargetDAGCombine(ISD::FDIV);
584 setTargetDAGCombine(ISD::FSQRT);
585 }
586
Dale Johannesen10432e52007-10-19 00:59:18 +0000587 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng39e90022012-07-02 22:39:56 +0000588 if (Subtarget->isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000589 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000590 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
591 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000592 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
593 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000594 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
595 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
596 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
597 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
598 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000599 }
600
Hal Finkel940ab932014-02-28 00:27:01 +0000601 // With 32 condition bits, we don't need to sink (and duplicate) compares
602 // aggressively in CodeGenPrep.
603 if (Subtarget->useCRBits())
604 setHasMultipleConditionRegisters();
605
Hal Finkel65298572011-10-17 18:53:03 +0000606 setMinFunctionAlignment(2);
607 if (PPCSubTarget.isDarwin())
608 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000609
Evan Cheng39e90022012-07-02 22:39:56 +0000610 if (isPPC64 && Subtarget->isJITCodeModel())
611 // Temporary workaround for the inability of PPC64 JIT to handle jump
612 // tables.
613 setSupportJumpTables(false);
614
Eli Friedman30a49e92011-08-03 21:06:02 +0000615 setInsertFencesForAtomic(true);
616
Hal Finkel21442b22013-09-11 23:05:25 +0000617 if (Subtarget->enableMachineScheduler())
618 setSchedulingPreference(Sched::Source);
619 else
620 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000621
Chris Lattnerf22556d2005-08-16 17:14:42 +0000622 computeRegisterProperties();
Hal Finkel742b5352012-08-28 16:12:39 +0000623
624 // The Freescale cores does better with aggressive inlining of memcpy and
625 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
626 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
627 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000628 MaxStoresPerMemset = 32;
629 MaxStoresPerMemsetOptSize = 16;
630 MaxStoresPerMemcpy = 32;
631 MaxStoresPerMemcpyOptSize = 8;
632 MaxStoresPerMemmove = 32;
633 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel742b5352012-08-28 16:12:39 +0000634
635 setPrefFunctionAlignment(4);
Hal Finkel742b5352012-08-28 16:12:39 +0000636 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000637}
638
Hal Finkel262a2242013-09-12 23:20:06 +0000639/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
640/// the desired ByVal argument alignment.
641static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
642 unsigned MaxMaxAlign) {
643 if (MaxAlign == MaxMaxAlign)
644 return;
645 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
646 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
647 MaxAlign = 32;
648 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
649 MaxAlign = 16;
650 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
651 unsigned EltAlign = 0;
652 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
653 if (EltAlign > MaxAlign)
654 MaxAlign = EltAlign;
655 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
656 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
657 unsigned EltAlign = 0;
658 getMaxByValAlign(STy->getElementType(i), EltAlign, MaxMaxAlign);
659 if (EltAlign > MaxAlign)
660 MaxAlign = EltAlign;
661 if (MaxAlign == MaxMaxAlign)
662 break;
663 }
664 }
665}
666
Dale Johannesencbde4c22008-02-28 22:31:51 +0000667/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
668/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000669unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000670 // Darwin passes everything on 4 byte boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000671 if (PPCSubTarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000672 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000673
674 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000675 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000676 unsigned Align = PPCSubTarget.isPPC64() ? 8 : 4;
677 if (PPCSubTarget.hasAltivec() || PPCSubTarget.hasQPX())
678 getMaxByValAlign(Ty, Align, PPCSubTarget.hasQPX() ? 32 : 16);
679 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000680}
681
Chris Lattner347ed8a2006-01-09 23:52:17 +0000682const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
683 switch (Opcode) {
684 default: return 0;
Evan Cheng32e376f2008-07-12 02:23:19 +0000685 case PPCISD::FSEL: return "PPCISD::FSEL";
686 case PPCISD::FCFID: return "PPCISD::FCFID";
687 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
688 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000689 case PPCISD::FRE: return "PPCISD::FRE";
690 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000691 case PPCISD::STFIWX: return "PPCISD::STFIWX";
692 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
693 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
694 case PPCISD::VPERM: return "PPCISD::VPERM";
695 case PPCISD::Hi: return "PPCISD::Hi";
696 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000697 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller79fef932009-12-18 13:00:15 +0000698 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
699 case PPCISD::LOAD: return "PPCISD::LOAD";
700 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000701 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
702 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
703 case PPCISD::SRL: return "PPCISD::SRL";
704 case PPCISD::SRA: return "PPCISD::SRA";
705 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000706 case PPCISD::CALL: return "PPCISD::CALL";
707 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000708 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000709 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng32e376f2008-07-12 02:23:19 +0000710 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel756810f2013-03-21 21:37:52 +0000711 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
712 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000713 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng32e376f2008-07-12 02:23:19 +0000714 case PPCISD::VCMP: return "PPCISD::VCMP";
715 case PPCISD::VCMPo: return "PPCISD::VCMPo";
716 case PPCISD::LBRX: return "PPCISD::LBRX";
717 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng32e376f2008-07-12 02:23:19 +0000718 case PPCISD::LARX: return "PPCISD::LARX";
719 case PPCISD::STCX: return "PPCISD::STCX";
720 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +0000721 case PPCISD::BDNZ: return "PPCISD::BDNZ";
722 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000723 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000724 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000725 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +0000726 case PPCISD::CR6SET: return "PPCISD::CR6SET";
727 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34627e32012-11-27 17:35:46 +0000728 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
729 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
730 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Roman Divacky32143e22013-12-20 18:08:54 +0000731 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000732 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
733 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000734 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000735 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
736 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
737 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000738 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
739 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
740 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
741 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
742 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +0000743 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +0000744 case PPCISD::SC: return "PPCISD::SC";
Chris Lattner347ed8a2006-01-09 23:52:17 +0000745 }
746}
747
Matt Arsenault758659232013-05-18 00:21:46 +0000748EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000749 if (!VT.isVector())
Hal Finkel940ab932014-02-28 00:27:01 +0000750 return PPCSubTarget.useCRBits() ? MVT::i1 : MVT::i32;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000751 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000752}
753
Chris Lattner4211ca92006-04-14 06:01:58 +0000754//===----------------------------------------------------------------------===//
755// Node matching predicates, for use by the tblgen matching code.
756//===----------------------------------------------------------------------===//
757
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000758/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000759static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000760 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000761 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000762 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000763 // Maybe this has already been legalized into the constant pool?
764 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000765 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000766 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000767 }
768 return false;
769}
770
Chris Lattnere8b83b42006-04-06 17:23:16 +0000771/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
772/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000773static bool isConstantOrUndef(int Op, int Val) {
774 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000775}
776
777/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
778/// VPKUHUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000779bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000780 if (!isUnary) {
781 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000782 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000783 return false;
784 } else {
785 for (unsigned i = 0; i != 8; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000786 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
787 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000788 return false;
789 }
Chris Lattner1d338192006-04-06 18:26:28 +0000790 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000791}
792
793/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
794/// VPKUWUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000795bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000796 if (!isUnary) {
797 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000798 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
799 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000800 return false;
801 } else {
802 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000803 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
804 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
805 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
806 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000807 return false;
808 }
Chris Lattner1d338192006-04-06 18:26:28 +0000809 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000810}
811
Chris Lattnerf38e0332006-04-06 22:02:42 +0000812/// isVMerge - Common function, used to match vmrg* shuffles.
813///
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000814static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +0000815 unsigned LHSStart, unsigned RHSStart) {
Owen Anderson9f944592009-08-11 20:47:22 +0000816 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000817 "PPC only supports shuffles by bytes!");
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000818 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
819 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000820
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000821 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
822 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000823 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000824 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000825 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000826 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000827 return false;
828 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000829 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +0000830}
831
832/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
833/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000834bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000835 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000836 if (!isUnary)
837 return isVMerge(N, UnitSize, 8, 24);
838 return isVMerge(N, UnitSize, 8, 8);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000839}
840
841/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
842/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000843bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000844 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000845 if (!isUnary)
846 return isVMerge(N, UnitSize, 0, 16);
847 return isVMerge(N, UnitSize, 0, 0);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000848}
849
850
Chris Lattner1d338192006-04-06 18:26:28 +0000851/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
852/// amount, otherwise return -1.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000853int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Owen Anderson9f944592009-08-11 20:47:22 +0000854 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000855 "PPC only supports shuffles by bytes!");
856
857 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +0000858
Chris Lattner1d338192006-04-06 18:26:28 +0000859 // Find the first non-undef value in the shuffle mask.
860 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000861 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +0000862 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000863
Chris Lattner1d338192006-04-06 18:26:28 +0000864 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000865
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000866 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +0000867 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000868 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +0000869 if (ShiftAmt < i) return -1;
870 ShiftAmt -= i;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000871
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000872 if (!isUnary) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000873 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000874 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000875 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000876 return -1;
877 } else {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000878 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000879 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000880 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000881 return -1;
882 }
Chris Lattner1d338192006-04-06 18:26:28 +0000883 return ShiftAmt;
884}
Chris Lattnerffc47562006-03-20 06:33:01 +0000885
886/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
887/// specifies a splat of a single element that is suitable for input to
888/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000889bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +0000890 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +0000891 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000892
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000893 // This is a splat operation if each element of the permute is the same, and
894 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000895 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +0000896
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000897 // FIXME: Handle UNDEF elements too!
898 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +0000899 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000900
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000901 // Check that the indices are consecutive, in the case of a multi-byte element
902 // splatted with a v16i8 mask.
903 for (unsigned i = 1; i != EltSize; ++i)
904 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000905 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000906
Chris Lattner95c7adc2006-04-04 17:25:31 +0000907 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000908 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +0000909 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000910 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000911 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000912 }
Chris Lattner95c7adc2006-04-04 17:25:31 +0000913 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +0000914}
915
Evan Cheng581d2792007-07-30 07:51:22 +0000916/// isAllNegativeZeroVector - Returns true if all elements of build_vector
917/// are -0.0.
918bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000919 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
920
921 APInt APVal, APUndef;
922 unsigned BitSize;
923 bool HasAnyUndefs;
Wesley Peck527da1b2010-11-23 03:31:01 +0000924
Dale Johannesen5f4eecf2009-11-13 01:45:18 +0000925 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000926 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000927 return CFP->getValueAPF().isNegZero();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000928
Evan Cheng581d2792007-07-30 07:51:22 +0000929 return false;
930}
931
Chris Lattnerffc47562006-03-20 06:33:01 +0000932/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
933/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner95c7adc2006-04-04 17:25:31 +0000934unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000935 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
936 assert(isSplatShuffleMask(SVOp, EltSize));
937 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +0000938}
939
Chris Lattner74cf9ff2006-04-12 17:37:20 +0000940/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +0000941/// by using a vspltis[bhw] instruction of the specified element size, return
942/// the constant being splatted. The ByteSize field indicates the number of
943/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000944SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
945 SDValue OpVal(0, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000946
947 // If ByteSize of the splat is bigger than the element size of the
948 // build_vector, then we have a case where we are checking for a splat where
949 // multiple elements of the buildvector are folded together into a single
950 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
951 unsigned EltSize = 16/N->getNumOperands();
952 if (EltSize < ByteSize) {
953 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000954 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000955 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000956
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000957 // See if all of the elements in the buildvector agree across.
958 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
959 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
960 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000961 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000962
Scott Michelcf0da6c2009-02-17 22:15:04 +0000963
Gabor Greiff304a7a2008-08-28 21:40:38 +0000964 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000965 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
966 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000967 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000968 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000969
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000970 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
971 // either constant or undef values that are identical for each chunk. See
972 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000973
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000974 // Check to see if all of the leading entries are either 0 or -1. If
975 // neither, then this won't fit into the immediate field.
976 bool LeadingZero = true;
977 bool LeadingOnes = true;
978 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000979 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000980
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000981 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
982 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
983 }
984 // Finally, check the least significant entry.
985 if (LeadingZero) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000986 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +0000987 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +0000988 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000989 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +0000990 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000991 }
992 if (LeadingOnes) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000993 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +0000994 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +0000995 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000996 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +0000997 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000998 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000999
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001000 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001001 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001002
Chris Lattner2771e2c2006-03-25 06:12:06 +00001003 // Check to see if this buildvec has a single non-undef value in its elements.
1004 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1005 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001006 if (OpVal.getNode() == 0)
Chris Lattner2771e2c2006-03-25 06:12:06 +00001007 OpVal = N->getOperand(i);
1008 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001009 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001010 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001011
Gabor Greiff304a7a2008-08-28 21:40:38 +00001012 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001013
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001014 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001015 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001016 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001017 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001018 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001019 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001020 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001021 }
1022
1023 // If the splat value is larger than the element value, then we can never do
1024 // this splat. The only case that we could fit the replicated bits into our
1025 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001026 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001027
Chris Lattner2771e2c2006-03-25 06:12:06 +00001028 // If the element value is larger than the splat value, cut it in half and
1029 // check to see if the two halves are equal. Continue doing this until we
1030 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
1031 while (ValSizeInBytes > ByteSize) {
1032 ValSizeInBytes >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001033
Chris Lattner2771e2c2006-03-25 06:12:06 +00001034 // If the top half equals the bottom half, we're still ok.
Chris Lattner39cc7172006-04-05 17:39:25 +00001035 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
1036 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001037 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001038 }
1039
1040 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001041 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001042
Evan Chengb1ddc982006-03-26 09:52:32 +00001043 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001044 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001045
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001046 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001047 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +00001048 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001049 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001050}
1051
Chris Lattner4211ca92006-04-14 06:01:58 +00001052//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001053// Addressing Mode Selection
1054//===----------------------------------------------------------------------===//
1055
1056/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1057/// or 64-bit immediate, and if the value can be accurately represented as a
1058/// sign extension from a 16-bit value. If so, this returns true and the
1059/// immediate.
1060static bool isIntS16Immediate(SDNode *N, short &Imm) {
1061 if (N->getOpcode() != ISD::Constant)
1062 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001063
Dan Gohmaneffb8942008-09-12 16:56:44 +00001064 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001065 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001066 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001067 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001068 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001069}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001070static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001071 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001072}
1073
1074
1075/// SelectAddressRegReg - Given the specified addressed, check to see if it
1076/// can be represented as an indexed [r+r] operation. Returns false if it
1077/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001078bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1079 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001080 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001081 short imm = 0;
1082 if (N.getOpcode() == ISD::ADD) {
1083 if (isIntS16Immediate(N.getOperand(1), imm))
1084 return false; // r+i
1085 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1086 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001087
Chris Lattnera801fced2006-11-08 02:15:41 +00001088 Base = N.getOperand(0);
1089 Index = N.getOperand(1);
1090 return true;
1091 } else if (N.getOpcode() == ISD::OR) {
1092 if (isIntS16Immediate(N.getOperand(1), imm))
1093 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001094
Chris Lattnera801fced2006-11-08 02:15:41 +00001095 // If this is an or of disjoint bitfields, we can codegen this as an add
1096 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1097 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001098 APInt LHSKnownZero, LHSKnownOne;
1099 APInt RHSKnownZero, RHSKnownOne;
1100 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001101 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001102
Dan Gohmanf19609a2008-02-27 01:23:58 +00001103 if (LHSKnownZero.getBoolValue()) {
1104 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001105 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001106 // If all of the bits are known zero on the LHS or RHS, the add won't
1107 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001108 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001109 Base = N.getOperand(0);
1110 Index = N.getOperand(1);
1111 return true;
1112 }
1113 }
1114 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001115
Chris Lattnera801fced2006-11-08 02:15:41 +00001116 return false;
1117}
1118
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001119// If we happen to be doing an i64 load or store into a stack slot that has
1120// less than a 4-byte alignment, then the frame-index elimination may need to
1121// use an indexed load or store instruction (because the offset may not be a
1122// multiple of 4). The extra register needed to hold the offset comes from the
1123// register scavenger, and it is possible that the scavenger will need to use
1124// an emergency spill slot. As a result, we need to make sure that a spill slot
1125// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1126// stack slot.
1127static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1128 // FIXME: This does not handle the LWA case.
1129 if (VT != MVT::i64)
1130 return;
1131
Hal Finkel7ab3db52013-07-10 15:29:01 +00001132 // NOTE: We'll exclude negative FIs here, which come from argument
1133 // lowering, because there are no known test cases triggering this problem
1134 // using packed structures (or similar). We can remove this exclusion if
1135 // we find such a test case. The reason why this is so test-case driven is
1136 // because this entire 'fixup' is only to prevent crashes (from the
1137 // register scavenger) on not-really-valid inputs. For example, if we have:
1138 // %a = alloca i1
1139 // %b = bitcast i1* %a to i64*
1140 // store i64* a, i64 b
1141 // then the store should really be marked as 'align 1', but is not. If it
1142 // were marked as 'align 1' then the indexed form would have been
1143 // instruction-selected initially, and the problem this 'fixup' is preventing
1144 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001145 if (FrameIdx < 0)
1146 return;
1147
1148 MachineFunction &MF = DAG.getMachineFunction();
1149 MachineFrameInfo *MFI = MF.getFrameInfo();
1150
1151 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1152 if (Align >= 4)
1153 return;
1154
1155 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1156 FuncInfo->setHasNonRISpills();
1157}
1158
Chris Lattnera801fced2006-11-08 02:15:41 +00001159/// Returns true if the address N can be represented by a base register plus
1160/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001161/// represented as reg+reg. If Aligned is true, only accept displacements
1162/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001163bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001164 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001165 SelectionDAG &DAG,
1166 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001167 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001168 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001169 // If this can be more profitably realized as r+r, fail.
1170 if (SelectAddressRegReg(N, Disp, Base, DAG))
1171 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001172
Chris Lattnera801fced2006-11-08 02:15:41 +00001173 if (N.getOpcode() == ISD::ADD) {
1174 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001175 if (isIntS16Immediate(N.getOperand(1), imm) &&
1176 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001177 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001178 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1179 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001180 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001181 } else {
1182 Base = N.getOperand(0);
1183 }
1184 return true; // [r+i]
1185 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1186 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001187 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001188 && "Cannot handle constant offsets yet!");
1189 Disp = N.getOperand(1).getOperand(0); // The global address.
1190 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001191 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001192 Disp.getOpcode() == ISD::TargetConstantPool ||
1193 Disp.getOpcode() == ISD::TargetJumpTable);
1194 Base = N.getOperand(0);
1195 return true; // [&g+r]
1196 }
1197 } else if (N.getOpcode() == ISD::OR) {
1198 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001199 if (isIntS16Immediate(N.getOperand(1), imm) &&
1200 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001201 // If this is an or of disjoint bitfields, we can codegen this as an add
1202 // (for better address arithmetic) if the LHS and RHS of the OR are
1203 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001204 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001205 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001206
Dan Gohmanf19609a2008-02-27 01:23:58 +00001207 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001208 // If all of the bits are known zero on the LHS or RHS, the add won't
1209 // carry.
1210 Base = N.getOperand(0);
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001211 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001212 return true;
1213 }
1214 }
1215 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1216 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001217
Chris Lattnera801fced2006-11-08 02:15:41 +00001218 // If this address fits entirely in a 16-bit sext immediate field, codegen
1219 // this as "d, 0"
1220 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001221 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001222 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkelf70c41e2013-03-21 23:45:03 +00001223 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1224 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001225 return true;
1226 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001227
1228 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001229 if ((CN->getValueType(0) == MVT::i32 ||
1230 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1231 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001232 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001233
Chris Lattnera801fced2006-11-08 02:15:41 +00001234 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001235 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001236
Owen Anderson9f944592009-08-11 20:47:22 +00001237 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1238 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001239 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001240 return true;
1241 }
1242 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001243
Chris Lattnera801fced2006-11-08 02:15:41 +00001244 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001245 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001246 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001247 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1248 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001249 Base = N;
1250 return true; // [r+0]
1251}
1252
1253/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1254/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001255bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1256 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001257 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001258 // Check to see if we can easily represent this as an [r+r] address. This
1259 // will fail if it thinks that the address is more profitably represented as
1260 // reg+imm, e.g. where imm = 0.
1261 if (SelectAddressRegReg(N, Base, Index, DAG))
1262 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001263
Chris Lattnera801fced2006-11-08 02:15:41 +00001264 // If the operand is an addition, always emit this as [r+r], since this is
1265 // better (for code size, and execution, as the memop does the add for free)
1266 // than emitting an explicit add.
1267 if (N.getOpcode() == ISD::ADD) {
1268 Base = N.getOperand(0);
1269 Index = N.getOperand(1);
1270 return true;
1271 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001272
Chris Lattnera801fced2006-11-08 02:15:41 +00001273 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkelf70c41e2013-03-21 23:45:03 +00001274 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1275 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001276 Index = N;
1277 return true;
1278}
1279
Chris Lattnera801fced2006-11-08 02:15:41 +00001280/// getPreIndexedAddressParts - returns true by value, base pointer and
1281/// offset pointer and addressing mode by reference if the node's address
1282/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001283bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1284 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001285 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001286 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001287 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001288
Ulrich Weigande90b0222013-03-22 14:58:48 +00001289 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001290 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001291 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001292 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001293 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1294 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001295 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001296 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001297 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001298 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001299 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001300 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001301 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001302 } else
1303 return false;
1304
Chris Lattner68371252006-11-14 01:38:31 +00001305 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands13237ac2008-06-06 12:08:01 +00001306 if (VT.isVector())
Chris Lattner68371252006-11-14 01:38:31 +00001307 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001308
Ulrich Weigande90b0222013-03-22 14:58:48 +00001309 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1310
1311 // Common code will reject creating a pre-inc form if the base pointer
1312 // is a frame index, or if N is a store and the base pointer is either
1313 // the same as or a predecessor of the value being stored. Check for
1314 // those situations here, and try with swapped Base/Offset instead.
1315 bool Swap = false;
1316
1317 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1318 Swap = true;
1319 else if (!isLoad) {
1320 SDValue Val = cast<StoreSDNode>(N)->getValue();
1321 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1322 Swap = true;
1323 }
1324
1325 if (Swap)
1326 std::swap(Base, Offset);
1327
Hal Finkelca542be2012-06-20 15:43:03 +00001328 AM = ISD::PRE_INC;
1329 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001330 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001331
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001332 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001333 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001334 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001335 return false;
1336 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001337 // LDU/STU need an address with at least 4-byte alignment.
1338 if (Alignment < 4)
1339 return false;
1340
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001341 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001342 return false;
1343 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001344
Chris Lattnerb314b152006-11-11 00:08:42 +00001345 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001346 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1347 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001348 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001349 LD->getExtensionType() == ISD::SEXTLOAD &&
1350 isa<ConstantSDNode>(Offset))
1351 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001352 }
1353
Chris Lattnerce645542006-11-10 02:08:47 +00001354 AM = ISD::PRE_INC;
1355 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001356}
1357
1358//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001359// LowerOperation implementation
1360//===----------------------------------------------------------------------===//
1361
Chris Lattneredb9d842010-11-15 02:46:57 +00001362/// GetLabelAccessInfo - Return true if we should reference labels using a
1363/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1364static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Chris Lattnerdd6df842010-11-15 03:13:19 +00001365 unsigned &LoOpFlags, const GlobalValue *GV = 0) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001366 HiOpFlags = PPCII::MO_HA;
1367 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001368
Chris Lattneredb9d842010-11-15 02:46:57 +00001369 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1370 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peck527da1b2010-11-23 03:31:01 +00001371 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattneredb9d842010-11-15 02:46:57 +00001372 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattnerdd6df842010-11-15 03:13:19 +00001373 if (isPIC) {
1374 HiOpFlags |= PPCII::MO_PIC_FLAG;
1375 LoOpFlags |= PPCII::MO_PIC_FLAG;
1376 }
1377
1378 // If this is a reference to a global value that requires a non-lazy-ptr, make
1379 // sure that instruction lowering adds it.
1380 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1381 HiOpFlags |= PPCII::MO_NLP_FLAG;
1382 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001383
Chris Lattnerdd6df842010-11-15 03:13:19 +00001384 if (GV->hasHiddenVisibility()) {
1385 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1386 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1387 }
1388 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001389
Chris Lattneredb9d842010-11-15 02:46:57 +00001390 return isPIC;
1391}
1392
1393static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1394 SelectionDAG &DAG) {
1395 EVT PtrVT = HiPart.getValueType();
1396 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001397 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001398
1399 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1400 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001401
Chris Lattneredb9d842010-11-15 02:46:57 +00001402 // With PIC, the first instruction is actually "GR+hi(&G)".
1403 if (isPIC)
1404 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1405 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001406
Chris Lattneredb9d842010-11-15 02:46:57 +00001407 // Generate non-pic code that has direct accesses to the constant pool.
1408 // The address of the global is just (hi(&g)+lo(&g)).
1409 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1410}
1411
Scott Michelcf0da6c2009-02-17 22:15:04 +00001412SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001413 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001414 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001415 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001416 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001417
Roman Divackyace47072012-08-24 16:26:02 +00001418 // 64-bit SVR4 ABI code is always position-independent.
1419 // The actual address of the GlobalValue is stored in the TOC.
1420 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1421 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001422 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001423 DAG.getRegister(PPC::X2, MVT::i64));
1424 }
1425
Chris Lattneredb9d842010-11-15 02:46:57 +00001426 unsigned MOHiFlag, MOLoFlag;
1427 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1428 SDValue CPIHi =
1429 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1430 SDValue CPILo =
1431 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1432 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001433}
1434
Dan Gohman21cea8a2010-04-17 15:26:15 +00001435SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001436 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001437 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001438
Roman Divackyace47072012-08-24 16:26:02 +00001439 // 64-bit SVR4 ABI code is always position-independent.
1440 // The actual address of the GlobalValue is stored in the TOC.
1441 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1442 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001443 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001444 DAG.getRegister(PPC::X2, MVT::i64));
1445 }
1446
Chris Lattneredb9d842010-11-15 02:46:57 +00001447 unsigned MOHiFlag, MOLoFlag;
1448 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1449 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1450 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1451 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001452}
1453
Dan Gohman21cea8a2010-04-17 15:26:15 +00001454SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1455 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001456 EVT PtrVT = Op.getValueType();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001457
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001458 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peck527da1b2010-11-23 03:31:01 +00001459
Chris Lattneredb9d842010-11-15 02:46:57 +00001460 unsigned MOHiFlag, MOLoFlag;
1461 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001462 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1463 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001464 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1465}
1466
Roman Divackye3f15c982012-06-04 17:36:38 +00001467SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1468 SelectionDAG &DAG) const {
1469
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001470 // FIXME: TLS addresses currently use medium model code sequences,
1471 // which is the most useful form. Eventually support for small and
1472 // large models could be added if users need it, at the cost of
1473 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001474 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001475 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001476 const GlobalValue *GV = GA->getGlobal();
1477 EVT PtrVT = getPointerTy();
1478 bool is64bit = PPCSubTarget.isPPC64();
1479
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001480 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001481
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001482 if (Model == TLSModel::LocalExec) {
1483 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001484 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001485 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001486 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001487 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1488 is64bit ? MVT::i64 : MVT::i32);
1489 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1490 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1491 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001492
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001493 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001494 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001495 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1496 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00001497 SDValue GOTPtr;
1498 if (is64bit) {
1499 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1500 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1501 PtrVT, GOTReg, TGA);
1502 } else
1503 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001504 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00001505 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001506 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001507 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001508
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001509 if (Model == TLSModel::GeneralDynamic) {
1510 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1511 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1512 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1513 GOTReg, TGA);
1514 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1515 GOTEntryHi, TGA);
1516
1517 // We need a chain node, and don't have one handy. The underlying
1518 // call has no side effects, so using the function entry node
1519 // suffices.
1520 SDValue Chain = DAG.getEntryNode();
1521 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1522 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1523 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1524 PtrVT, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001525 // The return value from GET_TLS_ADDR really is in X3 already, but
1526 // some hacks are needed here to tie everything together. The extra
1527 // copies dissolve during subsequent transforms.
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001528 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1529 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1530 }
1531
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001532 if (Model == TLSModel::LocalDynamic) {
1533 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1534 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1535 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1536 GOTReg, TGA);
1537 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1538 GOTEntryHi, TGA);
1539
1540 // We need a chain node, and don't have one handy. The underlying
1541 // call has no side effects, so using the function entry node
1542 // suffices.
1543 SDValue Chain = DAG.getEntryNode();
1544 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1545 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1546 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1547 PtrVT, ParmReg, TGA);
1548 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1549 // some hacks are needed here to tie everything together. The extra
1550 // copies dissolve during subsequent transforms.
1551 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1552 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt9ed4dbc2012-12-13 20:57:10 +00001553 Chain, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001554 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1555 }
1556
1557 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00001558}
1559
Chris Lattneredb9d842010-11-15 02:46:57 +00001560SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1561 SelectionDAG &DAG) const {
1562 EVT PtrVT = Op.getValueType();
1563 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001564 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00001565 const GlobalValue *GV = GSDN->getGlobal();
1566
Chris Lattneredb9d842010-11-15 02:46:57 +00001567 // 64-bit SVR4 ABI code is always position-independent.
1568 // The actual address of the GlobalValue is stored in the TOC.
1569 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1570 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1571 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1572 DAG.getRegister(PPC::X2, MVT::i64));
1573 }
1574
Chris Lattnerdd6df842010-11-15 03:13:19 +00001575 unsigned MOHiFlag, MOLoFlag;
1576 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00001577
Chris Lattnerdd6df842010-11-15 03:13:19 +00001578 SDValue GAHi =
1579 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1580 SDValue GALo =
1581 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00001582
Chris Lattnerdd6df842010-11-15 03:13:19 +00001583 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00001584
Chris Lattnerdd6df842010-11-15 03:13:19 +00001585 // If the global reference is actually to a non-lazy-pointer, we have to do an
1586 // extra load to get the address of the global.
1587 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1588 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001589 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00001590 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00001591}
1592
Dan Gohman21cea8a2010-04-17 15:26:15 +00001593SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00001594 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001595 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001596
Chris Lattner4211ca92006-04-14 06:01:58 +00001597 // If we're comparing for equality to zero, expose the fact that this is
1598 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1599 // fold the new nodes.
1600 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1601 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001602 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001603 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001604 if (VT.bitsLT(MVT::i32)) {
1605 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001606 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001607 }
Duncan Sands13237ac2008-06-06 12:08:01 +00001608 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001609 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1610 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00001611 DAG.getConstant(Log2b, MVT::i32));
1612 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00001613 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001614 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00001615 // optimized. FIXME: revisit this when we can custom lower all setcc
1616 // optimizations.
1617 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001618 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001619 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001620
Chris Lattner4211ca92006-04-14 06:01:58 +00001621 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00001622 // by xor'ing the rhs with the lhs, which is faster than setting a
1623 // condition register, reading it back out, and masking the correct bit. The
1624 // normal approach here uses sub to do this instead of xor. Using xor exposes
1625 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001626 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00001627 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001628 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001629 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00001630 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001631 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00001632 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001633 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001634}
1635
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001636SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001637 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00001638 SDNode *Node = Op.getNode();
1639 EVT VT = Node->getValueType(0);
1640 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1641 SDValue InChain = Node->getOperand(0);
1642 SDValue VAListPtr = Node->getOperand(1);
1643 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001644 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001645
Roman Divacky4394e682011-06-28 15:30:42 +00001646 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1647
1648 // gpr_index
1649 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1650 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1651 false, false, 0);
1652 InChain = GprIndex.getValue(1);
1653
1654 if (VT == MVT::i64) {
1655 // Check if GprIndex is even
1656 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1657 DAG.getConstant(1, MVT::i32));
1658 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1659 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1660 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1661 DAG.getConstant(1, MVT::i32));
1662 // Align GprIndex to be even if it isn't
1663 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1664 GprIndex);
1665 }
1666
1667 // fpr index is 1 byte after gpr
1668 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1669 DAG.getConstant(1, MVT::i32));
1670
1671 // fpr
1672 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1673 FprPtr, MachinePointerInfo(SV), MVT::i8,
1674 false, false, 0);
1675 InChain = FprIndex.getValue(1);
1676
1677 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1678 DAG.getConstant(8, MVT::i32));
1679
1680 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1681 DAG.getConstant(4, MVT::i32));
1682
1683 // areas
1684 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001685 MachinePointerInfo(), false, false,
1686 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001687 InChain = OverflowArea.getValue(1);
1688
1689 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001690 MachinePointerInfo(), false, false,
1691 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001692 InChain = RegSaveArea.getValue(1);
1693
1694 // select overflow_area if index > 8
1695 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1696 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1697
Roman Divacky4394e682011-06-28 15:30:42 +00001698 // adjustment constant gpr_index * 4/8
1699 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1700 VT.isInteger() ? GprIndex : FprIndex,
1701 DAG.getConstant(VT.isInteger() ? 4 : 8,
1702 MVT::i32));
1703
1704 // OurReg = RegSaveArea + RegConstant
1705 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1706 RegConstant);
1707
1708 // Floating types are 32 bytes into RegSaveArea
1709 if (VT.isFloatingPoint())
1710 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1711 DAG.getConstant(32, MVT::i32));
1712
1713 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1714 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1715 VT.isInteger() ? GprIndex : FprIndex,
1716 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1717 MVT::i32));
1718
1719 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1720 VT.isInteger() ? VAListPtr : FprPtr,
1721 MachinePointerInfo(SV),
1722 MVT::i8, false, false, 0);
1723
1724 // determine if we should load from reg_save_area or overflow_area
1725 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1726
1727 // increase overflow_area by 4/8 if gpr/fpr > 8
1728 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1729 DAG.getConstant(VT.isInteger() ? 4 : 8,
1730 MVT::i32));
1731
1732 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1733 OverflowAreaPlusN);
1734
1735 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1736 OverflowAreaPtr,
1737 MachinePointerInfo(),
1738 MVT::i32, false, false, 0);
1739
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00001740 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001741 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001742}
1743
Roman Divackyc3825df2013-07-25 21:36:47 +00001744SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
1745 const PPCSubtarget &Subtarget) const {
1746 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
1747
1748 // We have to copy the entire va_list struct:
1749 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
1750 return DAG.getMemcpy(Op.getOperand(0), Op,
1751 Op.getOperand(1), Op.getOperand(2),
1752 DAG.getConstant(12, MVT::i32), 8, false, true,
1753 MachinePointerInfo(), MachinePointerInfo());
1754}
1755
Duncan Sandsa0984362011-09-06 13:37:06 +00001756SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1757 SelectionDAG &DAG) const {
1758 return Op.getOperand(0);
1759}
1760
1761SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1762 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00001763 SDValue Chain = Op.getOperand(0);
1764 SDValue Trmp = Op.getOperand(1); // trampoline
1765 SDValue FPtr = Op.getOperand(2); // nested function
1766 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00001767 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00001768
Owen Anderson53aa7a92009-08-10 22:56:29 +00001769 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00001770 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00001771 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001772 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00001773 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00001774
Scott Michelcf0da6c2009-02-17 22:15:04 +00001775 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00001776 TargetLowering::ArgListEntry Entry;
1777
1778 Entry.Ty = IntPtrTy;
1779 Entry.Node = Trmp; Args.push_back(Entry);
1780
1781 // TrampSize == (isPPC64 ? 48 : 40);
1782 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00001783 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00001784 Args.push_back(Entry);
1785
1786 Entry.Node = FPtr; Args.push_back(Entry);
1787 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001788
Bill Wendling95e1af22008-09-17 00:30:57 +00001789 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Justin Holewinskiaa583972012-05-25 16:35:28 +00001790 TargetLowering::CallLoweringInfo CLI(Chain,
1791 Type::getVoidTy(*DAG.getContext()),
1792 false, false, false, false, 0,
1793 CallingConv::C,
Evan Cheng65f9d192012-02-28 18:51:51 +00001794 /*isTailCall=*/false,
Justin Holewinskiaa583972012-05-25 16:35:28 +00001795 /*doesNotRet=*/false,
1796 /*isReturnValueUsed=*/true,
Bill Wendling95e1af22008-09-17 00:30:57 +00001797 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00001798 Args, DAG, dl);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001799 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bill Wendling95e1af22008-09-17 00:30:57 +00001800
Duncan Sandsa0984362011-09-06 13:37:06 +00001801 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00001802}
1803
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001804SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001805 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001806 MachineFunction &MF = DAG.getMachineFunction();
1807 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1808
Andrew Trickef9de2a2013-05-25 02:42:55 +00001809 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001810
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001811 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001812 // vastart just stores the address of the VarArgsFrameIndex slot into the
1813 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001814 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00001815 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001816 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00001817 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1818 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00001819 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001820 }
1821
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001822 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001823 // We suppose the given va_list is already allocated.
1824 //
1825 // typedef struct {
1826 // char gpr; /* index into the array of 8 GPRs
1827 // * stored in the register save area
1828 // * gpr=0 corresponds to r3,
1829 // * gpr=1 to r4, etc.
1830 // */
1831 // char fpr; /* index into the array of 8 FPRs
1832 // * stored in the register save area
1833 // * fpr=0 corresponds to f1,
1834 // * fpr=1 to f2, etc.
1835 // */
1836 // char *overflow_arg_area;
1837 // /* location on stack that holds
1838 // * the next overflow argument
1839 // */
1840 // char *reg_save_area;
1841 // /* where r3:r10 and f1:f8 (if saved)
1842 // * are stored
1843 // */
1844 // } va_list[1];
1845
1846
Dan Gohman31ae5862010-04-17 14:41:14 +00001847 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1848 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001849
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001850
Owen Anderson53aa7a92009-08-10 22:56:29 +00001851 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001852
Dan Gohman31ae5862010-04-17 14:41:14 +00001853 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1854 PtrVT);
1855 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1856 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001857
Duncan Sands13237ac2008-06-06 12:08:01 +00001858 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001859 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001860
Duncan Sands13237ac2008-06-06 12:08:01 +00001861 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001862 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001863
1864 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001865 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001866
Dan Gohman2d489b52008-02-06 22:27:42 +00001867 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001868
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001869 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001870 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00001871 Op.getOperand(1),
1872 MachinePointerInfo(SV),
1873 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001874 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001875 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001876 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001877
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001878 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001879 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00001880 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1881 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00001882 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001883 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001884 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001885
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001886 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001887 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00001888 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1889 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001890 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001891 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001892 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001893
1894 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00001895 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1896 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001897 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001898
Chris Lattner4211ca92006-04-14 06:01:58 +00001899}
1900
Chris Lattner4f2e4e02007-03-06 00:59:59 +00001901#include "PPCGenCallingConv.inc"
1902
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001903// Function whose sole purpose is to kill compiler warnings
1904// stemming from unused functions included from PPCGenCallingConv.inc.
1905CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001906 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001907}
1908
Bill Schmidt230b4512013-06-12 16:39:22 +00001909bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1910 CCValAssign::LocInfo &LocInfo,
1911 ISD::ArgFlagsTy &ArgFlags,
1912 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001913 return true;
1914}
1915
Bill Schmidt230b4512013-06-12 16:39:22 +00001916bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
1917 MVT &LocVT,
1918 CCValAssign::LocInfo &LocInfo,
1919 ISD::ArgFlagsTy &ArgFlags,
1920 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00001921 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001922 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1923 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1924 };
1925 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00001926
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001927 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1928
1929 // Skip one register if the first unallocated register has an even register
1930 // number and there are still argument registers available which have not been
1931 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1932 // need to skip a register if RegNum is odd.
1933 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1934 State.AllocateReg(ArgRegs[RegNum]);
1935 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001936
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001937 // Always return false here, as this function only makes sure that the first
1938 // unallocated register has an odd register number and does not actually
1939 // allocate a register for the current argument.
1940 return false;
1941}
1942
Bill Schmidt230b4512013-06-12 16:39:22 +00001943bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
1944 MVT &LocVT,
1945 CCValAssign::LocInfo &LocInfo,
1946 ISD::ArgFlagsTy &ArgFlags,
1947 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00001948 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001949 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1950 PPC::F8
1951 };
1952
1953 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00001954
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001955 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1956
1957 // If there is only one Floating-point register left we need to put both f64
1958 // values of a split ppc_fp128 value on the stack.
1959 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1960 State.AllocateReg(ArgRegs[RegNum]);
1961 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001962
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001963 // Always return false here, as this function only makes sure that the two f64
1964 // values a ppc_fp128 value is split into are both passed in registers or both
1965 // passed on the stack and does not actually allocate a register for the
1966 // current argument.
1967 return false;
1968}
1969
Chris Lattner43df5b32007-02-25 05:34:32 +00001970/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001971/// on Darwin.
Craig Topperca658c22012-03-11 07:16:55 +00001972static const uint16_t *GetFPR() {
1973 static const uint16_t FPR[] = {
Chris Lattner43df5b32007-02-25 05:34:32 +00001974 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001975 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner43df5b32007-02-25 05:34:32 +00001976 };
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001977
Chris Lattner43df5b32007-02-25 05:34:32 +00001978 return FPR;
1979}
1980
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00001981/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1982/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001983static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00001984 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00001985 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00001986 if (Flags.isByVal())
1987 ArgSize = Flags.getByValSize();
1988 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1989
1990 return ArgSize;
1991}
1992
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001993SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001994PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001995 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001996 const SmallVectorImpl<ISD::InputArg>
1997 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001998 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001999 SmallVectorImpl<SDValue> &InVals)
2000 const {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002001 if (PPCSubTarget.isSVR4ABI()) {
2002 if (PPCSubTarget.isPPC64())
2003 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
2004 dl, DAG, InVals);
2005 else
2006 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2007 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002008 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002009 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2010 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002011 }
2012}
2013
2014SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002015PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002016 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002017 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002018 const SmallVectorImpl<ISD::InputArg>
2019 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002020 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002021 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002022
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002023 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002024 // +-----------------------------------+
2025 // +--> | Back chain |
2026 // | +-----------------------------------+
2027 // | | Floating-point register save area |
2028 // | +-----------------------------------+
2029 // | | General register save area |
2030 // | +-----------------------------------+
2031 // | | CR save word |
2032 // | +-----------------------------------+
2033 // | | VRSAVE save word |
2034 // | +-----------------------------------+
2035 // | | Alignment padding |
2036 // | +-----------------------------------+
2037 // | | Vector register save area |
2038 // | +-----------------------------------+
2039 // | | Local variable space |
2040 // | +-----------------------------------+
2041 // | | Parameter list area |
2042 // | +-----------------------------------+
2043 // | | LR save word |
2044 // | +-----------------------------------+
2045 // SP--> +--- | Back chain |
2046 // +-----------------------------------+
2047 //
2048 // Specifications:
2049 // System V Application Binary Interface PowerPC Processor Supplement
2050 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002051
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002052 MachineFunction &MF = DAG.getMachineFunction();
2053 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002054 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002055
Owen Anderson53aa7a92009-08-10 22:56:29 +00002056 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002057 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002058 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2059 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002060 unsigned PtrByteSize = 4;
2061
2062 // Assign locations to all of the incoming arguments.
2063 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002064 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002065 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002066
2067 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002068 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002069
Bill Schmidtef17c142013-02-06 17:33:58 +00002070 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002071
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002072 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2073 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002074
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002075 // Arguments stored in registers.
2076 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002077 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002078 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002079
Owen Anderson9f944592009-08-11 20:47:22 +00002080 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002081 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002082 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002083 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002084 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002085 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002086 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002087 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00002088 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002089 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002090 case MVT::f64:
Craig Topperabadc662012-04-20 06:31:50 +00002091 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002092 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002093 case MVT::v16i8:
2094 case MVT::v8i16:
2095 case MVT::v4i32:
2096 case MVT::v4f32:
Craig Topperabadc662012-04-20 06:31:50 +00002097 RC = &PPC::VRRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002098 break;
2099 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002100
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002101 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002102 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002103 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2104 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2105
2106 if (ValVT == MVT::i1)
2107 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002108
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002109 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002110 } else {
2111 // Argument stored in memory.
2112 assert(VA.isMemLoc());
2113
Hal Finkel940ab932014-02-28 00:27:01 +00002114 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002115 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002116 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002117
2118 // Create load nodes to retrieve arguments from the stack.
2119 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002120 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2121 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002122 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002123 }
2124 }
2125
2126 // Assign locations to all of the incoming aggregate by value arguments.
2127 // Aggregates passed by value are stored in the local variable space of the
2128 // caller's stack frame, right above the parameter list area.
2129 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002130 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002131 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002132
2133 // Reserve stack space for the allocations in CCInfo.
2134 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2135
Bill Schmidtef17c142013-02-06 17:33:58 +00002136 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002137
2138 // Area that is at least reserved in the caller of this function.
2139 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00002140
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002141 // Set the size that is at least reserved in caller of this function. Tail
2142 // call optimized function's reserved stack space needs to be aligned so that
2143 // taking the difference between two stack areas will result in an aligned
2144 // stack.
2145 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2146
2147 MinReservedArea =
2148 std::max(MinReservedArea,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002149 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peck527da1b2010-11-23 03:31:01 +00002150
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002151 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002152 getStackAlignment();
2153 unsigned AlignMask = TargetAlign-1;
2154 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peck527da1b2010-11-23 03:31:01 +00002155
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002156 FI->setMinReservedArea(MinReservedArea);
2157
2158 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002159
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002160 // If the function takes variable number of arguments, make a frame index for
2161 // the start of the first vararg value... for expansion of llvm.va_start.
2162 if (isVarArg) {
Craig Topperbef78fc2012-03-11 07:57:25 +00002163 static const uint16_t GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002164 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2165 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2166 };
2167 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2168
Craig Topperbef78fc2012-03-11 07:57:25 +00002169 static const uint16_t FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002170 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2171 PPC::F8
2172 };
2173 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2174
Dan Gohman31ae5862010-04-17 14:41:14 +00002175 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2176 NumGPArgRegs));
2177 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2178 NumFPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002179
2180 // Make room for NumGPArgRegs and NumFPArgRegs.
2181 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson9f944592009-08-11 20:47:22 +00002182 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002183
Dan Gohman31ae5862010-04-17 14:41:14 +00002184 FuncInfo->setVarArgsStackOffset(
2185 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002186 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002187
Dan Gohman31ae5862010-04-17 14:41:14 +00002188 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2189 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002190
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002191 // The fixed integer arguments of a variadic function are stored to the
2192 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2193 // the result of va_next.
2194 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2195 // Get an existing live-in vreg, or add a new one.
2196 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2197 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002198 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002199
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002200 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002201 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2202 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002203 MemOps.push_back(Store);
2204 // Increment the address by four for the next argument to store
2205 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2206 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2207 }
2208
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002209 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2210 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002211 // The double arguments are stored to the VarArgsFrameIndex
2212 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002213 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2214 // Get an existing live-in vreg, or add a new one.
2215 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2216 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002217 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002218
Owen Anderson9f944592009-08-11 20:47:22 +00002219 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002220 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2221 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002222 MemOps.push_back(Store);
2223 // Increment the address by eight for the next argument to store
Owen Anderson9f944592009-08-11 20:47:22 +00002224 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002225 PtrVT);
2226 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2227 }
2228 }
2229
2230 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002231 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002232 MVT::Other, &MemOps[0], MemOps.size());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002233
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002234 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002235}
2236
Bill Schmidt57d6de52012-10-23 15:51:16 +00002237// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2238// value to MVT::i64 and then truncate to the correct register size.
2239SDValue
2240PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2241 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002242 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002243 if (Flags.isSExt())
2244 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2245 DAG.getValueType(ObjectVT));
2246 else if (Flags.isZExt())
2247 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2248 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002249
Hal Finkel940ab932014-02-28 00:27:01 +00002250 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002251}
2252
2253// Set the size that is at least reserved in caller of this function. Tail
2254// call optimized functions' reserved stack space needs to be aligned so that
2255// taking the difference between two stack areas will result in an aligned
2256// stack.
2257void
2258PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2259 unsigned nAltivecParamsAtEnd,
2260 unsigned MinReservedArea,
2261 bool isPPC64) const {
2262 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2263 // Add the Altivec parameters at the end, if needed.
2264 if (nAltivecParamsAtEnd) {
2265 MinReservedArea = ((MinReservedArea+15)/16)*16;
2266 MinReservedArea += 16*nAltivecParamsAtEnd;
2267 }
2268 MinReservedArea =
2269 std::max(MinReservedArea,
2270 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2271 unsigned TargetAlign
2272 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2273 getStackAlignment();
2274 unsigned AlignMask = TargetAlign-1;
2275 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2276 FI->setMinReservedArea(MinReservedArea);
2277}
2278
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002279SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002280PPCTargetLowering::LowerFormalArguments_64SVR4(
2281 SDValue Chain,
2282 CallingConv::ID CallConv, bool isVarArg,
2283 const SmallVectorImpl<ISD::InputArg>
2284 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002285 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002286 SmallVectorImpl<SDValue> &InVals) const {
2287 // TODO: add description of PPC stack frame format, or at least some docs.
2288 //
2289 MachineFunction &MF = DAG.getMachineFunction();
2290 MachineFrameInfo *MFI = MF.getFrameInfo();
2291 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2292
2293 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2294 // Potential tail calls could cause overwriting of argument stack slots.
2295 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2296 (CallConv == CallingConv::Fast));
2297 unsigned PtrByteSize = 8;
2298
2299 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2300 // Area that is at least reserved in caller of this function.
2301 unsigned MinReservedArea = ArgOffset;
2302
2303 static const uint16_t GPR[] = {
2304 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2305 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2306 };
2307
2308 static const uint16_t *FPR = GetFPR();
2309
2310 static const uint16_t VR[] = {
2311 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2312 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2313 };
2314
2315 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2316 const unsigned Num_FPR_Regs = 13;
2317 const unsigned Num_VR_Regs = array_lengthof(VR);
2318
2319 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2320
2321 // Add DAG nodes to load the arguments or copy them out of registers. On
2322 // entry to a function on PPC, the arguments start after the linkage area,
2323 // although the first ones are often in registers.
2324
2325 SmallVector<SDValue, 8> MemOps;
2326 unsigned nAltivecParamsAtEnd = 0;
2327 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002328 unsigned CurArgIdx = 0;
2329 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002330 SDValue ArgVal;
2331 bool needsLoad = false;
2332 EVT ObjectVT = Ins[ArgNo].VT;
Hal Finkel940ab932014-02-28 00:27:01 +00002333 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002334 unsigned ArgSize = ObjSize;
2335 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt6631e942013-02-20 17:31:41 +00002336 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2337 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002338
2339 unsigned CurArgOffset = ArgOffset;
2340
2341 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2342 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2343 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
2344 if (isVarArg) {
2345 MinReservedArea = ((MinReservedArea+15)/16)*16;
2346 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2347 Flags,
2348 PtrByteSize);
2349 } else
2350 nAltivecParamsAtEnd++;
2351 } else
2352 // Calculate min reserved area.
2353 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2354 Flags,
2355 PtrByteSize);
2356
2357 // FIXME the codegen can be much improved in some cases.
2358 // We do not have to keep everything in memory.
2359 if (Flags.isByVal()) {
2360 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2361 ObjSize = Flags.getByValSize();
2362 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002363 // Empty aggregate parameters do not take up registers. Examples:
2364 // struct { } a;
2365 // union { } b;
2366 // int c[0];
2367 // etc. However, we have to provide a place-holder in InVals, so
2368 // pretend we have an 8-byte item at the current address for that
2369 // purpose.
2370 if (!ObjSize) {
2371 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2372 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2373 InVals.push_back(FIN);
2374 continue;
2375 }
Hal Finkel262a2242013-09-12 23:20:06 +00002376
2377 unsigned BVAlign = Flags.getByValAlign();
2378 if (BVAlign > 8) {
2379 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
2380 CurArgOffset = ArgOffset;
2381 }
2382
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002383 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt48081ca2012-10-16 13:30:53 +00002384 if (ObjSize < PtrByteSize)
2385 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002386 // The value of the object is its address.
2387 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2388 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2389 InVals.push_back(FIN);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002390
2391 if (ObjSize < 8) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002392 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002393 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002394 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002395 SDValue Store;
2396
2397 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2398 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2399 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2400 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002401 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002402 ObjType, false, false, 0);
2403 } else {
2404 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2405 // store the whole register as-is to the parameter save area
2406 // slot. The address of the parameter was already calculated
2407 // above (InVals.push_back(FIN)) to be the right-justified
2408 // offset within the slot. For this store, we need a new
2409 // frame index that points at the beginning of the slot.
2410 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2411 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2412 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002413 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002414 false, false, 0);
2415 }
2416
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002417 MemOps.push_back(Store);
2418 ++GPR_idx;
2419 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002420 // Whether we copied from a register or not, advance the offset
2421 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002422 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002423 continue;
2424 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002425
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002426 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2427 // Store whatever pieces of the object are in registers
2428 // to memory. ArgOffset will be the address of the beginning
2429 // of the object.
2430 if (GPR_idx != Num_GPR_Regs) {
2431 unsigned VReg;
2432 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2433 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2434 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2435 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002436 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002437 MachinePointerInfo(FuncArg, j),
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002438 false, false, 0);
2439 MemOps.push_back(Store);
2440 ++GPR_idx;
2441 ArgOffset += PtrByteSize;
2442 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00002443 ArgOffset += ArgSize - j;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002444 break;
2445 }
2446 }
2447 continue;
2448 }
2449
2450 switch (ObjectVT.getSimpleVT().SimpleTy) {
2451 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00002452 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002453 case MVT::i32:
2454 case MVT::i64:
2455 if (GPR_idx != Num_GPR_Regs) {
2456 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2457 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2458
Hal Finkel940ab932014-02-28 00:27:01 +00002459 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002460 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2461 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002462 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002463
2464 ++GPR_idx;
2465 } else {
2466 needsLoad = true;
2467 ArgSize = PtrByteSize;
2468 }
2469 ArgOffset += 8;
2470 break;
2471
2472 case MVT::f32:
2473 case MVT::f64:
2474 // Every 8 bytes of argument space consumes one of the GPRs available for
2475 // argument passing.
2476 if (GPR_idx != Num_GPR_Regs) {
2477 ++GPR_idx;
2478 }
2479 if (FPR_idx != Num_FPR_Regs) {
2480 unsigned VReg;
2481
2482 if (ObjectVT == MVT::f32)
2483 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2484 else
2485 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
2486
2487 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2488 ++FPR_idx;
2489 } else {
2490 needsLoad = true;
Bill Schmidt22162472012-10-11 15:38:20 +00002491 ArgSize = PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002492 }
2493
2494 ArgOffset += 8;
2495 break;
2496 case MVT::v4f32:
2497 case MVT::v4i32:
2498 case MVT::v8i16:
2499 case MVT::v16i8:
2500 // Note that vector arguments in registers don't reserve stack space,
2501 // except in varargs functions.
2502 if (VR_idx != Num_VR_Regs) {
2503 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
2504 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2505 if (isVarArg) {
2506 while ((ArgOffset % 16) != 0) {
2507 ArgOffset += PtrByteSize;
2508 if (GPR_idx != Num_GPR_Regs)
2509 GPR_idx++;
2510 }
2511 ArgOffset += 16;
2512 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2513 }
2514 ++VR_idx;
2515 } else {
2516 // Vectors are aligned.
2517 ArgOffset = ((ArgOffset+15)/16)*16;
2518 CurArgOffset = ArgOffset;
2519 ArgOffset += 16;
2520 needsLoad = true;
2521 }
2522 break;
2523 }
2524
2525 // We need to load the argument to a virtual register if we determined
2526 // above that we ran out of physical registers of the appropriate type.
2527 if (needsLoad) {
2528 int FI = MFI->CreateFixedObject(ObjSize,
2529 CurArgOffset + (ArgSize - ObjSize),
2530 isImmutable);
2531 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2532 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2533 false, false, false, 0);
2534 }
2535
2536 InVals.push_back(ArgVal);
2537 }
2538
2539 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002540 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002541 // taking the difference between two stack areas will result in an aligned
2542 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002543 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002544
2545 // If the function takes variable number of arguments, make a frame index for
2546 // the start of the first vararg value... for expansion of llvm.va_start.
2547 if (isVarArg) {
2548 int Depth = ArgOffset;
2549
2550 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00002551 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002552 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2553
2554 // If this function is vararg, store any remaining integer argument regs
2555 // to their spots on the stack so that they may be loaded by deferencing the
2556 // result of va_next.
2557 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2558 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2559 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2560 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2561 MachinePointerInfo(), false, false, 0);
2562 MemOps.push_back(Store);
2563 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00002564 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002565 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2566 }
2567 }
2568
2569 if (!MemOps.empty())
2570 Chain = DAG.getNode(ISD::TokenFactor, dl,
2571 MVT::Other, &MemOps[0], MemOps.size());
2572
2573 return Chain;
2574}
2575
2576SDValue
2577PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002578 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002579 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002580 const SmallVectorImpl<ISD::InputArg>
2581 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002582 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002583 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002584 // TODO: add description of PPC stack frame format, or at least some docs.
2585 //
2586 MachineFunction &MF = DAG.getMachineFunction();
2587 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002588 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002589
Owen Anderson53aa7a92009-08-10 22:56:29 +00002590 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002591 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002592 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002593 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2594 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00002595 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey48850c12006-11-16 22:43:37 +00002596
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002597 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002598 // Area that is at least reserved in caller of this function.
2599 unsigned MinReservedArea = ArgOffset;
2600
Craig Topperca658c22012-03-11 07:16:55 +00002601 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002602 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2603 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2604 };
Craig Topperca658c22012-03-11 07:16:55 +00002605 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00002606 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2607 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2608 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00002609
Craig Topperca658c22012-03-11 07:16:55 +00002610 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002611
Craig Topperca658c22012-03-11 07:16:55 +00002612 static const uint16_t VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002613 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2614 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2615 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00002616
Owen Andersone2f23a32007-09-07 04:06:50 +00002617 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002618 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00002619 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00002620
2621 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002622
Craig Topperca658c22012-03-11 07:16:55 +00002623 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002624
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002625 // In 32-bit non-varargs functions, the stack space for vectors is after the
2626 // stack space for non-vectors. We do not use this space unless we have
2627 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00002628 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002629 // that out...for the pathological case, compute VecArgOffset as the
2630 // start of the vector parameter area. Computing VecArgOffset is the
2631 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002632 unsigned VecArgOffset = ArgOffset;
2633 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002634 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002635 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002636 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002637 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002638
Duncan Sandsd97eea32008-03-21 09:14:45 +00002639 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002640 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00002641 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002642 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002643 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2644 VecArgOffset += ArgSize;
2645 continue;
2646 }
2647
Owen Anderson9f944592009-08-11 20:47:22 +00002648 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002649 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002650 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002651 case MVT::i32:
2652 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002653 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002654 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002655 case MVT::i64: // PPC64
2656 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002657 // FIXME: We are guaranteed to be !isPPC64 at this point.
2658 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002659 VecArgOffset += 8;
2660 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002661 case MVT::v4f32:
2662 case MVT::v4i32:
2663 case MVT::v8i16:
2664 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002665 // Nothing to do, we're only looking at Nonvector args here.
2666 break;
2667 }
2668 }
2669 }
2670 // We've found where the vector parameter area in memory is. Skip the
2671 // first 12 parameters; these don't use that memory.
2672 VecArgOffset = ((VecArgOffset+15)/16)*16;
2673 VecArgOffset += 12*16;
2674
Chris Lattner4302e8f2006-05-16 18:18:50 +00002675 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00002676 // entry to a function on PPC, the arguments start after the linkage area,
2677 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00002678
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002679 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002680 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00002681 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002682 unsigned CurArgIdx = 0;
2683 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002684 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002685 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002686 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00002687 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00002688 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002689 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002690 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2691 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002692
Chris Lattner318f0d22006-05-16 18:51:52 +00002693 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002694
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002695 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002696 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2697 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002698 if (isVarArg || isPPC64) {
2699 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002700 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002701 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002702 PtrByteSize);
2703 } else nAltivecParamsAtEnd++;
2704 } else
2705 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002706 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002707 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002708 PtrByteSize);
2709
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002710 // FIXME the codegen can be much improved in some cases.
2711 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002712 if (Flags.isByVal()) {
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002713 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002714 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002715 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002716 // Objects of size 1 and 2 are right justified, everything else is
2717 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00002718 if (ObjSize==1 || ObjSize==2) {
2719 CurArgOffset = CurArgOffset + (4 - ObjSize);
2720 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002721 // The value of the object is its address.
Evan Cheng0664a672010-07-03 00:40:23 +00002722 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002723 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002724 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002725 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00002726 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002727 unsigned VReg;
2728 if (isPPC64)
2729 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2730 else
2731 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002732 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002733 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002734 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002735 MachinePointerInfo(FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002736 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00002737 MemOps.push_back(Store);
2738 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00002739 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002740
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002741 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00002742
Dale Johannesen21a8f142008-03-08 01:41:42 +00002743 continue;
2744 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002745 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2746 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002747 // to memory. ArgOffset will be the address of the beginning
2748 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002749 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002750 unsigned VReg;
2751 if (isPPC64)
2752 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2753 else
2754 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00002755 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002756 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002757 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002758 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002759 MachinePointerInfo(FuncArg, j),
David Greene87a5abe2010-02-15 16:56:53 +00002760 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002761 MemOps.push_back(Store);
2762 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002763 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002764 } else {
2765 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2766 break;
2767 }
2768 }
2769 continue;
2770 }
2771
Owen Anderson9f944592009-08-11 20:47:22 +00002772 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002773 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002774 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002775 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00002776 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00002777 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002778 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002779 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Bill Wendling968f32c2008-03-07 20:49:02 +00002780 ++GPR_idx;
2781 } else {
2782 needsLoad = true;
2783 ArgSize = PtrByteSize;
2784 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002785 // All int arguments reserve stack space in the Darwin ABI.
2786 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00002787 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002788 }
Bill Wendling968f32c2008-03-07 20:49:02 +00002789 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00002790 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00002791 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002792 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002793 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00002794
Hal Finkel940ab932014-02-28 00:27:01 +00002795 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00002796 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00002797 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002798 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00002799
Chris Lattnerec78cad2006-06-26 22:48:35 +00002800 ++GPR_idx;
2801 } else {
2802 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00002803 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002804 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002805 // All int arguments reserve stack space in the Darwin ABI.
2806 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002807 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002808
Owen Anderson9f944592009-08-11 20:47:22 +00002809 case MVT::f32:
2810 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00002811 // Every 4 bytes of argument space consumes one of the GPRs available for
2812 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002813 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002814 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00002815 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002816 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00002817 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002818 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002819 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002820
Owen Anderson9f944592009-08-11 20:47:22 +00002821 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00002822 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002823 else
Devang Patelf3292b22011-02-21 23:21:26 +00002824 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002825
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002826 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002827 ++FPR_idx;
2828 } else {
2829 needsLoad = true;
2830 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002831
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002832 // All FP arguments reserve stack space in the Darwin ABI.
2833 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002834 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002835 case MVT::v4f32:
2836 case MVT::v4i32:
2837 case MVT::v8i16:
2838 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00002839 // Note that vector arguments in registers don't reserve stack space,
2840 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002841 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002842 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002843 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00002844 if (isVarArg) {
2845 while ((ArgOffset % 16) != 0) {
2846 ArgOffset += PtrByteSize;
2847 if (GPR_idx != Num_GPR_Regs)
2848 GPR_idx++;
2849 }
2850 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002851 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00002852 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002853 ++VR_idx;
2854 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002855 if (!isVarArg && !isPPC64) {
2856 // Vectors go after all the nonvectors.
2857 CurArgOffset = VecArgOffset;
2858 VecArgOffset += 16;
2859 } else {
2860 // Vectors are aligned.
2861 ArgOffset = ((ArgOffset+15)/16)*16;
2862 CurArgOffset = ArgOffset;
2863 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00002864 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002865 needsLoad = true;
2866 }
2867 break;
2868 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002869
Chris Lattner4302e8f2006-05-16 18:18:50 +00002870 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002871 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002872 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002873 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002874 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00002875 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002876 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002877 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002878 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002879 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002880
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002881 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002882 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002883
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002884 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002885 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002886 // taking the difference between two stack areas will result in an aligned
2887 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002888 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002889
Chris Lattner4302e8f2006-05-16 18:18:50 +00002890 // If the function takes variable number of arguments, make a frame index for
2891 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002892 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002893 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002894
Dan Gohman31ae5862010-04-17 14:41:14 +00002895 FuncInfo->setVarArgsFrameIndex(
2896 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002897 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00002898 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002899
Chris Lattner4302e8f2006-05-16 18:18:50 +00002900 // If this function is vararg, store any remaining integer argument regs
2901 // to their spots on the stack so that they may be loaded by deferencing the
2902 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002903 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00002904 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00002905
Chris Lattner2cca3852006-11-18 01:57:19 +00002906 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00002907 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00002908 else
Devang Patelf3292b22011-02-21 23:21:26 +00002909 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00002910
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002911 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002912 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2913 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002914 MemOps.push_back(Store);
2915 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002916 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00002917 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002918 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002919 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002920
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002921 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002922 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002923 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002924
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002925 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002926}
2927
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002928/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
2929/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002930static unsigned
2931CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2932 bool isPPC64,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002933 bool isVarArg,
2934 unsigned CC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002935 const SmallVectorImpl<ISD::OutputArg>
2936 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002937 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002938 unsigned &nAltivecParamsAtEnd) {
2939 // Count how many bytes are to be pushed on the stack, including the linkage
2940 // area, and parameter passing area. We start with 24/48 bytes, which is
2941 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002942 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002943 unsigned NumOps = Outs.size();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002944 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2945
2946 // Add up all the space actually used.
2947 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2948 // they all go in registers, but we must reserve stack space for them for
2949 // possible use by the caller. In varargs or 64-bit calls, parameters are
2950 // assigned stack space in order, with padding so Altivec parameters are
2951 // 16-byte aligned.
2952 nAltivecParamsAtEnd = 0;
2953 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002954 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002955 EVT ArgVT = Outs[i].VT;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002956 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002957 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2958 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002959 if (!isVarArg && !isPPC64) {
2960 // Non-varargs Altivec parameters go after all the non-Altivec
2961 // parameters; handle those later so we know how much padding we need.
2962 nAltivecParamsAtEnd++;
2963 continue;
2964 }
2965 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2966 NumBytes = ((NumBytes+15)/16)*16;
2967 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002968 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002969 }
2970
2971 // Allow for Altivec parameters at the end, if needed.
2972 if (nAltivecParamsAtEnd) {
2973 NumBytes = ((NumBytes+15)/16)*16;
2974 NumBytes += 16*nAltivecParamsAtEnd;
2975 }
2976
2977 // The prolog code of the callee may store up to 8 GPR argument registers to
2978 // the stack, allowing va_start to index over them in memory if its varargs.
2979 // Because we cannot tell if this is needed on the caller side, we have to
2980 // conservatively assume that it is needed. As such, make sure we have at
2981 // least enough stack space for the caller to store the 8 GPRs.
2982 NumBytes = std::max(NumBytes,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002983 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002984
2985 // Tail call needs the stack to be aligned.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002986 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
2987 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
2988 getFrameLowering()->getStackAlignment();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002989 unsigned AlignMask = TargetAlign-1;
2990 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2991 }
2992
2993 return NumBytes;
2994}
2995
2996/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00002997/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00002998static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002999 unsigned ParamSize) {
3000
Dale Johannesen86dcae12009-11-24 01:09:07 +00003001 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003002
3003 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
3004 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3005 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3006 // Remember only if the new adjustement is bigger.
3007 if (SPDiff < FI->getTailCallSPDelta())
3008 FI->setTailCallSPDelta(SPDiff);
3009
3010 return SPDiff;
3011}
3012
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003013/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3014/// for tail call optimization. Targets which want to do tail call
3015/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003016bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003017PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003018 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003019 bool isVarArg,
3020 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003021 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003022 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00003023 return false;
3024
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003025 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003026 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003027 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003028
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003029 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00003030 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003031 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
3032 // Functions containing by val parameters are not supported.
3033 for (unsigned i = 0; i != Ins.size(); i++) {
3034 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3035 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003036 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003037
Alp Tokerf907b892013-12-05 05:44:44 +00003038 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003039 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
3040 return true;
3041
3042 // At the moment we can only do local tail calls (in same module, hidden
3043 // or protected) if we are generating PIC.
3044 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
3045 return G->getGlobal()->hasHiddenVisibility()
3046 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003047 }
3048
3049 return false;
3050}
3051
Chris Lattnereb755fc2006-05-17 19:00:46 +00003052/// isCallCompatibleAddress - Return the immediate to use if the specified
3053/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003054static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00003055 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3056 if (!C) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003057
Dan Gohmaneffb8942008-09-12 16:56:44 +00003058 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003059 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00003060 SignExtend32<26>(Addr) != Addr)
Chris Lattnereb755fc2006-05-17 19:00:46 +00003061 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003062
Dan Gohmaneffb8942008-09-12 16:56:44 +00003063 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00003064 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003065}
3066
Dan Gohmand78c4002008-05-13 00:00:25 +00003067namespace {
3068
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003069struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003070 SDValue Arg;
3071 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003072 int FrameIdx;
3073
3074 TailCallArgumentInfo() : FrameIdx(0) {}
3075};
3076
Dan Gohmand78c4002008-05-13 00:00:25 +00003077}
3078
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003079/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3080static void
3081StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003082 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003083 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3084 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003085 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003086 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003087 SDValue Arg = TailCallArgs[i].Arg;
3088 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003089 int FI = TailCallArgs[i].FrameIdx;
3090 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003091 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003092 MachinePointerInfo::getFixedStack(FI),
3093 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003094 }
3095}
3096
3097/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3098/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003099static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003100 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003101 SDValue Chain,
3102 SDValue OldRetAddr,
3103 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003104 int SPDiff,
3105 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003106 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003107 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003108 if (SPDiff) {
3109 // Calculate the new stack slot for the return address.
3110 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003111 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003112 isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003113 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003114 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003115 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003116 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003117 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003118 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003119 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003120
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003121 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3122 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003123 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003124 int NewFPLoc =
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003125 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene1fbe0542009-11-12 20:49:22 +00003126 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003127 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003128 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3129 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003130 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003131 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003132 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003133 }
3134 return Chain;
3135}
3136
3137/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3138/// the position of the argument.
3139static void
3140CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003141 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003142 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003143 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003144 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003145 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003146 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003147 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003148 TailCallArgumentInfo Info;
3149 Info.Arg = Arg;
3150 Info.FrameIdxOp = FIN;
3151 Info.FrameIdx = FI;
3152 TailCallArguments.push_back(Info);
3153}
3154
3155/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3156/// stack slot. Returns the chain as result and the loaded frame pointers in
3157/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003158SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003159 int SPDiff,
3160 SDValue Chain,
3161 SDValue &LROpOut,
3162 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003163 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003164 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003165 if (SPDiff) {
3166 // Load the LR and FP stack slot for later adjusting.
Owen Anderson9f944592009-08-11 20:47:22 +00003167 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003168 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003169 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003170 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003171 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003172
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003173 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3174 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003175 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003176 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003177 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003178 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003179 Chain = SDValue(FPOpOut.getNode(), 1);
3180 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003181 }
3182 return Chain;
3183}
3184
Dale Johannesen85d41a12008-03-04 23:17:14 +00003185/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003186/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003187/// specified by the specific parameter attribute. The copy will be passed as
3188/// a byval function parameter.
3189/// Sometimes what we are copying is the end of a larger object, the part that
3190/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003191static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003192CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003193 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003194 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003195 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003196 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Chris Lattner2510de22010-09-21 05:40:29 +00003197 false, false, MachinePointerInfo(0),
3198 MachinePointerInfo(0));
Dale Johannesen85d41a12008-03-04 23:17:14 +00003199}
Chris Lattner43df5b32007-02-25 05:34:32 +00003200
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003201/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3202/// tail calls.
3203static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003204LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3205 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003206 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003207 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3208 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003209 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003210 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003211 if (!isTailCall) {
3212 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003213 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003214 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003215 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003216 else
Owen Anderson9f944592009-08-11 20:47:22 +00003217 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003218 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003219 DAG.getConstant(ArgOffset, PtrVT));
3220 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003221 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3222 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003223 // Calculate and remember argument location.
3224 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3225 TailCallArguments);
3226}
3227
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003228static
3229void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003230 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003231 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003232 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003233 MachineFunction &MF = DAG.getMachineFunction();
3234
3235 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3236 // might overwrite each other in case of tail call optimization.
3237 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003238 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003239 InFlag = SDValue();
3240 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3241 MemOpChains2, dl);
3242 if (!MemOpChains2.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003243 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003244 &MemOpChains2[0], MemOpChains2.size());
3245
3246 // Store the return address to the appropriate stack slot.
3247 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3248 isPPC64, isDarwinABI, dl);
3249
3250 // Emit callseq_end just before tailcall node.
3251 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003252 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003253 InFlag = Chain.getValue(1);
3254}
3255
3256static
3257unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003258 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003259 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3260 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003261 const PPCSubtarget &PPCSubTarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003262
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003263 bool isPPC64 = PPCSubTarget.isPPC64();
3264 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3265
Owen Anderson53aa7a92009-08-10 22:56:29 +00003266 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003267 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003268 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003269
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003270 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003271
Torok Edwin31e90d22010-08-04 20:47:44 +00003272 bool needIndirectCall = true;
3273 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003274 // If this is an absolute destination address, use the munged value.
3275 Callee = SDValue(Dest, 0);
Torok Edwin31e90d22010-08-04 20:47:44 +00003276 needIndirectCall = false;
3277 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003278
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003279 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3280 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3281 // Use indirect calls for ALL functions calls in JIT mode, since the
3282 // far-call stubs may be outside relocation limits for a BL instruction.
3283 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3284 unsigned OpFlags = 0;
3285 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003286 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003287 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003288 (G->getGlobal()->isDeclaration() ||
3289 G->getGlobal()->isWeakForLinker())) {
3290 // PC-relative references to external symbols should go through $stub,
3291 // unless we're building with the leopard linker or later, which
3292 // automatically synthesizes these stubs.
3293 OpFlags = PPCII::MO_DARWIN_STUB;
3294 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003295
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003296 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3297 // every direct call is) turn it into a TargetGlobalAddress /
3298 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin31e90d22010-08-04 20:47:44 +00003299 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003300 Callee.getValueType(),
3301 0, OpFlags);
Torok Edwin31e90d22010-08-04 20:47:44 +00003302 needIndirectCall = false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003303 }
Torok Edwin31e90d22010-08-04 20:47:44 +00003304 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003305
Torok Edwin31e90d22010-08-04 20:47:44 +00003306 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003307 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003308
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003309 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003310 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003311 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003312 // PC-relative references to external symbols should go through $stub,
3313 // unless we're building with the leopard linker or later, which
3314 // automatically synthesizes these stubs.
3315 OpFlags = PPCII::MO_DARWIN_STUB;
3316 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003317
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003318 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3319 OpFlags);
3320 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003321 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003322
Torok Edwin31e90d22010-08-04 20:47:44 +00003323 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003324 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3325 // to do the call, we can't use PPCISD::CALL.
3326 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003327
3328 if (isSVR4ABI && isPPC64) {
3329 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3330 // entry point, but to the function descriptor (the function entry point
3331 // address is part of the function descriptor though).
3332 // The function descriptor is a three doubleword structure with the
3333 // following fields: function entry point, TOC base address and
3334 // environment pointer.
3335 // Thus for a call through a function pointer, the following actions need
3336 // to be performed:
3337 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003338 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003339 // 2. Load the address of the function entry point from the function
3340 // descriptor.
3341 // 3. Load the TOC of the callee from the function descriptor into r2.
3342 // 4. Load the environment pointer from the function descriptor into
3343 // r11.
3344 // 5. Branch to the function entry point address.
3345 // 6. On return of the callee, the TOC of the caller needs to be
3346 // restored (this is done in FinishCall()).
3347 //
3348 // All those operations are flagged together to ensure that no other
3349 // operations can be scheduled in between. E.g. without flagging the
3350 // operations together, a TOC access in the caller could be scheduled
3351 // between the load of the callee TOC and the branch to the callee, which
3352 // results in the TOC access going through the TOC of the callee instead
3353 // of going through the TOC of the caller, which leads to incorrect code.
3354
3355 // Load the address of the function entry point from the function
3356 // descriptor.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003357 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003358 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, MTCTROps,
3359 InFlag.getNode() ? 3 : 2);
3360 Chain = LoadFuncPtr.getValue(1);
3361 InFlag = LoadFuncPtr.getValue(2);
3362
3363 // Load environment pointer into r11.
3364 // Offset of the environment pointer within the function descriptor.
3365 SDValue PtrOff = DAG.getIntPtrConstant(16);
3366
3367 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3368 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3369 InFlag);
3370 Chain = LoadEnvPtr.getValue(1);
3371 InFlag = LoadEnvPtr.getValue(2);
3372
3373 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3374 InFlag);
3375 Chain = EnvVal.getValue(0);
3376 InFlag = EnvVal.getValue(1);
3377
3378 // Load TOC of the callee into r2. We are using a target-specific load
3379 // with r2 hard coded, because the result of a target-independent load
3380 // would never go directly into r2, since r2 is a reserved register (which
3381 // prevents the register allocator from allocating it), resulting in an
3382 // additional register being allocated and an unnecessary move instruction
3383 // being generated.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003384 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003385 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3386 Callee, InFlag);
3387 Chain = LoadTOCPtr.getValue(0);
3388 InFlag = LoadTOCPtr.getValue(1);
3389
3390 MTCTROps[0] = Chain;
3391 MTCTROps[1] = LoadFuncPtr;
3392 MTCTROps[2] = InFlag;
3393 }
3394
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003395 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
3396 2 + (InFlag.getNode() != 0));
3397 InFlag = Chain.getValue(1);
3398
3399 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00003400 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003401 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003402 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003403 CallOpc = PPCISD::BCTRL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003404 Callee.setNode(0);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003405 // Add use of X11 (holding environment pointer)
3406 if (isSVR4ABI && isPPC64)
3407 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003408 // Add CTR register as callee so a bctr can be emitted later.
3409 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00003410 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003411 }
3412
3413 // If this is a direct call, pass the chain and the callee.
3414 if (Callee.getNode()) {
3415 Ops.push_back(Chain);
3416 Ops.push_back(Callee);
3417 }
3418 // If this is a tail call add stack pointer delta.
3419 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00003420 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003421
3422 // Add argument registers to the end of the list so that they are known live
3423 // into the call.
3424 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3425 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3426 RegsToPass[i].second.getValueType()));
3427
3428 return CallOpc;
3429}
3430
Roman Divacky76293062012-09-18 16:47:58 +00003431static
3432bool isLocalCall(const SDValue &Callee)
3433{
3434 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00003435 return !G->getGlobal()->isDeclaration() &&
3436 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00003437 return false;
3438}
3439
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003440SDValue
3441PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003442 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003443 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003444 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003445 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003446
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003447 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003448 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003449 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003450 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003451
3452 // Copy all of the result registers out of their specified physreg.
3453 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3454 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003455 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00003456
3457 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3458 VA.getLocReg(), VA.getLocVT(), InFlag);
3459 Chain = Val.getValue(1);
3460 InFlag = Val.getValue(2);
3461
3462 switch (VA.getLocInfo()) {
3463 default: llvm_unreachable("Unknown loc info!");
3464 case CCValAssign::Full: break;
3465 case CCValAssign::AExt:
3466 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3467 break;
3468 case CCValAssign::ZExt:
3469 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3470 DAG.getValueType(VA.getValVT()));
3471 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3472 break;
3473 case CCValAssign::SExt:
3474 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3475 DAG.getValueType(VA.getValVT()));
3476 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3477 break;
3478 }
3479
3480 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003481 }
3482
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003483 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003484}
3485
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003486SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00003487PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003488 bool isTailCall, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003489 SelectionDAG &DAG,
3490 SmallVector<std::pair<unsigned, SDValue>, 8>
3491 &RegsToPass,
3492 SDValue InFlag, SDValue Chain,
3493 SDValue &Callee,
3494 int SPDiff, unsigned NumBytes,
3495 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003496 SmallVectorImpl<SDValue> &InVals) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003497 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003498 SmallVector<SDValue, 8> Ops;
3499 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3500 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003501 PPCSubTarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003502
Hal Finkel5ab37802012-08-28 02:10:27 +00003503 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3504 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3505 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3506
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003507 // When performing tail call optimization the callee pops its arguments off
3508 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00003509 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003510 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003511 (CallConv == CallingConv::Fast &&
3512 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003513
Roman Divackyef21be22012-03-06 16:41:49 +00003514 // Add a register mask operand representing the call-preserved registers.
3515 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3516 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3517 assert(Mask && "Missing call preserved mask for calling convention");
3518 Ops.push_back(DAG.getRegisterMask(Mask));
3519
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003520 if (InFlag.getNode())
3521 Ops.push_back(InFlag);
3522
3523 // Emit tail call.
3524 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003525 assert(((Callee.getOpcode() == ISD::Register &&
3526 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3527 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3528 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3529 isa<ConstantSDNode>(Callee)) &&
3530 "Expecting an global address, external symbol, absolute value or register");
3531
Owen Anderson9f944592009-08-11 20:47:22 +00003532 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003533 }
3534
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003535 // Add a NOP immediately after the branch instruction when using the 64-bit
3536 // SVR4 ABI. At link time, if caller and callee are in a different module and
3537 // thus have a different TOC, the call will be replaced with a call to a stub
3538 // function which saves the current TOC, loads the TOC of the callee and
3539 // branches to the callee. The NOP will be replaced with a load instruction
3540 // which restores the TOC of the caller from the TOC save slot of the current
3541 // stack frame. If caller and callee belong to the same module (and have the
3542 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00003543
3544 bool needsTOCRestore = false;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003545 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003546 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003547 // This is a call through a function pointer.
3548 // Restore the caller TOC from the save area into R2.
3549 // See PrepareCall() for more information about calls through function
3550 // pointers in the 64-bit SVR4 ABI.
3551 // We are using a target-specific load with r2 hard coded, because the
3552 // result of a target-independent load would never go directly into r2,
3553 // since r2 is a reserved register (which prevents the register allocator
3554 // from allocating it), resulting in an additional register being
3555 // allocated and an unnecessary move instruction being generated.
Hal Finkel51861b42012-03-31 14:45:15 +00003556 needsTOCRestore = true;
Bill Schmidtcea15962013-09-26 17:09:28 +00003557 } else if ((CallOpc == PPCISD::CALL) &&
3558 (!isLocalCall(Callee) ||
3559 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Roman Divacky76293062012-09-18 16:47:58 +00003560 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003561 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller79fef932009-12-18 13:00:15 +00003562 }
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003563 }
3564
Hal Finkel51861b42012-03-31 14:45:15 +00003565 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
3566 InFlag = Chain.getValue(1);
3567
3568 if (needsTOCRestore) {
3569 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3570 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3571 InFlag = Chain.getValue(1);
3572 }
3573
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003574 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3575 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003576 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003577 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003578 InFlag = Chain.getValue(1);
3579
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003580 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3581 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003582}
3583
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003584SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00003585PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003586 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00003587 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00003588 SDLoc &dl = CLI.DL;
3589 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3590 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3591 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00003592 SDValue Chain = CLI.Chain;
3593 SDValue Callee = CLI.Callee;
3594 bool &isTailCall = CLI.IsTailCall;
3595 CallingConv::ID CallConv = CLI.CallConv;
3596 bool isVarArg = CLI.IsVarArg;
3597
Evan Cheng67a69dd2010-01-27 00:07:07 +00003598 if (isTailCall)
3599 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3600 Ins, DAG);
3601
Bill Schmidt57d6de52012-10-23 15:51:16 +00003602 if (PPCSubTarget.isSVR4ABI()) {
3603 if (PPCSubTarget.isPPC64())
3604 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3605 isTailCall, Outs, OutVals, Ins,
3606 dl, DAG, InVals);
3607 else
3608 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3609 isTailCall, Outs, OutVals, Ins,
3610 dl, DAG, InVals);
3611 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003612
Bill Schmidt57d6de52012-10-23 15:51:16 +00003613 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3614 isTailCall, Outs, OutVals, Ins,
3615 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003616}
3617
3618SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003619PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3620 CallingConv::ID CallConv, bool isVarArg,
3621 bool isTailCall,
3622 const SmallVectorImpl<ISD::OutputArg> &Outs,
3623 const SmallVectorImpl<SDValue> &OutVals,
3624 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003625 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003626 SmallVectorImpl<SDValue> &InVals) const {
3627 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003628 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003629
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003630 assert((CallConv == CallingConv::C ||
3631 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003632
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003633 unsigned PtrByteSize = 4;
3634
3635 MachineFunction &MF = DAG.getMachineFunction();
3636
3637 // Mark this function as potentially containing a function that contains a
3638 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3639 // and restoring the callers stack pointer in this functions epilog. This is
3640 // done because by tail calling the called function might overwrite the value
3641 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003642 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3643 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003644 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00003645
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003646 // Count how many bytes are to be pushed on the stack, including the linkage
3647 // area, parameter list area and the part of the local variable space which
3648 // contains copies of aggregates which are passed by value.
3649
3650 // Assign locations to all of the outgoing arguments.
3651 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003652 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003653 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003654
3655 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003656 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003657
3658 if (isVarArg) {
3659 // Handle fixed and variable vector arguments differently.
3660 // Fixed vector arguments go into registers as long as registers are
3661 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003662 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00003663
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003664 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003665 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003666 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003667 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00003668
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003669 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00003670 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3671 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003672 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00003673 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3674 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003675 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003676
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003677 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003678#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00003679 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00003680 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003681#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00003682 llvm_unreachable(0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003683 }
3684 }
3685 } else {
3686 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00003687 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003688 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003689
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003690 // Assign locations to all of the outgoing aggregate by value arguments.
3691 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003692 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003693 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003694
3695 // Reserve stack space for the allocations in CCInfo.
3696 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3697
Bill Schmidtef17c142013-02-06 17:33:58 +00003698 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003699
3700 // Size of the linkage area, parameter list area and the part of the local
3701 // space variable where copies of aggregates which are passed by value are
3702 // stored.
3703 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003704
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003705 // Calculate by how many bytes the stack has to be adjusted in case of tail
3706 // call optimization.
3707 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3708
3709 // Adjust the stack pointer for the new arguments...
3710 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003711 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3712 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003713 SDValue CallSeqStart = Chain;
3714
3715 // Load the return address and frame pointer so it can be moved somewhere else
3716 // later.
3717 SDValue LROp, FPOp;
3718 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3719 dl);
3720
3721 // Set up a copy of the stack pointer for use loading and storing any
3722 // arguments that may not fit in the registers available for argument
3723 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00003724 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00003725
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003726 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3727 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3728 SmallVector<SDValue, 8> MemOpChains;
3729
Roman Divacky71038e72011-08-30 17:04:16 +00003730 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003731 // Walk the register/memloc assignments, inserting copies/loads.
3732 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3733 i != e;
3734 ++i) {
3735 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003736 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003737 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00003738
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003739 if (Flags.isByVal()) {
3740 // Argument is an aggregate which is passed by value, thus we need to
3741 // create a copy of it in the local variable space of the current stack
3742 // frame (which is the stack frame of the caller) and pass the address of
3743 // this copy to the callee.
3744 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3745 CCValAssign &ByValVA = ByValArgLocs[j++];
3746 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00003747
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003748 // Memory reserved in the local variable space of the callers stack frame.
3749 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003750
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003751 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3752 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00003753
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003754 // Create a copy of the argument in the local area of the current
3755 // stack frame.
3756 SDValue MemcpyCall =
3757 CreateCopyOfByValArgument(Arg, PtrOff,
3758 CallSeqStart.getNode()->getOperand(0),
3759 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00003760
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003761 // This must go outside the CALLSEQ_START..END.
3762 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003763 CallSeqStart.getNode()->getOperand(1),
3764 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003765 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3766 NewCallSeqStart.getNode());
3767 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00003768
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003769 // Pass the address of the aggregate copy on the stack either in a
3770 // physical register or in the parameter list area of the current stack
3771 // frame to the callee.
3772 Arg = PtrOff;
3773 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003774
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003775 if (VA.isRegLoc()) {
Roman Divacky71038e72011-08-30 17:04:16 +00003776 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003777 // Put argument in a physical register.
3778 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3779 } else {
3780 // Put argument in the parameter list area of the current stack frame.
3781 assert(VA.isMemLoc());
3782 unsigned LocMemOffset = VA.getLocMemOffset();
3783
3784 if (!isTailCall) {
3785 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3786 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3787
3788 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00003789 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00003790 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003791 } else {
3792 // Calculate and remember argument location.
3793 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3794 TailCallArguments);
3795 }
3796 }
3797 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003798
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003799 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003800 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003801 &MemOpChains[0], MemOpChains.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00003802
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003803 // Build a sequence of copy-to-reg nodes chained together with token chain
3804 // and flag operands which copy the outgoing args into the appropriate regs.
3805 SDValue InFlag;
3806 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3807 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3808 RegsToPass[i].second, InFlag);
3809 InFlag = Chain.getValue(1);
3810 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003811
Hal Finkel5ab37802012-08-28 02:10:27 +00003812 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3813 // registers.
3814 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003815 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3816 SDValue Ops[] = { Chain, InFlag };
3817
Hal Finkel5ab37802012-08-28 02:10:27 +00003818 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003819 dl, VTs, Ops, InFlag.getNode() ? 2 : 1);
3820
Hal Finkel5ab37802012-08-28 02:10:27 +00003821 InFlag = Chain.getValue(1);
3822 }
3823
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003824 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003825 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3826 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003827
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003828 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3829 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3830 Ins, InVals);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003831}
3832
Bill Schmidt57d6de52012-10-23 15:51:16 +00003833// Copy an argument into memory, being careful to do this outside the
3834// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003835SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00003836PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3837 SDValue CallSeqStart,
3838 ISD::ArgFlagsTy Flags,
3839 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003840 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003841 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3842 CallSeqStart.getNode()->getOperand(0),
3843 Flags, DAG, dl);
3844 // The MEMCPY must go outside the CALLSEQ_START..END.
3845 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003846 CallSeqStart.getNode()->getOperand(1),
3847 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00003848 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3849 NewCallSeqStart.getNode());
3850 return NewCallSeqStart;
3851}
3852
3853SDValue
3854PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003855 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003856 bool isTailCall,
3857 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003858 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003859 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003860 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003861 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003862
Bill Schmidt57d6de52012-10-23 15:51:16 +00003863 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003864
Bill Schmidt57d6de52012-10-23 15:51:16 +00003865 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3866 unsigned PtrByteSize = 8;
3867
3868 MachineFunction &MF = DAG.getMachineFunction();
3869
3870 // Mark this function as potentially containing a function that contains a
3871 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3872 // and restoring the callers stack pointer in this functions epilog. This is
3873 // done because by tail calling the called function might overwrite the value
3874 // in this function's (MF) stack pointer stack slot 0(SP).
3875 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3876 CallConv == CallingConv::Fast)
3877 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3878
3879 unsigned nAltivecParamsAtEnd = 0;
3880
3881 // Count how many bytes are to be pushed on the stack, including the linkage
3882 // area, and parameter passing area. We start with at least 48 bytes, which
3883 // is reserved space for [SP][CR][LR][3 x unused].
3884 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
3885 // of this call.
3886 unsigned NumBytes =
3887 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
3888 Outs, OutVals, nAltivecParamsAtEnd);
3889
3890 // Calculate by how many bytes the stack has to be adjusted in case of tail
3891 // call optimization.
3892 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3893
3894 // To protect arguments on the stack from being clobbered in a tail call,
3895 // force all the loads to happen before doing any other lowering.
3896 if (isTailCall)
3897 Chain = DAG.getStackArgumentTokenFactor(Chain);
3898
3899 // Adjust the stack pointer for the new arguments...
3900 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003901 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3902 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003903 SDValue CallSeqStart = Chain;
3904
3905 // Load the return address and frame pointer so it can be move somewhere else
3906 // later.
3907 SDValue LROp, FPOp;
3908 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
3909 dl);
3910
3911 // Set up a copy of the stack pointer for use loading and storing any
3912 // arguments that may not fit in the registers available for argument
3913 // passing.
3914 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
3915
3916 // Figure out which arguments are going to go in registers, and which in
3917 // memory. Also, if this is a vararg function, floating point operations
3918 // must be stored to our stack, and loaded into integer regs as well, if
3919 // any integer regs are available for argument passing.
3920 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
3921 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
3922
3923 static const uint16_t GPR[] = {
3924 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3925 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3926 };
3927 static const uint16_t *FPR = GetFPR();
3928
3929 static const uint16_t VR[] = {
3930 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3931 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3932 };
3933 const unsigned NumGPRs = array_lengthof(GPR);
3934 const unsigned NumFPRs = 13;
3935 const unsigned NumVRs = array_lengthof(VR);
3936
3937 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3938 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3939
3940 SmallVector<SDValue, 8> MemOpChains;
3941 for (unsigned i = 0; i != NumOps; ++i) {
3942 SDValue Arg = OutVals[i];
3943 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3944
3945 // PtrOff will be used to store the current argument to the stack if a
3946 // register cannot be found for it.
3947 SDValue PtrOff;
3948
3949 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
3950
3951 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
3952
3953 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00003954 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003955 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
3956 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3957 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
3958 }
3959
3960 // FIXME memcpy is used way more than necessary. Correctness first.
3961 // Note: "by value" is code for passing a structure by value, not
3962 // basic types.
3963 if (Flags.isByVal()) {
3964 // Note: Size includes alignment padding, so
3965 // struct x { short a; char b; }
3966 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
3967 // These are the proper values we need for right-justifying the
3968 // aggregate in a parameter register.
3969 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00003970
3971 // An empty aggregate parameter takes up no storage and no
3972 // registers.
3973 if (Size == 0)
3974 continue;
3975
Hal Finkel262a2242013-09-12 23:20:06 +00003976 unsigned BVAlign = Flags.getByValAlign();
3977 if (BVAlign > 8) {
3978 if (BVAlign % PtrByteSize != 0)
3979 llvm_unreachable(
3980 "ByVal alignment is not a multiple of the pointer size");
3981
3982 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
3983 }
3984
Bill Schmidt57d6de52012-10-23 15:51:16 +00003985 // All aggregates smaller than 8 bytes must be passed right-justified.
3986 if (Size==1 || Size==2 || Size==4) {
3987 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
3988 if (GPR_idx != NumGPRs) {
3989 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
3990 MachinePointerInfo(), VT,
3991 false, false, 0);
3992 MemOpChains.push_back(Load.getValue(1));
3993 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3994
3995 ArgOffset += PtrByteSize;
3996 continue;
3997 }
3998 }
3999
4000 if (GPR_idx == NumGPRs && Size < 8) {
4001 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4002 PtrOff.getValueType());
4003 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4004 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4005 CallSeqStart,
4006 Flags, DAG, dl);
4007 ArgOffset += PtrByteSize;
4008 continue;
4009 }
4010 // Copy entire object into memory. There are cases where gcc-generated
4011 // code assumes it is there, even if it could be put entirely into
4012 // registers. (This is not what the doc says.)
4013
4014 // FIXME: The above statement is likely due to a misunderstanding of the
4015 // documents. All arguments must be copied into the parameter area BY
4016 // THE CALLEE in the event that the callee takes the address of any
4017 // formal argument. That has not yet been implemented. However, it is
4018 // reasonable to use the stack area as a staging area for the register
4019 // load.
4020
4021 // Skip this for small aggregates, as we will use the same slot for a
4022 // right-justified copy, below.
4023 if (Size >= 8)
4024 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4025 CallSeqStart,
4026 Flags, DAG, dl);
4027
4028 // When a register is available, pass a small aggregate right-justified.
4029 if (Size < 8 && GPR_idx != NumGPRs) {
4030 // The easiest way to get this right-justified in a register
4031 // is to copy the structure into the rightmost portion of a
4032 // local variable slot, then load the whole slot into the
4033 // register.
4034 // FIXME: The memcpy seems to produce pretty awful code for
4035 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00004036 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00004037 // parameter save area instead of a new local variable.
4038 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
4039 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4040 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4041 CallSeqStart,
4042 Flags, DAG, dl);
4043
4044 // Load the slot into the register.
4045 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
4046 MachinePointerInfo(),
4047 false, false, false, 0);
4048 MemOpChains.push_back(Load.getValue(1));
4049 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4050
4051 // Done with this argument.
4052 ArgOffset += PtrByteSize;
4053 continue;
4054 }
4055
4056 // For aggregates larger than PtrByteSize, copy the pieces of the
4057 // object that fit into registers from the parameter save area.
4058 for (unsigned j=0; j<Size; j+=PtrByteSize) {
4059 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
4060 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
4061 if (GPR_idx != NumGPRs) {
4062 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4063 MachinePointerInfo(),
4064 false, false, false, 0);
4065 MemOpChains.push_back(Load.getValue(1));
4066 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4067 ArgOffset += PtrByteSize;
4068 } else {
4069 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4070 break;
4071 }
4072 }
4073 continue;
4074 }
4075
Craig Topper56710102013-08-15 02:33:50 +00004076 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004077 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00004078 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004079 case MVT::i32:
4080 case MVT::i64:
4081 if (GPR_idx != NumGPRs) {
4082 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
4083 } else {
4084 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4085 true, isTailCall, false, MemOpChains,
4086 TailCallArguments, dl);
4087 }
4088 ArgOffset += PtrByteSize;
4089 break;
4090 case MVT::f32:
4091 case MVT::f64:
4092 if (FPR_idx != NumFPRs) {
4093 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4094
4095 if (isVarArg) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004096 // A single float or an aggregate containing only a single float
4097 // must be passed right-justified in the stack doubleword, and
4098 // in the GPR, if one is available.
4099 SDValue StoreOff;
Craig Topper56710102013-08-15 02:33:50 +00004100 if (Arg.getSimpleValueType().SimpleTy == MVT::f32) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004101 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4102 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4103 } else
4104 StoreOff = PtrOff;
4105
4106 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004107 MachinePointerInfo(), false, false, 0);
4108 MemOpChains.push_back(Store);
4109
4110 // Float varargs are always shadowed in available integer registers
4111 if (GPR_idx != NumGPRs) {
4112 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4113 MachinePointerInfo(), false, false,
4114 false, 0);
4115 MemOpChains.push_back(Load.getValue(1));
4116 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4117 }
4118 } else if (GPR_idx != NumGPRs)
4119 // If we have any FPRs remaining, we may also have GPRs remaining.
4120 ++GPR_idx;
4121 } else {
4122 // Single-precision floating-point values are mapped to the
4123 // second (rightmost) word of the stack doubleword.
4124 if (Arg.getValueType() == MVT::f32) {
4125 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4126 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4127 }
4128
4129 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4130 true, isTailCall, false, MemOpChains,
4131 TailCallArguments, dl);
4132 }
4133 ArgOffset += 8;
4134 break;
4135 case MVT::v4f32:
4136 case MVT::v4i32:
4137 case MVT::v8i16:
4138 case MVT::v16i8:
4139 if (isVarArg) {
4140 // These go aligned on the stack, or in the corresponding R registers
4141 // when within range. The Darwin PPC ABI doc claims they also go in
4142 // V registers; in fact gcc does this only for arguments that are
4143 // prototyped, not for those that match the ... We do it for all
4144 // arguments, seems to work.
4145 while (ArgOffset % 16 !=0) {
4146 ArgOffset += PtrByteSize;
4147 if (GPR_idx != NumGPRs)
4148 GPR_idx++;
4149 }
4150 // We could elide this store in the case where the object fits
4151 // entirely in R registers. Maybe later.
4152 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
4153 DAG.getConstant(ArgOffset, PtrVT));
4154 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4155 MachinePointerInfo(), false, false, 0);
4156 MemOpChains.push_back(Store);
4157 if (VR_idx != NumVRs) {
4158 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4159 MachinePointerInfo(),
4160 false, false, false, 0);
4161 MemOpChains.push_back(Load.getValue(1));
4162 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4163 }
4164 ArgOffset += 16;
4165 for (unsigned i=0; i<16; i+=PtrByteSize) {
4166 if (GPR_idx == NumGPRs)
4167 break;
4168 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4169 DAG.getConstant(i, PtrVT));
4170 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4171 false, false, false, 0);
4172 MemOpChains.push_back(Load.getValue(1));
4173 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4174 }
4175 break;
4176 }
4177
4178 // Non-varargs Altivec params generally go in registers, but have
4179 // stack space allocated at the end.
4180 if (VR_idx != NumVRs) {
4181 // Doesn't have GPR space allocated.
4182 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4183 } else {
4184 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4185 true, isTailCall, true, MemOpChains,
4186 TailCallArguments, dl);
4187 ArgOffset += 16;
4188 }
4189 break;
4190 }
4191 }
4192
4193 if (!MemOpChains.empty())
4194 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4195 &MemOpChains[0], MemOpChains.size());
4196
4197 // Check if this is an indirect call (MTCTR/BCTRL).
4198 // See PrepareCall() for more information about calls through function
4199 // pointers in the 64-bit SVR4 ABI.
4200 if (!isTailCall &&
4201 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4202 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4203 !isBLACompatibleAddress(Callee, DAG)) {
4204 // Load r2 into a virtual register and store it to the TOC save area.
4205 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4206 // TOC save area offset.
4207 SDValue PtrOff = DAG.getIntPtrConstant(40);
4208 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4209 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4210 false, false, 0);
4211 // R12 must contain the address of an indirect callee. This does not
4212 // mean the MTCTR instruction must use R12; it's easier to model this
4213 // as an extra parameter, so do that.
4214 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4215 }
4216
4217 // Build a sequence of copy-to-reg nodes chained together with token chain
4218 // and flag operands which copy the outgoing args into the appropriate regs.
4219 SDValue InFlag;
4220 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4221 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4222 RegsToPass[i].second, InFlag);
4223 InFlag = Chain.getValue(1);
4224 }
4225
4226 if (isTailCall)
4227 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4228 FPOp, true, TailCallArguments);
4229
4230 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4231 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4232 Ins, InVals);
4233}
4234
4235SDValue
4236PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4237 CallingConv::ID CallConv, bool isVarArg,
4238 bool isTailCall,
4239 const SmallVectorImpl<ISD::OutputArg> &Outs,
4240 const SmallVectorImpl<SDValue> &OutVals,
4241 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004242 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004243 SmallVectorImpl<SDValue> &InVals) const {
4244
4245 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004246
Owen Anderson53aa7a92009-08-10 22:56:29 +00004247 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00004248 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004249 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004250
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004251 MachineFunction &MF = DAG.getMachineFunction();
4252
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004253 // Mark this function as potentially containing a function that contains a
4254 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4255 // and restoring the callers stack pointer in this functions epilog. This is
4256 // done because by tail calling the called function might overwrite the value
4257 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004258 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4259 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004260 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4261
4262 unsigned nAltivecParamsAtEnd = 0;
4263
Chris Lattneraa40ec12006-05-16 22:56:08 +00004264 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00004265 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00004266 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004267 unsigned NumBytes =
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004268 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004269 Outs, OutVals,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004270 nAltivecParamsAtEnd);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004271
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004272 // Calculate by how many bytes the stack has to be adjusted in case of tail
4273 // call optimization.
4274 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004275
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004276 // To protect arguments on the stack from being clobbered in a tail call,
4277 // force all the loads to happen before doing any other lowering.
4278 if (isTailCall)
4279 Chain = DAG.getStackArgumentTokenFactor(Chain);
4280
Chris Lattnerb7552a82006-05-17 00:15:40 +00004281 // Adjust the stack pointer for the new arguments...
4282 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004283 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4284 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004285 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004286
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004287 // Load the return address and frame pointer so it can be move somewhere else
4288 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004289 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004290 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4291 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004292
Chris Lattnerb7552a82006-05-17 00:15:40 +00004293 // Set up a copy of the stack pointer for use loading and storing any
4294 // arguments that may not fit in the registers available for argument
4295 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004296 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004297 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004298 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004299 else
Owen Anderson9f944592009-08-11 20:47:22 +00004300 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004301
Chris Lattnerb7552a82006-05-17 00:15:40 +00004302 // Figure out which arguments are going to go in registers, and which in
4303 // memory. Also, if this is a vararg function, floating point operations
4304 // must be stored to our stack, and loaded into integer regs as well, if
4305 // any integer regs are available for argument passing.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004306 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004307 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004308
Craig Topperca658c22012-03-11 07:16:55 +00004309 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004310 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4311 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4312 };
Craig Topperca658c22012-03-11 07:16:55 +00004313 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00004314 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4315 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4316 };
Craig Topperca658c22012-03-11 07:16:55 +00004317 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004318
Craig Topperca658c22012-03-11 07:16:55 +00004319 static const uint16_t VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004320 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4321 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4322 };
Owen Andersone2f23a32007-09-07 04:06:50 +00004323 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004324 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00004325 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004326
Craig Topperca658c22012-03-11 07:16:55 +00004327 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004328
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004329 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004330 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4331
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004332 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00004333 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004334 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004335 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004336
Chris Lattnerb7552a82006-05-17 00:15:40 +00004337 // PtrOff will be used to store the current argument to the stack if a
4338 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004339 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004340
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004341 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004342
Dale Johannesen679073b2009-02-04 02:34:38 +00004343 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004344
4345 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00004346 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00004347 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4348 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00004349 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004350 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00004351
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004352 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004353 // Note: "by value" is code for passing a structure by value, not
4354 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00004355 if (Flags.isByVal()) {
4356 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004357 // Very small objects are passed right-justified. Everything else is
4358 // passed left-justified.
4359 if (Size==1 || Size==2) {
4360 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004361 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00004362 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00004363 MachinePointerInfo(), VT,
4364 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004365 MemOpChains.push_back(Load.getValue(1));
4366 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004367
4368 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004369 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00004370 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4371 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004372 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004373 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4374 CallSeqStart,
4375 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004376 ArgOffset += PtrByteSize;
4377 }
4378 continue;
4379 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004380 // Copy entire object into memory. There are cases where gcc-generated
4381 // code assumes it is there, even if it could be put entirely into
4382 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004383 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4384 CallSeqStart,
4385 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004386
4387 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4388 // copy the pieces of the object that fit into registers from the
4389 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00004390 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004391 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004392 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00004393 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004394 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4395 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004396 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00004397 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00004398 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004399 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004400 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004401 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004402 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004403 }
4404 }
4405 continue;
4406 }
4407
Craig Topper56710102013-08-15 02:33:50 +00004408 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004409 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00004410 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00004411 case MVT::i32:
4412 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004413 if (GPR_idx != NumGPRs) {
4414 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004415 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004416 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4417 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004418 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00004419 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004420 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004421 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004422 case MVT::f32:
4423 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004424 if (FPR_idx != NumFPRs) {
4425 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4426
Chris Lattnerb7552a82006-05-17 00:15:40 +00004427 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00004428 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4429 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004430 MemOpChains.push_back(Store);
4431
Chris Lattnerb7552a82006-05-17 00:15:40 +00004432 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004433 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004434 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004435 MachinePointerInfo(), false, false,
4436 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004437 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004438 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004439 }
Owen Anderson9f944592009-08-11 20:47:22 +00004440 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004441 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004442 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00004443 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4444 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004445 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004446 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004447 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00004448 }
4449 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00004450 // If we have any FPRs remaining, we may also have GPRs remaining.
4451 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4452 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004453 if (GPR_idx != NumGPRs)
4454 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00004455 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004456 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4457 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004458 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004459 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004460 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4461 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004462 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004463 if (isPPC64)
4464 ArgOffset += 8;
4465 else
Owen Anderson9f944592009-08-11 20:47:22 +00004466 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004467 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004468 case MVT::v4f32:
4469 case MVT::v4i32:
4470 case MVT::v8i16:
4471 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00004472 if (isVarArg) {
4473 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00004474 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00004475 // V registers; in fact gcc does this only for arguments that are
4476 // prototyped, not for those that match the ... We do it for all
4477 // arguments, seems to work.
4478 while (ArgOffset % 16 !=0) {
4479 ArgOffset += PtrByteSize;
4480 if (GPR_idx != NumGPRs)
4481 GPR_idx++;
4482 }
4483 // We could elide this store in the case where the object fits
4484 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004485 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004486 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00004487 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4488 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004489 MemOpChains.push_back(Store);
4490 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004491 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00004492 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004493 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004494 MemOpChains.push_back(Load.getValue(1));
4495 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4496 }
4497 ArgOffset += 16;
4498 for (unsigned i=0; i<16; i+=PtrByteSize) {
4499 if (GPR_idx == NumGPRs)
4500 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00004501 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004502 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00004503 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004504 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004505 MemOpChains.push_back(Load.getValue(1));
4506 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4507 }
4508 break;
4509 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004510
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004511 // Non-varargs Altivec params generally go in registers, but have
4512 // stack space allocated at the end.
4513 if (VR_idx != NumVRs) {
4514 // Doesn't have GPR space allocated.
4515 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4516 } else if (nAltivecParamsAtEnd==0) {
4517 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004518 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4519 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004520 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004521 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00004522 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00004523 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004524 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00004525 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004526 // If all Altivec parameters fit in registers, as they usually do,
4527 // they get stack space following the non-Altivec parameters. We
4528 // don't track this here because nobody below needs it.
4529 // If there are more Altivec parameters than fit in registers emit
4530 // the stores here.
4531 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4532 unsigned j = 0;
4533 // Offset is aligned; skip 1st 12 params which go in V registers.
4534 ArgOffset = ((ArgOffset+15)/16)*16;
4535 ArgOffset += 12*16;
4536 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004537 SDValue Arg = OutVals[i];
4538 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00004539 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4540 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004541 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004542 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004543 // We are emitting Altivec params in order.
4544 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4545 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004546 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004547 ArgOffset += 16;
4548 }
4549 }
4550 }
4551 }
4552
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004553 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00004554 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnered728e82006-08-11 17:38:39 +00004555 &MemOpChains[0], MemOpChains.size());
Scott Michelcf0da6c2009-02-17 22:15:04 +00004556
Dale Johannesen90eab672010-03-09 20:15:42 +00004557 // On Darwin, R12 must contain the address of an indirect callee. This does
4558 // not mean the MTCTR instruction must use R12; it's easier to model this as
4559 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004560 if (!isTailCall &&
Dale Johannesen90eab672010-03-09 20:15:42 +00004561 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4562 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4563 !isBLACompatibleAddress(Callee, DAG))
4564 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4565 PPC::R12), Callee));
4566
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004567 // Build a sequence of copy-to-reg nodes chained together with token chain
4568 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004569 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004570 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00004571 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00004572 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004573 InFlag = Chain.getValue(1);
4574 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004575
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004576 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004577 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4578 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004579
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004580 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4581 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4582 Ins, InVals);
Chris Lattneraa40ec12006-05-16 22:56:08 +00004583}
4584
Hal Finkel450128a2011-10-14 19:51:36 +00004585bool
4586PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4587 MachineFunction &MF, bool isVarArg,
4588 const SmallVectorImpl<ISD::OutputArg> &Outs,
4589 LLVMContext &Context) const {
4590 SmallVector<CCValAssign, 16> RVLocs;
4591 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4592 RVLocs, Context);
4593 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4594}
4595
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004596SDValue
4597PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004598 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004599 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004600 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004601 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004602
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004603 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004604 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00004605 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004606 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004607
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004608 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004609 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004610
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004611 // Copy the result values into the output registers.
4612 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4613 CCValAssign &VA = RVLocs[i];
4614 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004615
4616 SDValue Arg = OutVals[i];
4617
4618 switch (VA.getLocInfo()) {
4619 default: llvm_unreachable("Unknown loc info!");
4620 case CCValAssign::Full: break;
4621 case CCValAssign::AExt:
4622 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4623 break;
4624 case CCValAssign::ZExt:
4625 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4626 break;
4627 case CCValAssign::SExt:
4628 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4629 break;
4630 }
4631
4632 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004633 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004634 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004635 }
4636
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004637 RetOps[0] = Chain; // Update chain.
4638
4639 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00004640 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004641 RetOps.push_back(Flag);
4642
4643 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other,
4644 &RetOps[0], RetOps.size());
Chris Lattner4211ca92006-04-14 06:01:58 +00004645}
4646
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004647SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004648 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00004649 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004650 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004651
Jim Laskeye4f4d042006-12-04 22:04:42 +00004652 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004653 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00004654
4655 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00004656 bool isPPC64 = Subtarget.isPPC64();
4657 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004658 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004659
4660 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004661 SDValue Chain = Op.getOperand(0);
4662 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004663
Jim Laskeye4f4d042006-12-04 22:04:42 +00004664 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00004665 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4666 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004667 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004668
Jim Laskeye4f4d042006-12-04 22:04:42 +00004669 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00004670 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004671
Jim Laskeye4f4d042006-12-04 22:04:42 +00004672 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00004673 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004674 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004675}
4676
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004677
4678
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004679SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004680PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004681 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004682 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004683 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004684 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004685
4686 // Get current frame pointer save index. The users of this index will be
4687 // primarily DYNALLOC instructions.
4688 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4689 int RASI = FI->getReturnAddrSaveIndex();
4690
4691 // If the frame pointer save index hasn't been defined yet.
4692 if (!RASI) {
4693 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004694 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004695 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004696 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004697 // Save the result.
4698 FI->setReturnAddrSaveIndex(RASI);
4699 }
4700 return DAG.getFrameIndex(RASI, PtrVT);
4701}
4702
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004703SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004704PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4705 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004706 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004707 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004708 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004709
4710 // Get current frame pointer save index. The users of this index will be
4711 // primarily DYNALLOC instructions.
4712 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4713 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004714
Jim Laskey48850c12006-11-16 22:43:37 +00004715 // If the frame pointer save index hasn't been defined yet.
4716 if (!FPSI) {
4717 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004718 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004719 isDarwinABI);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004720
Jim Laskey48850c12006-11-16 22:43:37 +00004721 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004722 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00004723 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004724 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00004725 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004726 return DAG.getFrameIndex(FPSI, PtrVT);
4727}
Jim Laskey48850c12006-11-16 22:43:37 +00004728
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004729SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004730 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004731 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004732 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004733 SDValue Chain = Op.getOperand(0);
4734 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004735 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004736
Jim Laskey48850c12006-11-16 22:43:37 +00004737 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004738 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004739 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004740 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00004741 DAG.getConstant(0, PtrVT), Size);
4742 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004743 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00004744 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004745 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00004746 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004747 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey48850c12006-11-16 22:43:37 +00004748}
4749
Hal Finkel756810f2013-03-21 21:37:52 +00004750SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
4751 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004752 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004753 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
4754 DAG.getVTList(MVT::i32, MVT::Other),
4755 Op.getOperand(0), Op.getOperand(1));
4756}
4757
4758SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
4759 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004760 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004761 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
4762 Op.getOperand(0), Op.getOperand(1));
4763}
4764
Hal Finkel940ab932014-02-28 00:27:01 +00004765SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
4766 assert(Op.getValueType() == MVT::i1 &&
4767 "Custom lowering only for i1 loads");
4768
4769 // First, load 8 bits into 32 bits, then truncate to 1 bit.
4770
4771 SDLoc dl(Op);
4772 LoadSDNode *LD = cast<LoadSDNode>(Op);
4773
4774 SDValue Chain = LD->getChain();
4775 SDValue BasePtr = LD->getBasePtr();
4776 MachineMemOperand *MMO = LD->getMemOperand();
4777
4778 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(), Chain,
4779 BasePtr, MVT::i8, MMO);
4780 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
4781
4782 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
4783 return DAG.getMergeValues(Ops, 2, dl);
4784}
4785
4786SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
4787 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
4788 "Custom lowering only for i1 stores");
4789
4790 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
4791
4792 SDLoc dl(Op);
4793 StoreSDNode *ST = cast<StoreSDNode>(Op);
4794
4795 SDValue Chain = ST->getChain();
4796 SDValue BasePtr = ST->getBasePtr();
4797 SDValue Value = ST->getValue();
4798 MachineMemOperand *MMO = ST->getMemOperand();
4799
4800 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(), Value);
4801 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
4802}
4803
4804// FIXME: Remove this once the ANDI glue bug is fixed:
4805SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
4806 assert(Op.getValueType() == MVT::i1 &&
4807 "Custom lowering only for i1 results");
4808
4809 SDLoc DL(Op);
4810 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
4811 Op.getOperand(0));
4812}
4813
Chris Lattner4211ca92006-04-14 06:01:58 +00004814/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4815/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004816SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00004817 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00004818 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4819 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00004820 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004821
Hal Finkel81f87992013-04-07 22:11:09 +00004822 // We might be able to do better than this under some circumstances, but in
4823 // general, fsel-based lowering of select is a finite-math-only optimization.
4824 // For more information, see section F.3 of the 2.06 ISA specification.
4825 if (!DAG.getTarget().Options.NoInfsFPMath ||
4826 !DAG.getTarget().Options.NoNaNsFPMath)
4827 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004828
Hal Finkel81f87992013-04-07 22:11:09 +00004829 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004830
Owen Anderson53aa7a92009-08-10 22:56:29 +00004831 EVT ResVT = Op.getValueType();
4832 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004833 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4834 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004835 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004836
Chris Lattner4211ca92006-04-14 06:01:58 +00004837 // If the RHS of the comparison is a 0.0, we don't need to do the
4838 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00004839 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00004840 if (isFloatingPointZero(RHS))
4841 switch (CC) {
4842 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004843 case ISD::SETNE:
4844 std::swap(TV, FV);
4845 case ISD::SETEQ:
4846 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4847 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
4848 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
4849 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4850 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4851 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4852 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004853 case ISD::SETULT:
4854 case ISD::SETLT:
4855 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004856 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004857 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00004858 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4859 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004860 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004861 case ISD::SETUGT:
4862 case ISD::SETGT:
4863 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004864 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004865 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00004866 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4867 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004868 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00004869 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004870 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004871
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004872 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00004873 switch (CC) {
4874 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004875 case ISD::SETNE:
4876 std::swap(TV, FV);
4877 case ISD::SETEQ:
4878 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
4879 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4880 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
4881 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
4882 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4883 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4884 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4885 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004886 case ISD::SETULT:
4887 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004888 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004889 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4890 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004891 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004892 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004893 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004894 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004895 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4896 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004897 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004898 case ISD::SETUGT:
4899 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004900 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004901 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4902 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004903 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004904 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004905 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004906 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004907 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4908 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004909 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004910 }
Eli Friedman5806e182009-05-28 04:31:08 +00004911 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00004912}
4913
Chris Lattner57ee7c62007-11-28 18:44:47 +00004914// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen37bc85f2009-06-04 20:53:52 +00004915SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004916 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00004917 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004918 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00004919 if (Src.getValueType() == MVT::f32)
4920 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00004921
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004922 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00004923 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004924 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00004925 case MVT::i32:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00004926 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Hal Finkelf6d45f22013-04-01 17:52:07 +00004927 (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ :
4928 PPCISD::FCTIDZ),
Owen Anderson9f944592009-08-11 20:47:22 +00004929 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00004930 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004931 case MVT::i64:
Hal Finkel3f88d082013-04-01 18:42:58 +00004932 assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) &&
4933 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00004934 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
4935 PPCISD::FCTIDUZ,
4936 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00004937 break;
4938 }
Duncan Sands2a287912008-07-19 16:26:02 +00004939
Chris Lattner4211ca92006-04-14 06:01:58 +00004940 // Convert the FP value to an int value through memory.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004941 bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() &&
4942 (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT());
4943 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
4944 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
4945 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00004946
Chris Lattner06a49542007-10-15 20:14:52 +00004947 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004948 SDValue Chain;
4949 if (i32Stack) {
4950 MachineFunction &MF = DAG.getMachineFunction();
4951 MachineMemOperand *MMO =
4952 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
4953 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
4954 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
4955 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
4956 MVT::i32, MMO);
4957 } else
4958 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
4959 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00004960
4961 // Result is a load from the stack slot. If loading 4 bytes, make sure to
4962 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004963 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00004964 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00004965 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkelf6d45f22013-04-01 17:52:07 +00004966 MPI = MachinePointerInfo();
4967 }
4968
4969 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004970 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00004971}
4972
Hal Finkelf6d45f22013-04-01 17:52:07 +00004973SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004974 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004975 SDLoc dl(Op);
Dan Gohmand6819da2008-03-11 01:59:03 +00004976 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00004977 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004978 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00004979
Hal Finkel6a56b212014-03-05 22:14:00 +00004980 if (Op.getOperand(0).getValueType() == MVT::i1)
4981 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0),
4982 DAG.getConstantFP(1.0, Op.getValueType()),
4983 DAG.getConstantFP(0.0, Op.getValueType()));
4984
Hal Finkelf6d45f22013-04-01 17:52:07 +00004985 assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) &&
4986 "UINT_TO_FP is supported only with FPCVT");
4987
4988 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00004989 // Otherwise, convert to double-precision and then round.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004990 unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4991 (Op.getOpcode() == ISD::UINT_TO_FP ?
4992 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
4993 (Op.getOpcode() == ISD::UINT_TO_FP ?
4994 PPCISD::FCFIDU : PPCISD::FCFID);
4995 MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4996 MVT::f32 : MVT::f64;
4997
Owen Anderson9f944592009-08-11 20:47:22 +00004998 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00004999 SDValue SINT = Op.getOperand(0);
5000 // When converting to single-precision, we actually need to convert
5001 // to double-precision first and then round to single-precision.
5002 // To avoid double-rounding effects during that operation, we have
5003 // to prepare the input operand. Bits that might be truncated when
5004 // converting to double-precision are replaced by a bit that won't
5005 // be lost at this stage, but is below the single-precision rounding
5006 // position.
5007 //
5008 // However, if -enable-unsafe-fp-math is in effect, accept double
5009 // rounding to avoid the extra overhead.
5010 if (Op.getValueType() == MVT::f32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005011 !PPCSubTarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005012 !DAG.getTarget().Options.UnsafeFPMath) {
5013
5014 // Twiddle input to make sure the low 11 bits are zero. (If this
5015 // is the case, we are guaranteed the value will fit into the 53 bit
5016 // mantissa of an IEEE double-precision value without rounding.)
5017 // If any of those low 11 bits were not zero originally, make sure
5018 // bit 12 (value 2048) is set instead, so that the final rounding
5019 // to single-precision gets the correct result.
5020 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5021 SINT, DAG.getConstant(2047, MVT::i64));
5022 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
5023 Round, DAG.getConstant(2047, MVT::i64));
5024 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
5025 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5026 Round, DAG.getConstant(-2048, MVT::i64));
5027
5028 // However, we cannot use that value unconditionally: if the magnitude
5029 // of the input value is small, the bit-twiddling we did above might
5030 // end up visibly changing the output. Fortunately, in that case, we
5031 // don't need to twiddle bits since the original input will convert
5032 // exactly to double-precision floating-point already. Therefore,
5033 // construct a conditional to use the original value if the top 11
5034 // bits are all sign-bit copies, and use the rounded value computed
5035 // above otherwise.
5036 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
5037 SINT, DAG.getConstant(53, MVT::i32));
5038 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
5039 Cond, DAG.getConstant(1, MVT::i64));
5040 Cond = DAG.getSetCC(dl, MVT::i32,
5041 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
5042
5043 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
5044 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00005045
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005046 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005047 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
5048
5049 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00005050 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005051 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005052 return FP;
5053 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005054
Owen Anderson9f944592009-08-11 20:47:22 +00005055 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005056 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005057 // Since we only generate this in 64-bit mode, we can take advantage of
5058 // 64-bit registers. In particular, sign extend the input value into the
5059 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
5060 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00005061 MachineFunction &MF = DAG.getMachineFunction();
5062 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005063 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005064
Hal Finkelbeb296b2013-03-31 10:12:51 +00005065 SDValue Ld;
Hal Finkelf6d45f22013-04-01 17:52:07 +00005066 if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) {
Hal Finkelbeb296b2013-03-31 10:12:51 +00005067 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
5068 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005069
Hal Finkelbeb296b2013-03-31 10:12:51 +00005070 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
5071 MachinePointerInfo::getFixedStack(FrameIdx),
5072 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00005073
Hal Finkelbeb296b2013-03-31 10:12:51 +00005074 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
5075 "Expected an i32 store");
5076 MachineMemOperand *MMO =
5077 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
5078 MachineMemOperand::MOLoad, 4, 4);
5079 SDValue Ops[] = { Store, FIdx };
Hal Finkelf6d45f22013-04-01 17:52:07 +00005080 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
5081 PPCISD::LFIWZX : PPCISD::LFIWAX,
5082 dl, DAG.getVTList(MVT::f64, MVT::Other),
5083 Ops, 2, MVT::i32, MMO);
Hal Finkelbeb296b2013-03-31 10:12:51 +00005084 } else {
Hal Finkelf6d45f22013-04-01 17:52:07 +00005085 assert(PPCSubTarget.isPPC64() &&
5086 "i32->FP without LFIWAX supported only on PPC64");
5087
Hal Finkelbeb296b2013-03-31 10:12:51 +00005088 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
5089 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
5090
5091 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
5092 Op.getOperand(0));
5093
5094 // STD the extended value into the stack slot.
5095 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
5096 MachinePointerInfo::getFixedStack(FrameIdx),
5097 false, false, 0);
5098
5099 // Load the value as a double.
5100 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
5101 MachinePointerInfo::getFixedStack(FrameIdx),
5102 false, false, false, 0);
5103 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005104
Chris Lattner4211ca92006-04-14 06:01:58 +00005105 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005106 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
5107 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00005108 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005109 return FP;
5110}
5111
Dan Gohman21cea8a2010-04-17 15:26:15 +00005112SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
5113 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005114 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005115 /*
5116 The rounding mode is in bits 30:31 of FPSR, and has the following
5117 settings:
5118 00 Round to nearest
5119 01 Round to 0
5120 10 Round to +inf
5121 11 Round to -inf
5122
5123 FLT_ROUNDS, on the other hand, expects the following:
5124 -1 Undefined
5125 0 Round to 0
5126 1 Round to nearest
5127 2 Round to +inf
5128 3 Round to -inf
5129
5130 To perform the conversion, we do:
5131 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
5132 */
5133
5134 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005135 EVT VT = Op.getValueType();
5136 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005137 SDValue MFFSreg, InFlag;
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005138
5139 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005140 EVT NodeTys[] = {
5141 MVT::f64, // return register
5142 MVT::Glue // unused in this context
5143 };
Dale Johannesen021052a2009-02-04 20:06:27 +00005144 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005145
5146 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00005147 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005148 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005149 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00005150 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005151
5152 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005153 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005154 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00005155 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005156 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005157
5158 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005159 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00005160 DAG.getNode(ISD::AND, dl, MVT::i32,
5161 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005162 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00005163 DAG.getNode(ISD::SRL, dl, MVT::i32,
5164 DAG.getNode(ISD::AND, dl, MVT::i32,
5165 DAG.getNode(ISD::XOR, dl, MVT::i32,
5166 CWD, DAG.getConstant(3, MVT::i32)),
5167 DAG.getConstant(3, MVT::i32)),
5168 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005169
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005170 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00005171 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005172
Duncan Sands13237ac2008-06-06 12:08:01 +00005173 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00005174 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005175}
5176
Dan Gohman21cea8a2010-04-17 15:26:15 +00005177SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005178 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005179 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005180 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00005181 assert(Op.getNumOperands() == 3 &&
5182 VT == Op.getOperand(1).getValueType() &&
5183 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005184
Chris Lattner601b8652006-09-20 03:47:40 +00005185 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005186 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005187 SDValue Lo = Op.getOperand(0);
5188 SDValue Hi = Op.getOperand(1);
5189 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005190 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005191
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005192 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005193 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005194 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5195 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5196 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5197 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005198 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005199 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5200 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5201 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005202 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005203 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005204}
5205
Dan Gohman21cea8a2010-04-17 15:26:15 +00005206SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005207 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005208 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00005209 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005210 assert(Op.getNumOperands() == 3 &&
5211 VT == Op.getOperand(1).getValueType() &&
5212 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005213
Dan Gohman8d2ead22008-03-07 20:36:53 +00005214 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005215 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005216 SDValue Lo = Op.getOperand(0);
5217 SDValue Hi = Op.getOperand(1);
5218 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005219 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005220
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005221 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005222 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005223 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5224 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5225 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5226 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005227 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005228 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5229 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5230 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005231 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005232 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005233}
5234
Dan Gohman21cea8a2010-04-17 15:26:15 +00005235SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005236 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005237 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005238 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005239 assert(Op.getNumOperands() == 3 &&
5240 VT == Op.getOperand(1).getValueType() &&
5241 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005242
Dan Gohman8d2ead22008-03-07 20:36:53 +00005243 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005244 SDValue Lo = Op.getOperand(0);
5245 SDValue Hi = Op.getOperand(1);
5246 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005247 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005248
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005249 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005250 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005251 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5252 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5253 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5254 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005255 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005256 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5257 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5258 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00005259 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005260 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005261 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005262}
5263
5264//===----------------------------------------------------------------------===//
5265// Vector related lowering.
5266//
5267
Chris Lattner2a099c02006-04-17 06:00:21 +00005268/// BuildSplatI - Build a canonical splati of Val with an element size of
5269/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005270static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005271 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00005272 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005273
Owen Anderson53aa7a92009-08-10 22:56:29 +00005274 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00005275 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00005276 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005277
Owen Anderson9f944592009-08-11 20:47:22 +00005278 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005279
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005280 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5281 if (Val == -1)
5282 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005283
Owen Anderson53aa7a92009-08-10 22:56:29 +00005284 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005285
Chris Lattner2a099c02006-04-17 06:00:21 +00005286 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00005287 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005288 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00005289 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga49de9d2009-02-25 22:49:59 +00005290 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
5291 &Ops[0], Ops.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00005292 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005293}
5294
Hal Finkelcf2e9082013-05-24 23:00:14 +00005295/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5296/// specified intrinsic ID.
5297static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005298 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00005299 EVT DestVT = MVT::Other) {
5300 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5301 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5302 DAG.getConstant(IID, MVT::i32), Op);
5303}
5304
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005305/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00005306/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005307static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005308 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005309 EVT DestVT = MVT::Other) {
5310 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005311 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005312 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005313}
5314
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005315/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5316/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005317static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005318 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005319 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00005320 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005321 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005322 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005323}
5324
5325
Chris Lattner264c9082006-04-17 17:55:10 +00005326/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5327/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005328static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005329 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00005330 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00005331 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5332 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005333
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005334 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00005335 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005336 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00005337 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005338 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00005339}
5340
Chris Lattner19e90552006-04-14 05:19:18 +00005341// If this is a case we can't handle, return null and let the default
5342// expansion code take care of it. If we CAN select this case, and if it
5343// selects to a single instruction, return Op. Otherwise, if we can codegen
5344// this case more efficiently than a constant pool load, lower it to the
5345// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005346SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5347 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005348 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005349 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5350 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00005351
Bob Wilson85cefe82009-03-02 23:24:16 +00005352 // Check if this is a splat of a constant value.
5353 APInt APSplatBits, APSplatUndef;
5354 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005355 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00005356 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00005357 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00005358 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00005359
Bob Wilson530e0382009-03-03 19:26:27 +00005360 unsigned SplatBits = APSplatBits.getZExtValue();
5361 unsigned SplatUndef = APSplatUndef.getZExtValue();
5362 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005363
Bob Wilson530e0382009-03-03 19:26:27 +00005364 // First, handle single instruction cases.
5365
5366 // All zeros?
5367 if (SplatBits == 0) {
5368 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00005369 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5370 SDValue Z = DAG.getConstant(0, MVT::i32);
5371 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00005372 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00005373 }
Bob Wilson530e0382009-03-03 19:26:27 +00005374 return Op;
5375 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00005376
Bob Wilson530e0382009-03-03 19:26:27 +00005377 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5378 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5379 (32-SplatBitSize));
5380 if (SextVal >= -16 && SextVal <= 15)
5381 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005382
5383
Bob Wilson530e0382009-03-03 19:26:27 +00005384 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005385
Bob Wilson530e0382009-03-03 19:26:27 +00005386 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00005387 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5388 // If this value is in the range [17,31] and is odd, use:
5389 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5390 // If this value is in the range [-31,-17] and is odd, use:
5391 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5392 // Note the last two are three-instruction sequences.
5393 if (SextVal >= -32 && SextVal <= 31) {
5394 // To avoid having these optimizations undone by constant folding,
5395 // we convert to a pseudo that will be expanded later into one of
5396 // the above forms.
5397 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt51e79512013-02-20 15:50:31 +00005398 EVT VT = Op.getValueType();
5399 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5400 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5401 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilson530e0382009-03-03 19:26:27 +00005402 }
5403
5404 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5405 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5406 // for fneg/fabs.
5407 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5408 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00005409 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005410
5411 // Make the VSLW intrinsic, computing 0x8000_0000.
5412 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5413 OnesV, DAG, dl);
5414
5415 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00005416 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00005417 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005418 }
5419
5420 // Check to see if this is a wide variety of vsplti*, binop self cases.
5421 static const signed char SplatCsts[] = {
5422 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5423 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5424 };
5425
5426 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5427 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5428 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5429 int i = SplatCsts[idx];
5430
5431 // Figure out what shift amount will be used by altivec if shifted by i in
5432 // this splat size.
5433 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5434
5435 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00005436 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005437 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005438 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5439 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5440 Intrinsic::ppc_altivec_vslw
5441 };
5442 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005443 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005444 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005445
Bob Wilson530e0382009-03-03 19:26:27 +00005446 // vsplti + srl self.
5447 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005448 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005449 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5450 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5451 Intrinsic::ppc_altivec_vsrw
5452 };
5453 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005454 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005455 }
5456
Bob Wilson530e0382009-03-03 19:26:27 +00005457 // vsplti + sra self.
5458 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005459 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005460 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5461 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5462 Intrinsic::ppc_altivec_vsraw
5463 };
5464 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005465 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005466 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005467
Bob Wilson530e0382009-03-03 19:26:27 +00005468 // vsplti + rol self.
5469 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5470 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005471 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005472 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5473 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5474 Intrinsic::ppc_altivec_vrlw
5475 };
5476 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005477 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005478 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005479
Bob Wilson530e0382009-03-03 19:26:27 +00005480 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00005481 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005482 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005483 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00005484 }
Bob Wilson530e0382009-03-03 19:26:27 +00005485 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00005486 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005487 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005488 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00005489 }
Bob Wilson530e0382009-03-03 19:26:27 +00005490 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00005491 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005492 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005493 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5494 }
5495 }
5496
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005497 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00005498}
5499
Chris Lattner071ad012006-04-17 05:28:54 +00005500/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5501/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005502static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005503 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005504 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00005505 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00005506 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00005507 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005508
Chris Lattner071ad012006-04-17 05:28:54 +00005509 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00005510 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 +00005511 OP_VMRGHW,
5512 OP_VMRGLW,
5513 OP_VSPLTISW0,
5514 OP_VSPLTISW1,
5515 OP_VSPLTISW2,
5516 OP_VSPLTISW3,
5517 OP_VSLDOI4,
5518 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00005519 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00005520 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00005521
Chris Lattner071ad012006-04-17 05:28:54 +00005522 if (OpNum == OP_COPY) {
5523 if (LHSID == (1*9+2)*9+3) return LHS;
5524 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5525 return RHS;
5526 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005527
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005528 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005529 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5530 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005531
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005532 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00005533 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005534 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00005535 case OP_VMRGHW:
5536 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5537 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5538 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5539 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5540 break;
5541 case OP_VMRGLW:
5542 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5543 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5544 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5545 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5546 break;
5547 case OP_VSPLTISW0:
5548 for (unsigned i = 0; i != 16; ++i)
5549 ShufIdxs[i] = (i&3)+0;
5550 break;
5551 case OP_VSPLTISW1:
5552 for (unsigned i = 0; i != 16; ++i)
5553 ShufIdxs[i] = (i&3)+4;
5554 break;
5555 case OP_VSPLTISW2:
5556 for (unsigned i = 0; i != 16; ++i)
5557 ShufIdxs[i] = (i&3)+8;
5558 break;
5559 case OP_VSPLTISW3:
5560 for (unsigned i = 0; i != 16; ++i)
5561 ShufIdxs[i] = (i&3)+12;
5562 break;
5563 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005564 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005565 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005566 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005567 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005568 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005569 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00005570 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00005571 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5572 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005573 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00005574 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00005575}
5576
Chris Lattner19e90552006-04-14 05:19:18 +00005577/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5578/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5579/// return the code it can be lowered into. Worst case, it can always be
5580/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005581SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005582 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005583 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005584 SDValue V1 = Op.getOperand(0);
5585 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005586 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005587 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005588
Chris Lattner19e90552006-04-14 05:19:18 +00005589 // Cases that are handled by instructions that take permute immediates
5590 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5591 // selected by the instruction selector.
5592 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005593 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5594 PPC::isSplatShuffleMask(SVOp, 2) ||
5595 PPC::isSplatShuffleMask(SVOp, 4) ||
5596 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5597 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5598 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5599 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5600 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5601 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5602 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5603 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5604 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattner19e90552006-04-14 05:19:18 +00005605 return Op;
5606 }
5607 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005608
Chris Lattner19e90552006-04-14 05:19:18 +00005609 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5610 // and produce a fixed permutation. If any of these match, do not lower to
5611 // VPERM.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005612 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5613 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5614 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5615 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5616 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5617 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5618 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5619 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5620 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattner19e90552006-04-14 05:19:18 +00005621 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005622
Chris Lattner071ad012006-04-17 05:28:54 +00005623 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5624 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005625 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00005626
Chris Lattner071ad012006-04-17 05:28:54 +00005627 unsigned PFIndexes[4];
5628 bool isFourElementShuffle = true;
5629 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5630 unsigned EltNo = 8; // Start out undef.
5631 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005632 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00005633 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005634
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005635 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00005636 if ((ByteSource & 3) != j) {
5637 isFourElementShuffle = false;
5638 break;
5639 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005640
Chris Lattner071ad012006-04-17 05:28:54 +00005641 if (EltNo == 8) {
5642 EltNo = ByteSource/4;
5643 } else if (EltNo != ByteSource/4) {
5644 isFourElementShuffle = false;
5645 break;
5646 }
5647 }
5648 PFIndexes[i] = EltNo;
5649 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005650
5651 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00005652 // perfect shuffle vector to determine if it is cost effective to do this as
5653 // discrete instructions, or whether we should use a vperm.
5654 if (isFourElementShuffle) {
5655 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005656 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00005657 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005658
Chris Lattner071ad012006-04-17 05:28:54 +00005659 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5660 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005661
Chris Lattner071ad012006-04-17 05:28:54 +00005662 // Determining when to avoid vperm is tricky. Many things affect the cost
5663 // of vperm, particularly how many times the perm mask needs to be computed.
5664 // For example, if the perm mask can be hoisted out of a loop or is already
5665 // used (perhaps because there are multiple permutes with the same shuffle
5666 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5667 // the loop requires an extra register.
5668 //
5669 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00005670 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00005671 // available, if this block is within a loop, we should avoid using vperm
5672 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005673 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005674 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005675 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005676
Chris Lattner19e90552006-04-14 05:19:18 +00005677 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5678 // vector that will get spilled to the constant pool.
5679 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005680
Chris Lattner19e90552006-04-14 05:19:18 +00005681 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5682 // that it is in input element units, not in bytes. Convert now.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005683 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005684 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005685
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005686 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005687 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5688 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005689
Chris Lattner19e90552006-04-14 05:19:18 +00005690 for (unsigned j = 0; j != BytesPerElement; ++j)
5691 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson9f944592009-08-11 20:47:22 +00005692 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00005693 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005694
Owen Anderson9f944592009-08-11 20:47:22 +00005695 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga49de9d2009-02-25 22:49:59 +00005696 &ResultMask[0], ResultMask.size());
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005697 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00005698}
5699
Chris Lattner9754d142006-04-18 17:59:36 +00005700/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5701/// altivec comparison. If it is, return true and fill in Opc/isDot with
5702/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005703static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner9754d142006-04-18 17:59:36 +00005704 bool &isDot) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005705 unsigned IntrinsicID =
5706 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00005707 CompareOpc = -1;
5708 isDot = false;
5709 switch (IntrinsicID) {
5710 default: return false;
5711 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00005712 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5713 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5714 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5715 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5716 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5717 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5718 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5719 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5720 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5721 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5722 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5723 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5724 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005725
Chris Lattner4211ca92006-04-14 06:01:58 +00005726 // Normal Comparisons.
5727 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5728 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5729 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5730 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5731 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5732 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5733 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5734 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5735 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5736 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5737 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5738 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5739 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5740 }
Chris Lattner9754d142006-04-18 17:59:36 +00005741 return true;
5742}
5743
5744/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5745/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005746SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005747 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00005748 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5749 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005750 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00005751 int CompareOpc;
5752 bool isDot;
5753 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005754 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005755
Chris Lattner9754d142006-04-18 17:59:36 +00005756 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00005757 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00005758 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00005759 Op.getOperand(1), Op.getOperand(2),
5760 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00005761 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00005762 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005763
Chris Lattner4211ca92006-04-14 06:01:58 +00005764 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005765 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005766 Op.getOperand(2), // LHS
5767 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00005768 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005769 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005770 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00005771 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005772
Chris Lattner4211ca92006-04-14 06:01:58 +00005773 // Now that we have the comparison, emit a copy from the CR to a GPR.
5774 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00005775 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00005776 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00005777 CompNode.getValue(1));
5778
Chris Lattner4211ca92006-04-14 06:01:58 +00005779 // Unpack the result based on how the target uses it.
5780 unsigned BitNo; // Bit # of CR6.
5781 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00005782 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00005783 default: // Can't happen, don't crash on invalid number though.
5784 case 0: // Return the value of the EQ bit of CR6.
5785 BitNo = 0; InvertBit = false;
5786 break;
5787 case 1: // Return the inverted value of the EQ bit of CR6.
5788 BitNo = 0; InvertBit = true;
5789 break;
5790 case 2: // Return the value of the LT bit of CR6.
5791 BitNo = 2; InvertBit = false;
5792 break;
5793 case 3: // Return the inverted value of the LT bit of CR6.
5794 BitNo = 2; InvertBit = true;
5795 break;
5796 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005797
Chris Lattner4211ca92006-04-14 06:01:58 +00005798 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00005799 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5800 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005801 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00005802 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5803 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00005804
Chris Lattner4211ca92006-04-14 06:01:58 +00005805 // If we are supposed to, toggle the bit.
5806 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00005807 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5808 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005809 return Flags;
5810}
5811
Scott Michelcf0da6c2009-02-17 22:15:04 +00005812SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005813 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005814 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00005815 // Create a stack slot that is 16-byte aligned.
5816 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00005817 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00005818 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005819 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005820
Chris Lattner4211ca92006-04-14 06:01:58 +00005821 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00005822 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00005823 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005824 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005825 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00005826 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005827 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005828}
5829
Dan Gohman21cea8a2010-04-17 15:26:15 +00005830SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005831 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005832 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005833 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005834
Owen Anderson9f944592009-08-11 20:47:22 +00005835 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
5836 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005837
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005838 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005839 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005840
Chris Lattner7e4398742006-04-18 03:43:48 +00005841 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00005842 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
5843 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
5844 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005845
Chris Lattner7e4398742006-04-18 03:43:48 +00005846 // Low parts multiplied together, generating 32-bit results (we ignore the
5847 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005848 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00005849 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005850
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005851 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00005852 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00005853 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005854 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005855 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005856 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
5857 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005858 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005859
Owen Anderson9f944592009-08-11 20:47:22 +00005860 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00005861
Chris Lattner96d50482006-04-18 04:28:57 +00005862 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005863 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005864 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005865 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005866
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005867 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005868 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00005869 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005870 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005871
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005872 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005873 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00005874 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005875 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005876
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005877 // Merge the results together.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005878 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005879 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005880 Ops[i*2 ] = 2*i+1;
5881 Ops[i*2+1] = 2*i+1+16;
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005882 }
Owen Anderson9f944592009-08-11 20:47:22 +00005883 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00005884 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005885 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00005886 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005887}
5888
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005889/// LowerOperation - Provide custom lowering hooks for some operations.
5890///
Dan Gohman21cea8a2010-04-17 15:26:15 +00005891SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005892 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005893 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005894 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00005895 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005896 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00005897 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00005898 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005899 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00005900 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
5901 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005902 case ISD::VASTART:
Dan Gohman31ae5862010-04-17 14:41:14 +00005903 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005904
5905 case ISD::VAARG:
Dan Gohman31ae5862010-04-17 14:41:14 +00005906 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00005907
Roman Divackyc3825df2013-07-25 21:36:47 +00005908 case ISD::VACOPY:
5909 return LowerVACOPY(Op, DAG, PPCSubTarget);
5910
Jim Laskeye4f4d042006-12-04 22:04:42 +00005911 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00005912 case ISD::DYNAMIC_STACKALLOC:
5913 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng51096af2008-04-19 01:30:48 +00005914
Hal Finkel756810f2013-03-21 21:37:52 +00005915 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
5916 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
5917
Hal Finkel940ab932014-02-28 00:27:01 +00005918 case ISD::LOAD: return LowerLOAD(Op, DAG);
5919 case ISD::STORE: return LowerSTORE(Op, DAG);
5920 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005921 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005922 case ISD::FP_TO_UINT:
5923 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005924 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00005925 case ISD::UINT_TO_FP:
5926 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00005927 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00005928
Chris Lattner4211ca92006-04-14 06:01:58 +00005929 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00005930 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
5931 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
5932 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00005933
Chris Lattner4211ca92006-04-14 06:01:58 +00005934 // Vector-related lowering.
5935 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5936 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5937 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5938 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005939 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005940
Hal Finkel25c19922013-05-15 21:37:41 +00005941 // For counter-based loop handling.
5942 case ISD::INTRINSIC_W_CHAIN: return SDValue();
5943
Chris Lattnerf6a81562007-12-08 06:59:59 +00005944 // Frame & Return address.
5945 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00005946 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00005947 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005948}
5949
Duncan Sands6ed40142008-12-01 11:39:25 +00005950void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
5951 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005952 SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00005953 const TargetMachine &TM = getTargetMachine();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005954 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00005955 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00005956 default:
Craig Toppere55c5562012-02-07 02:50:20 +00005957 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkel25c19922013-05-15 21:37:41 +00005958 case ISD::INTRINSIC_W_CHAIN: {
5959 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
5960 Intrinsic::ppc_is_decremented_ctr_nonzero)
5961 break;
5962
5963 assert(N->getValueType(0) == MVT::i1 &&
5964 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00005965 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00005966 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
5967 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
5968 N->getOperand(1));
5969
5970 Results.push_back(NewInt);
5971 Results.push_back(NewInt.getValue(1));
5972 break;
5973 }
Roman Divacky4394e682011-06-28 15:30:42 +00005974 case ISD::VAARG: {
5975 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
5976 || TM.getSubtarget<PPCSubtarget>().isPPC64())
5977 return;
5978
5979 EVT VT = N->getValueType(0);
5980
5981 if (VT == MVT::i64) {
5982 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
5983
5984 Results.push_back(NewNode);
5985 Results.push_back(NewNode.getValue(1));
5986 }
5987 return;
5988 }
Duncan Sands6ed40142008-12-01 11:39:25 +00005989 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00005990 assert(N->getValueType(0) == MVT::ppcf128);
5991 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005992 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005993 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00005994 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00005995 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005996 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00005997 DAG.getIntPtrConstant(1));
5998
Ulrich Weigand874fc622013-03-26 10:56:22 +00005999 // Add the two halves of the long double in round-to-zero mode.
6000 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00006001
6002 // We know the low half is about to be thrown away, so just use something
6003 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00006004 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00006005 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00006006 return;
Duncan Sands2a287912008-07-19 16:26:02 +00006007 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006008 case ISD::FP_TO_SINT:
Bill Schmidt41221692013-07-09 18:50:20 +00006009 // LowerFP_TO_INT() can only handle f32 and f64.
6010 if (N->getOperand(0).getValueType() == MVT::ppcf128)
6011 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006012 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00006013 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00006014 }
6015}
6016
6017
Chris Lattner4211ca92006-04-14 06:01:58 +00006018//===----------------------------------------------------------------------===//
6019// Other Lowering Code
6020//===----------------------------------------------------------------------===//
6021
Chris Lattner9b577f12005-08-26 21:23:58 +00006022MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00006023PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman747e55b2009-02-07 16:15:20 +00006024 bool is64bit, unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006025 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesend4eb0522008-08-25 22:34:37 +00006026 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6027
6028 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6029 MachineFunction *F = BB->getParent();
6030 MachineFunction::iterator It = BB;
6031 ++It;
6032
6033 unsigned dest = MI->getOperand(0).getReg();
6034 unsigned ptrA = MI->getOperand(1).getReg();
6035 unsigned ptrB = MI->getOperand(2).getReg();
6036 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006037 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00006038
6039 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6040 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6041 F->insert(It, loopMBB);
6042 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006043 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006044 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006045 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006046
6047 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006048 unsigned TmpReg = (!BinOpcode) ? incr :
6049 RegInfo.createVirtualRegister(
Dale Johannesenbc698292008-09-02 20:30:23 +00006050 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6051 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006052
6053 // thisMBB:
6054 // ...
6055 // fallthrough --> loopMBB
6056 BB->addSuccessor(loopMBB);
6057
6058 // loopMBB:
6059 // l[wd]arx dest, ptr
6060 // add r0, dest, incr
6061 // st[wd]cx. r0, ptr
6062 // bne- loopMBB
6063 // fallthrough --> exitMBB
6064 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006065 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00006066 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006067 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006068 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
6069 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesend4eb0522008-08-25 22:34:37 +00006070 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006071 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006072 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006073 BB->addSuccessor(loopMBB);
6074 BB->addSuccessor(exitMBB);
6075
6076 // exitMBB:
6077 // ...
6078 BB = exitMBB;
6079 return BB;
6080}
6081
6082MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00006083PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00006084 MachineBasicBlock *BB,
6085 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00006086 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006087 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesena32affb2008-08-28 17:53:09 +00006088 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6089 // In 64 bit mode we have to use 64 bits for addresses, even though the
6090 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
6091 // registers without caring whether they're 32 or 64, but here we're
6092 // doing actual arithmetic on the addresses.
6093 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00006094 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00006095
6096 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6097 MachineFunction *F = BB->getParent();
6098 MachineFunction::iterator It = BB;
6099 ++It;
6100
6101 unsigned dest = MI->getOperand(0).getReg();
6102 unsigned ptrA = MI->getOperand(1).getReg();
6103 unsigned ptrB = MI->getOperand(2).getReg();
6104 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006105 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00006106
6107 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6108 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6109 F->insert(It, loopMBB);
6110 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006111 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006112 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006113 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006114
6115 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006116 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006117 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6118 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00006119 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6120 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6121 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6122 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
6123 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6124 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6125 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6126 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6127 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
6128 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006129 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006130 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006131 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006132
6133 // thisMBB:
6134 // ...
6135 // fallthrough --> loopMBB
6136 BB->addSuccessor(loopMBB);
6137
6138 // The 4-byte load must be aligned, while a char or short may be
6139 // anywhere in the word. Hence all this nasty bookkeeping code.
6140 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6141 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006142 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00006143 // rlwinm ptr, ptr1, 0, 0, 29
6144 // slw incr2, incr, shift
6145 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6146 // slw mask, mask2, shift
6147 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00006148 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006149 // add tmp, tmpDest, incr2
6150 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00006151 // and tmp3, tmp, mask
6152 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00006153 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00006154 // bne- loopMBB
6155 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006156 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006157 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00006158 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006159 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006160 .addReg(ptrA).addReg(ptrB);
6161 } else {
6162 Ptr1Reg = ptrB;
6163 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006164 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006165 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006166 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006167 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6168 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006169 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006170 .addReg(Ptr1Reg).addImm(0).addImm(61);
6171 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006172 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006173 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006174 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006175 .addReg(incr).addReg(ShiftReg);
6176 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006177 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00006178 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006179 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6180 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00006181 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006182 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006183 .addReg(Mask2Reg).addReg(ShiftReg);
6184
6185 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006186 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006187 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006188 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006189 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006190 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006191 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006192 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006193 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006194 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006195 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006196 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00006197 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006198 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006199 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006200 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006201 BB->addSuccessor(loopMBB);
6202 BB->addSuccessor(exitMBB);
6203
6204 // exitMBB:
6205 // ...
6206 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006207 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6208 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00006209 return BB;
6210}
6211
Hal Finkel756810f2013-03-21 21:37:52 +00006212llvm::MachineBasicBlock*
6213PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6214 MachineBasicBlock *MBB) const {
6215 DebugLoc DL = MI->getDebugLoc();
6216 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6217
6218 MachineFunction *MF = MBB->getParent();
6219 MachineRegisterInfo &MRI = MF->getRegInfo();
6220
6221 const BasicBlock *BB = MBB->getBasicBlock();
6222 MachineFunction::iterator I = MBB;
6223 ++I;
6224
6225 // Memory Reference
6226 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6227 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6228
6229 unsigned DstReg = MI->getOperand(0).getReg();
6230 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6231 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6232 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6233 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6234
6235 MVT PVT = getPointerTy();
6236 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6237 "Invalid Pointer Size!");
6238 // For v = setjmp(buf), we generate
6239 //
6240 // thisMBB:
6241 // SjLjSetup mainMBB
6242 // bl mainMBB
6243 // v_restore = 1
6244 // b sinkMBB
6245 //
6246 // mainMBB:
6247 // buf[LabelOffset] = LR
6248 // v_main = 0
6249 //
6250 // sinkMBB:
6251 // v = phi(main, restore)
6252 //
6253
6254 MachineBasicBlock *thisMBB = MBB;
6255 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6256 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6257 MF->insert(I, mainMBB);
6258 MF->insert(I, sinkMBB);
6259
6260 MachineInstrBuilder MIB;
6261
6262 // Transfer the remainder of BB and its successor edges to sinkMBB.
6263 sinkMBB->splice(sinkMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006264 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Hal Finkel756810f2013-03-21 21:37:52 +00006265 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6266
6267 // Note that the structure of the jmp_buf used here is not compatible
6268 // with that used by libc, and is not designed to be. Specifically, it
6269 // stores only those 'reserved' registers that LLVM does not otherwise
6270 // understand how to spill. Also, by convention, by the time this
6271 // intrinsic is called, Clang has already stored the frame address in the
6272 // first slot of the buffer and stack address in the third. Following the
6273 // X86 target code, we'll store the jump address in the second slot. We also
6274 // need to save the TOC pointer (R2) to handle jumps between shared
6275 // libraries, and that will be stored in the fourth slot. The thread
6276 // identifier (R13) is not affected.
6277
6278 // thisMBB:
6279 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6280 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006281 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006282
6283 // Prepare IP either in reg.
6284 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6285 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6286 unsigned BufReg = MI->getOperand(1).getReg();
6287
6288 if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) {
6289 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6290 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006291 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006292 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006293 MIB.setMemRefs(MMOBegin, MMOEnd);
6294 }
6295
Hal Finkelf05d6c72013-07-17 23:50:51 +00006296 // Naked functions never have a base pointer, and so we use r1. For all
6297 // other functions, this decision must be delayed until during PEI.
6298 unsigned BaseReg;
6299 if (MF->getFunction()->getAttributes().hasAttribute(
6300 AttributeSet::FunctionIndex, Attribute::Naked))
6301 BaseReg = PPCSubTarget.isPPC64() ? PPC::X1 : PPC::R1;
6302 else
6303 BaseReg = PPCSubTarget.isPPC64() ? PPC::BP8 : PPC::BP;
6304
6305 MIB = BuildMI(*thisMBB, MI, DL,
6306 TII->get(PPCSubTarget.isPPC64() ? PPC::STD : PPC::STW))
6307 .addReg(BaseReg)
6308 .addImm(BPOffset)
6309 .addReg(BufReg);
6310 MIB.setMemRefs(MMOBegin, MMOEnd);
6311
Hal Finkel756810f2013-03-21 21:37:52 +00006312 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00006313 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling5e7656b2013-06-07 07:55:53 +00006314 const PPCRegisterInfo *TRI =
6315 static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo());
6316 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00006317
6318 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6319
6320 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6321 .addMBB(mainMBB);
6322 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6323
6324 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6325 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6326
6327 // mainMBB:
6328 // mainDstReg = 0
6329 MIB = BuildMI(mainMBB, DL,
6330 TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
6331
6332 // Store IP
6333 if (PPCSubTarget.isPPC64()) {
6334 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6335 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006336 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006337 .addReg(BufReg);
6338 } else {
6339 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6340 .addReg(LabelReg)
6341 .addImm(LabelOffset)
6342 .addReg(BufReg);
6343 }
6344
6345 MIB.setMemRefs(MMOBegin, MMOEnd);
6346
6347 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6348 mainMBB->addSuccessor(sinkMBB);
6349
6350 // sinkMBB:
6351 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6352 TII->get(PPC::PHI), DstReg)
6353 .addReg(mainDstReg).addMBB(mainMBB)
6354 .addReg(restoreDstReg).addMBB(thisMBB);
6355
6356 MI->eraseFromParent();
6357 return sinkMBB;
6358}
6359
6360MachineBasicBlock *
6361PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6362 MachineBasicBlock *MBB) const {
6363 DebugLoc DL = MI->getDebugLoc();
6364 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6365
6366 MachineFunction *MF = MBB->getParent();
6367 MachineRegisterInfo &MRI = MF->getRegInfo();
6368
6369 // Memory Reference
6370 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6371 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6372
6373 MVT PVT = getPointerTy();
6374 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6375 "Invalid Pointer Size!");
6376
6377 const TargetRegisterClass *RC =
6378 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6379 unsigned Tmp = MRI.createVirtualRegister(RC);
6380 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6381 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6382 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006383 unsigned BP = (PVT == MVT::i64) ? PPC::X30 : PPC::R30;
Hal Finkel756810f2013-03-21 21:37:52 +00006384
6385 MachineInstrBuilder MIB;
6386
6387 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6388 const int64_t SPOffset = 2 * PVT.getStoreSize();
6389 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006390 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006391
6392 unsigned BufReg = MI->getOperand(0).getReg();
6393
6394 // Reload FP (the jumped-to function may not have had a
6395 // frame pointer, and if so, then its r31 will be restored
6396 // as necessary).
6397 if (PVT == MVT::i64) {
6398 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6399 .addImm(0)
6400 .addReg(BufReg);
6401 } else {
6402 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6403 .addImm(0)
6404 .addReg(BufReg);
6405 }
6406 MIB.setMemRefs(MMOBegin, MMOEnd);
6407
6408 // Reload IP
6409 if (PVT == MVT::i64) {
6410 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006411 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006412 .addReg(BufReg);
6413 } else {
6414 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6415 .addImm(LabelOffset)
6416 .addReg(BufReg);
6417 }
6418 MIB.setMemRefs(MMOBegin, MMOEnd);
6419
6420 // Reload SP
6421 if (PVT == MVT::i64) {
6422 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006423 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006424 .addReg(BufReg);
6425 } else {
6426 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6427 .addImm(SPOffset)
6428 .addReg(BufReg);
6429 }
6430 MIB.setMemRefs(MMOBegin, MMOEnd);
6431
Hal Finkelf05d6c72013-07-17 23:50:51 +00006432 // Reload BP
6433 if (PVT == MVT::i64) {
6434 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
6435 .addImm(BPOffset)
6436 .addReg(BufReg);
6437 } else {
6438 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
6439 .addImm(BPOffset)
6440 .addReg(BufReg);
6441 }
6442 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00006443
6444 // Reload TOC
6445 if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) {
6446 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006447 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006448 .addReg(BufReg);
6449
6450 MIB.setMemRefs(MMOBegin, MMOEnd);
6451 }
6452
6453 // Jump
6454 BuildMI(*MBB, MI, DL,
6455 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6456 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6457
6458 MI->eraseFromParent();
6459 return MBB;
6460}
6461
Dale Johannesena32affb2008-08-28 17:53:09 +00006462MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00006463PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00006464 MachineBasicBlock *BB) const {
Hal Finkel756810f2013-03-21 21:37:52 +00006465 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6466 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6467 return emitEHSjLjSetJmp(MI, BB);
6468 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6469 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6470 return emitEHSjLjLongJmp(MI, BB);
6471 }
6472
Evan Cheng20350c42006-11-27 23:37:22 +00006473 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00006474
6475 // To "insert" these instructions we actually have to insert their
6476 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00006477 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00006478 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00006479 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00006480
Dan Gohman3b460302008-07-07 23:14:23 +00006481 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00006482
Hal Finkel460e94d2012-06-22 23:10:08 +00006483 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006484 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6485 MI->getOpcode() == PPC::SELECT_I4 ||
6486 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00006487 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00006488 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6489 MI->getOpcode() == PPC::SELECT_CC_I8)
6490 Cond.push_back(MI->getOperand(4));
6491 else
6492 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00006493 Cond.push_back(MI->getOperand(1));
6494
Hal Finkel460e94d2012-06-22 23:10:08 +00006495 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00006496 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6497 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
6498 Cond, MI->getOperand(2).getReg(),
6499 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00006500 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6501 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6502 MI->getOpcode() == PPC::SELECT_CC_F4 ||
6503 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006504 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
6505 MI->getOpcode() == PPC::SELECT_I4 ||
6506 MI->getOpcode() == PPC::SELECT_I8 ||
6507 MI->getOpcode() == PPC::SELECT_F4 ||
6508 MI->getOpcode() == PPC::SELECT_F8 ||
6509 MI->getOpcode() == PPC::SELECT_VRRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00006510 // The incoming instruction knows the destination vreg to set, the
6511 // condition code register to branch on, the true/false values to
6512 // select between, and a branch opcode to use.
6513
6514 // thisMBB:
6515 // ...
6516 // TrueVal = ...
6517 // cmpTY ccX, r1, r2
6518 // bCC copy1MBB
6519 // fallthrough --> copy0MBB
6520 MachineBasicBlock *thisMBB = BB;
6521 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6522 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006523 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006524 F->insert(It, copy0MBB);
6525 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006526
6527 // Transfer the remainder of BB and its successor edges to sinkMBB.
6528 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006529 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006530 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6531
Evan Cheng32e376f2008-07-12 02:23:19 +00006532 // Next, add the true and fallthrough blocks as its successors.
6533 BB->addSuccessor(copy0MBB);
6534 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006535
Hal Finkel940ab932014-02-28 00:27:01 +00006536 if (MI->getOpcode() == PPC::SELECT_I4 ||
6537 MI->getOpcode() == PPC::SELECT_I8 ||
6538 MI->getOpcode() == PPC::SELECT_F4 ||
6539 MI->getOpcode() == PPC::SELECT_F8 ||
6540 MI->getOpcode() == PPC::SELECT_VRRC) {
6541 BuildMI(BB, dl, TII->get(PPC::BC))
6542 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6543 } else {
6544 unsigned SelectPred = MI->getOperand(4).getImm();
6545 BuildMI(BB, dl, TII->get(PPC::BCC))
6546 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6547 }
Dan Gohman34396292010-07-06 20:24:04 +00006548
Evan Cheng32e376f2008-07-12 02:23:19 +00006549 // copy0MBB:
6550 // %FalseValue = ...
6551 // # fallthrough to sinkMBB
6552 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006553
Evan Cheng32e376f2008-07-12 02:23:19 +00006554 // Update machine-CFG edges
6555 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006556
Evan Cheng32e376f2008-07-12 02:23:19 +00006557 // sinkMBB:
6558 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6559 // ...
6560 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00006561 BuildMI(*BB, BB->begin(), dl,
6562 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00006563 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
6564 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6565 }
Dale Johannesena32affb2008-08-28 17:53:09 +00006566 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
6567 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
6568 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
6569 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006570 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
6571 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
6572 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
6573 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006574
6575 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
6576 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
6577 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
6578 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006579 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
6580 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
6581 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
6582 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006583
6584 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
6585 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
6586 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
6587 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006588 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
6589 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
6590 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
6591 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006592
6593 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
6594 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
6595 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
6596 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006597 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
6598 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
6599 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
6600 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006601
6602 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006603 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006604 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006605 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006606 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006607 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006608 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006609 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006610
6611 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6612 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6613 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6614 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006615 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6616 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6617 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6618 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006619
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006620 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6621 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6622 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6623 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6624 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6625 BB = EmitAtomicBinary(MI, BB, false, 0);
6626 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6627 BB = EmitAtomicBinary(MI, BB, true, 0);
6628
Evan Cheng32e376f2008-07-12 02:23:19 +00006629 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6630 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6631 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6632
6633 unsigned dest = MI->getOperand(0).getReg();
6634 unsigned ptrA = MI->getOperand(1).getReg();
6635 unsigned ptrB = MI->getOperand(2).getReg();
6636 unsigned oldval = MI->getOperand(3).getReg();
6637 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006638 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006639
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006640 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6641 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6642 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006643 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006644 F->insert(It, loop1MBB);
6645 F->insert(It, loop2MBB);
6646 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006647 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006648 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006649 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006650 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006651
6652 // thisMBB:
6653 // ...
6654 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006655 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006656
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006657 // loop1MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006658 // l[wd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006659 // cmp[wd] dest, oldval
6660 // bne- midMBB
6661 // loop2MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006662 // st[wd]cx. newval, ptr
6663 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006664 // b exitBB
6665 // midMBB:
6666 // st[wd]cx. dest, ptr
6667 // exitBB:
6668 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006669 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00006670 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006671 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00006672 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006673 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006674 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6675 BB->addSuccessor(loop2MBB);
6676 BB->addSuccessor(midMBB);
6677
6678 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006679 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng32e376f2008-07-12 02:23:19 +00006680 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006681 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006682 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006683 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006684 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006685 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006686
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006687 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006688 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006689 .addReg(dest).addReg(ptrA).addReg(ptrB);
6690 BB->addSuccessor(exitMBB);
6691
Evan Cheng32e376f2008-07-12 02:23:19 +00006692 // exitMBB:
6693 // ...
6694 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00006695 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6696 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6697 // We must use 64-bit registers for addresses when targeting 64-bit,
6698 // since we're actually doing arithmetic on them. Other registers
6699 // can be 32-bit.
6700 bool is64bit = PPCSubTarget.isPPC64();
6701 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6702
6703 unsigned dest = MI->getOperand(0).getReg();
6704 unsigned ptrA = MI->getOperand(1).getReg();
6705 unsigned ptrB = MI->getOperand(2).getReg();
6706 unsigned oldval = MI->getOperand(3).getReg();
6707 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006708 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00006709
6710 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6711 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6712 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6713 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6714 F->insert(It, loop1MBB);
6715 F->insert(It, loop2MBB);
6716 F->insert(It, midMBB);
6717 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006718 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006719 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006720 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006721
6722 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006723 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006724 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6725 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00006726 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6727 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6728 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6729 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6730 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6731 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6732 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6733 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6734 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6735 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6736 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6737 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6738 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6739 unsigned Ptr1Reg;
6740 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00006741 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00006742 // thisMBB:
6743 // ...
6744 // fallthrough --> loopMBB
6745 BB->addSuccessor(loop1MBB);
6746
6747 // The 4-byte load must be aligned, while a char or short may be
6748 // anywhere in the word. Hence all this nasty bookkeeping code.
6749 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6750 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006751 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00006752 // rlwinm ptr, ptr1, 0, 0, 29
6753 // slw newval2, newval, shift
6754 // slw oldval2, oldval,shift
6755 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6756 // slw mask, mask2, shift
6757 // and newval3, newval2, mask
6758 // and oldval3, oldval2, mask
6759 // loop1MBB:
6760 // lwarx tmpDest, ptr
6761 // and tmp, tmpDest, mask
6762 // cmpw tmp, oldval3
6763 // bne- midMBB
6764 // loop2MBB:
6765 // andc tmp2, tmpDest, mask
6766 // or tmp4, tmp2, newval3
6767 // stwcx. tmp4, ptr
6768 // bne- loop1MBB
6769 // b exitBB
6770 // midMBB:
6771 // stwcx. tmpDest, ptr
6772 // exitBB:
6773 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006774 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00006775 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006776 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006777 .addReg(ptrA).addReg(ptrB);
6778 } else {
6779 Ptr1Reg = ptrB;
6780 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006781 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006782 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006783 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006784 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6785 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006786 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006787 .addReg(Ptr1Reg).addImm(0).addImm(61);
6788 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006789 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006790 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006791 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006792 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006793 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006794 .addReg(oldval).addReg(ShiftReg);
6795 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006796 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00006797 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006798 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6799 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6800 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00006801 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006802 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006803 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006804 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006805 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006806 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006807 .addReg(OldVal2Reg).addReg(MaskReg);
6808
6809 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006810 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006811 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006812 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6813 .addReg(TmpDestReg).addReg(MaskReg);
6814 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00006815 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006816 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006817 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6818 BB->addSuccessor(loop2MBB);
6819 BB->addSuccessor(midMBB);
6820
6821 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006822 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6823 .addReg(TmpDestReg).addReg(MaskReg);
6824 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6825 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6826 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006827 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006828 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006829 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006830 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006831 BB->addSuccessor(loop1MBB);
6832 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006833
Dale Johannesen340d2642008-08-30 00:08:53 +00006834 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006835 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006836 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00006837 BB->addSuccessor(exitMBB);
6838
6839 // exitMBB:
6840 // ...
6841 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006842 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
6843 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00006844 } else if (MI->getOpcode() == PPC::FADDrtz) {
6845 // This pseudo performs an FADD with rounding mode temporarily forced
6846 // to round-to-zero. We emit this via custom inserter since the FPSCR
6847 // is not modeled at the SelectionDAG level.
6848 unsigned Dest = MI->getOperand(0).getReg();
6849 unsigned Src1 = MI->getOperand(1).getReg();
6850 unsigned Src2 = MI->getOperand(2).getReg();
6851 DebugLoc dl = MI->getDebugLoc();
6852
6853 MachineRegisterInfo &RegInfo = F->getRegInfo();
6854 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
6855
6856 // Save FPSCR value.
6857 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
6858
6859 // Set rounding mode to round-to-zero.
6860 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
6861 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
6862
6863 // Perform addition.
6864 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
6865
6866 // Restore FPSCR value.
6867 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00006868 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
6869 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
6870 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
6871 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
6872 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
6873 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
6874 PPC::ANDIo8 : PPC::ANDIo;
6875 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
6876 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
6877
6878 MachineRegisterInfo &RegInfo = F->getRegInfo();
6879 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
6880 &PPC::GPRCRegClass :
6881 &PPC::G8RCRegClass);
6882
6883 DebugLoc dl = MI->getDebugLoc();
6884 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
6885 .addReg(MI->getOperand(1).getReg()).addImm(1);
6886 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
6887 MI->getOperand(0).getReg())
6888 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Dale Johannesen340d2642008-08-30 00:08:53 +00006889 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006890 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00006891 }
Chris Lattner9b577f12005-08-26 21:23:58 +00006892
Dan Gohman34396292010-07-06 20:24:04 +00006893 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00006894 return BB;
6895}
6896
Chris Lattner4211ca92006-04-14 06:01:58 +00006897//===----------------------------------------------------------------------===//
6898// Target Optimization Hooks
6899//===----------------------------------------------------------------------===//
6900
Hal Finkelb0c810f2013-04-03 17:44:56 +00006901SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
6902 DAGCombinerInfo &DCI) const {
Hal Finkel2e103312013-04-03 04:01:11 +00006903 if (DCI.isAfterLegalizeVectorOps())
6904 return SDValue();
6905
Hal Finkelb0c810f2013-04-03 17:44:56 +00006906 EVT VT = Op.getValueType();
6907
6908 if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) ||
6909 (VT == MVT::f64 && PPCSubTarget.hasFRE()) ||
6910 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel2e103312013-04-03 04:01:11 +00006911
6912 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6913 // For the reciprocal, we need to find the zero of the function:
6914 // F(X) = A X - 1 [which has a zero at X = 1/A]
6915 // =>
6916 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
6917 // does not require additional intermediate precision]
6918
6919 // Convergence is quadratic, so we essentially double the number of digits
6920 // correct after every iteration. The minimum architected relative
6921 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6922 // 23 digits and double has 52 digits.
6923 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00006924 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00006925 ++Iterations;
6926
6927 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006928 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006929
6930 SDValue FPOne =
Hal Finkelb0c810f2013-04-03 17:44:56 +00006931 DAG.getConstantFP(1.0, VT.getScalarType());
6932 if (VT.isVector()) {
6933 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00006934 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00006935 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel2e103312013-04-03 04:01:11 +00006936 FPOne, FPOne, FPOne, FPOne);
6937 }
6938
Hal Finkelb0c810f2013-04-03 17:44:56 +00006939 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006940 DCI.AddToWorklist(Est.getNode());
6941
6942 // Newton iterations: Est = Est + Est (1 - Arg * Est)
6943 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00006944 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00006945 DCI.AddToWorklist(NewEst.getNode());
6946
Hal Finkelb0c810f2013-04-03 17:44:56 +00006947 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006948 DCI.AddToWorklist(NewEst.getNode());
6949
Hal Finkelb0c810f2013-04-03 17:44:56 +00006950 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006951 DCI.AddToWorklist(NewEst.getNode());
6952
Hal Finkelb0c810f2013-04-03 17:44:56 +00006953 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006954 DCI.AddToWorklist(Est.getNode());
6955 }
6956
6957 return Est;
6958 }
6959
6960 return SDValue();
6961}
6962
Hal Finkelb0c810f2013-04-03 17:44:56 +00006963SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel2e103312013-04-03 04:01:11 +00006964 DAGCombinerInfo &DCI) const {
6965 if (DCI.isAfterLegalizeVectorOps())
6966 return SDValue();
6967
Hal Finkelb0c810f2013-04-03 17:44:56 +00006968 EVT VT = Op.getValueType();
6969
6970 if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) ||
6971 (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) ||
6972 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel2e103312013-04-03 04:01:11 +00006973
6974 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6975 // For the reciprocal sqrt, we need to find the zero of the function:
6976 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
6977 // =>
6978 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
6979 // As a result, we precompute A/2 prior to the iteration loop.
6980
6981 // Convergence is quadratic, so we essentially double the number of digits
6982 // correct after every iteration. The minimum architected relative
6983 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6984 // 23 digits and double has 52 digits.
6985 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00006986 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00006987 ++Iterations;
6988
6989 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006990 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006991
Hal Finkelb0c810f2013-04-03 17:44:56 +00006992 SDValue FPThreeHalves =
6993 DAG.getConstantFP(1.5, VT.getScalarType());
6994 if (VT.isVector()) {
6995 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00006996 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00006997 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
6998 FPThreeHalves, FPThreeHalves,
6999 FPThreeHalves, FPThreeHalves);
Hal Finkel2e103312013-04-03 04:01:11 +00007000 }
7001
Hal Finkelb0c810f2013-04-03 17:44:56 +00007002 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007003 DCI.AddToWorklist(Est.getNode());
7004
7005 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
7006 // this entire sequence requires only one FP constant.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007007 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007008 DCI.AddToWorklist(HalfArg.getNode());
7009
Hal Finkelb0c810f2013-04-03 17:44:56 +00007010 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007011 DCI.AddToWorklist(HalfArg.getNode());
7012
7013 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
7014 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007015 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00007016 DCI.AddToWorklist(NewEst.getNode());
7017
Hal Finkelb0c810f2013-04-03 17:44:56 +00007018 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007019 DCI.AddToWorklist(NewEst.getNode());
7020
Hal Finkelb0c810f2013-04-03 17:44:56 +00007021 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007022 DCI.AddToWorklist(NewEst.getNode());
7023
Hal Finkelb0c810f2013-04-03 17:44:56 +00007024 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007025 DCI.AddToWorklist(Est.getNode());
7026 }
7027
7028 return Est;
7029 }
7030
7031 return SDValue();
7032}
7033
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007034// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
7035// not enforce equality of the chain operands.
7036static bool isConsecutiveLS(LSBaseSDNode *LS, LSBaseSDNode *Base,
7037 unsigned Bytes, int Dist,
7038 SelectionDAG &DAG) {
7039 EVT VT = LS->getMemoryVT();
7040 if (VT.getSizeInBits() / 8 != Bytes)
7041 return false;
7042
7043 SDValue Loc = LS->getBasePtr();
7044 SDValue BaseLoc = Base->getBasePtr();
7045 if (Loc.getOpcode() == ISD::FrameIndex) {
7046 if (BaseLoc.getOpcode() != ISD::FrameIndex)
7047 return false;
7048 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7049 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
7050 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
7051 int FS = MFI->getObjectSize(FI);
7052 int BFS = MFI->getObjectSize(BFI);
7053 if (FS != BFS || FS != (int)Bytes) return false;
7054 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
7055 }
7056
7057 // Handle X+C
7058 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
7059 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
7060 return true;
7061
7062 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7063 const GlobalValue *GV1 = NULL;
7064 const GlobalValue *GV2 = NULL;
7065 int64_t Offset1 = 0;
7066 int64_t Offset2 = 0;
7067 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
7068 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
7069 if (isGA1 && isGA2 && GV1 == GV2)
7070 return Offset1 == (Offset2 + Dist*Bytes);
7071 return false;
7072}
7073
Hal Finkel7d8a6912013-05-26 18:08:30 +00007074// Return true is there is a nearyby consecutive load to the one provided
7075// (regardless of alignment). We search up and down the chain, looking though
7076// token factors and other loads (but nothing else). As a result, a true
7077// results indicates that it is safe to create a new consecutive load adjacent
7078// to the load provided.
7079static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
7080 SDValue Chain = LD->getChain();
7081 EVT VT = LD->getMemoryVT();
7082
7083 SmallSet<SDNode *, 16> LoadRoots;
7084 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
7085 SmallSet<SDNode *, 16> Visited;
7086
7087 // First, search up the chain, branching to follow all token-factor operands.
7088 // If we find a consecutive load, then we're done, otherwise, record all
7089 // nodes just above the top-level loads and token factors.
7090 while (!Queue.empty()) {
7091 SDNode *ChainNext = Queue.pop_back_val();
7092 if (!Visited.insert(ChainNext))
7093 continue;
7094
7095 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007096 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007097 return true;
7098
7099 if (!Visited.count(ChainLD->getChain().getNode()))
7100 Queue.push_back(ChainLD->getChain().getNode());
7101 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
7102 for (SDNode::op_iterator O = ChainNext->op_begin(),
7103 OE = ChainNext->op_end(); O != OE; ++O)
7104 if (!Visited.count(O->getNode()))
7105 Queue.push_back(O->getNode());
7106 } else
7107 LoadRoots.insert(ChainNext);
7108 }
7109
7110 // Second, search down the chain, starting from the top-level nodes recorded
7111 // in the first phase. These top-level nodes are the nodes just above all
7112 // loads and token factors. Starting with their uses, recursively look though
7113 // all loads (just the chain uses) and token factors to find a consecutive
7114 // load.
7115 Visited.clear();
7116 Queue.clear();
7117
7118 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
7119 IE = LoadRoots.end(); I != IE; ++I) {
7120 Queue.push_back(*I);
7121
7122 while (!Queue.empty()) {
7123 SDNode *LoadRoot = Queue.pop_back_val();
7124 if (!Visited.insert(LoadRoot))
7125 continue;
7126
7127 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007128 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007129 return true;
7130
7131 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
7132 UE = LoadRoot->use_end(); UI != UE; ++UI)
7133 if (((isa<LoadSDNode>(*UI) &&
7134 cast<LoadSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
7135 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
7136 Queue.push_back(*UI);
7137 }
7138 }
7139
7140 return false;
7141}
7142
Hal Finkel940ab932014-02-28 00:27:01 +00007143SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
7144 DAGCombinerInfo &DCI) const {
7145 SelectionDAG &DAG = DCI.DAG;
7146 SDLoc dl(N);
7147
7148 assert(PPCSubTarget.useCRBits() &&
7149 "Expecting to be tracking CR bits");
7150 // If we're tracking CR bits, we need to be careful that we don't have:
7151 // trunc(binary-ops(zext(x), zext(y)))
7152 // or
7153 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
7154 // such that we're unnecessarily moving things into GPRs when it would be
7155 // better to keep them in CR bits.
7156
7157 // Note that trunc here can be an actual i1 trunc, or can be the effective
7158 // truncation that comes from a setcc or select_cc.
7159 if (N->getOpcode() == ISD::TRUNCATE &&
7160 N->getValueType(0) != MVT::i1)
7161 return SDValue();
7162
7163 if (N->getOperand(0).getValueType() != MVT::i32 &&
7164 N->getOperand(0).getValueType() != MVT::i64)
7165 return SDValue();
7166
7167 if (N->getOpcode() == ISD::SETCC ||
7168 N->getOpcode() == ISD::SELECT_CC) {
7169 // If we're looking at a comparison, then we need to make sure that the
7170 // high bits (all except for the first) don't matter the result.
7171 ISD::CondCode CC =
7172 cast<CondCodeSDNode>(N->getOperand(
7173 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
7174 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
7175
7176 if (ISD::isSignedIntSetCC(CC)) {
7177 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
7178 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
7179 return SDValue();
7180 } else if (ISD::isUnsignedIntSetCC(CC)) {
7181 if (!DAG.MaskedValueIsZero(N->getOperand(0),
7182 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
7183 !DAG.MaskedValueIsZero(N->getOperand(1),
7184 APInt::getHighBitsSet(OpBits, OpBits-1)))
7185 return SDValue();
7186 } else {
7187 // This is neither a signed nor an unsigned comparison, just make sure
7188 // that the high bits are equal.
7189 APInt Op1Zero, Op1One;
7190 APInt Op2Zero, Op2One;
7191 DAG.ComputeMaskedBits(N->getOperand(0), Op1Zero, Op1One);
7192 DAG.ComputeMaskedBits(N->getOperand(1), Op2Zero, Op2One);
7193
7194 // We don't really care about what is known about the first bit (if
7195 // anything), so clear it in all masks prior to comparing them.
7196 Op1Zero.clearBit(0); Op1One.clearBit(0);
7197 Op2Zero.clearBit(0); Op2One.clearBit(0);
7198
7199 if (Op1Zero != Op2Zero || Op1One != Op2One)
7200 return SDValue();
7201 }
7202 }
7203
7204 // We now know that the higher-order bits are irrelevant, we just need to
7205 // make sure that all of the intermediate operations are bit operations, and
7206 // all inputs are extensions.
7207 if (N->getOperand(0).getOpcode() != ISD::AND &&
7208 N->getOperand(0).getOpcode() != ISD::OR &&
7209 N->getOperand(0).getOpcode() != ISD::XOR &&
7210 N->getOperand(0).getOpcode() != ISD::SELECT &&
7211 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
7212 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
7213 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
7214 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
7215 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
7216 return SDValue();
7217
7218 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
7219 N->getOperand(1).getOpcode() != ISD::AND &&
7220 N->getOperand(1).getOpcode() != ISD::OR &&
7221 N->getOperand(1).getOpcode() != ISD::XOR &&
7222 N->getOperand(1).getOpcode() != ISD::SELECT &&
7223 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
7224 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
7225 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
7226 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
7227 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
7228 return SDValue();
7229
7230 SmallVector<SDValue, 4> Inputs;
7231 SmallVector<SDValue, 8> BinOps, PromOps;
7232 SmallPtrSet<SDNode *, 16> Visited;
7233
7234 for (unsigned i = 0; i < 2; ++i) {
7235 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7236 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7237 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7238 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7239 isa<ConstantSDNode>(N->getOperand(i)))
7240 Inputs.push_back(N->getOperand(i));
7241 else
7242 BinOps.push_back(N->getOperand(i));
7243
7244 if (N->getOpcode() == ISD::TRUNCATE)
7245 break;
7246 }
7247
7248 // Visit all inputs, collect all binary operations (and, or, xor and
7249 // select) that are all fed by extensions.
7250 while (!BinOps.empty()) {
7251 SDValue BinOp = BinOps.back();
7252 BinOps.pop_back();
7253
7254 if (!Visited.insert(BinOp.getNode()))
7255 continue;
7256
7257 PromOps.push_back(BinOp);
7258
7259 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7260 // The condition of the select is not promoted.
7261 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7262 continue;
7263 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7264 continue;
7265
7266 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7267 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7268 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7269 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7270 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7271 Inputs.push_back(BinOp.getOperand(i));
7272 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7273 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7274 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7275 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7276 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
7277 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7278 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7279 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7280 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
7281 BinOps.push_back(BinOp.getOperand(i));
7282 } else {
7283 // We have an input that is not an extension or another binary
7284 // operation; we'll abort this transformation.
7285 return SDValue();
7286 }
7287 }
7288 }
7289
7290 // Make sure that this is a self-contained cluster of operations (which
7291 // is not quite the same thing as saying that everything has only one
7292 // use).
7293 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7294 if (isa<ConstantSDNode>(Inputs[i]))
7295 continue;
7296
7297 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7298 UE = Inputs[i].getNode()->use_end();
7299 UI != UE; ++UI) {
7300 SDNode *User = *UI;
7301 if (User != N && !Visited.count(User))
7302 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007303
7304 // Make sure that we're not going to promote the non-output-value
7305 // operand(s) or SELECT or SELECT_CC.
7306 // FIXME: Although we could sometimes handle this, and it does occur in
7307 // practice that one of the condition inputs to the select is also one of
7308 // the outputs, we currently can't deal with this.
7309 if (User->getOpcode() == ISD::SELECT) {
7310 if (User->getOperand(0) == Inputs[i])
7311 return SDValue();
7312 } else if (User->getOpcode() == ISD::SELECT_CC) {
7313 if (User->getOperand(0) == Inputs[i] ||
7314 User->getOperand(1) == Inputs[i])
7315 return SDValue();
7316 }
Hal Finkel940ab932014-02-28 00:27:01 +00007317 }
7318 }
7319
7320 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7321 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7322 UE = PromOps[i].getNode()->use_end();
7323 UI != UE; ++UI) {
7324 SDNode *User = *UI;
7325 if (User != N && !Visited.count(User))
7326 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007327
7328 // Make sure that we're not going to promote the non-output-value
7329 // operand(s) or SELECT or SELECT_CC.
7330 // FIXME: Although we could sometimes handle this, and it does occur in
7331 // practice that one of the condition inputs to the select is also one of
7332 // the outputs, we currently can't deal with this.
7333 if (User->getOpcode() == ISD::SELECT) {
7334 if (User->getOperand(0) == PromOps[i])
7335 return SDValue();
7336 } else if (User->getOpcode() == ISD::SELECT_CC) {
7337 if (User->getOperand(0) == PromOps[i] ||
7338 User->getOperand(1) == PromOps[i])
7339 return SDValue();
7340 }
Hal Finkel940ab932014-02-28 00:27:01 +00007341 }
7342 }
7343
7344 // Replace all inputs with the extension operand.
7345 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7346 // Constants may have users outside the cluster of to-be-promoted nodes,
7347 // and so we need to replace those as we do the promotions.
7348 if (isa<ConstantSDNode>(Inputs[i]))
7349 continue;
7350 else
7351 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
7352 }
7353
7354 // Replace all operations (these are all the same, but have a different
7355 // (i1) return type). DAG.getNode will validate that the types of
7356 // a binary operator match, so go through the list in reverse so that
7357 // we've likely promoted both operands first. Any intermediate truncations or
7358 // extensions disappear.
7359 while (!PromOps.empty()) {
7360 SDValue PromOp = PromOps.back();
7361 PromOps.pop_back();
7362
7363 if (PromOp.getOpcode() == ISD::TRUNCATE ||
7364 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
7365 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
7366 PromOp.getOpcode() == ISD::ANY_EXTEND) {
7367 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
7368 PromOp.getOperand(0).getValueType() != MVT::i1) {
7369 // The operand is not yet ready (see comment below).
7370 PromOps.insert(PromOps.begin(), PromOp);
7371 continue;
7372 }
7373
7374 SDValue RepValue = PromOp.getOperand(0);
7375 if (isa<ConstantSDNode>(RepValue))
7376 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
7377
7378 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
7379 continue;
7380 }
7381
7382 unsigned C;
7383 switch (PromOp.getOpcode()) {
7384 default: C = 0; break;
7385 case ISD::SELECT: C = 1; break;
7386 case ISD::SELECT_CC: C = 2; break;
7387 }
7388
7389 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7390 PromOp.getOperand(C).getValueType() != MVT::i1) ||
7391 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7392 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
7393 // The to-be-promoted operands of this node have not yet been
7394 // promoted (this should be rare because we're going through the
7395 // list backward, but if one of the operands has several users in
7396 // this cluster of to-be-promoted nodes, it is possible).
7397 PromOps.insert(PromOps.begin(), PromOp);
7398 continue;
7399 }
7400
7401 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7402 PromOp.getNode()->op_end());
7403
7404 // If there are any constant inputs, make sure they're replaced now.
7405 for (unsigned i = 0; i < 2; ++i)
7406 if (isa<ConstantSDNode>(Ops[C+i]))
7407 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
7408
7409 DAG.ReplaceAllUsesOfValueWith(PromOp,
7410 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1,
7411 Ops.data(), Ops.size()));
7412 }
7413
7414 // Now we're left with the initial truncation itself.
7415 if (N->getOpcode() == ISD::TRUNCATE)
7416 return N->getOperand(0);
7417
7418 // Otherwise, this is a comparison. The operands to be compared have just
7419 // changed type (to i1), but everything else is the same.
7420 return SDValue(N, 0);
7421}
7422
7423SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
7424 DAGCombinerInfo &DCI) const {
7425 SelectionDAG &DAG = DCI.DAG;
7426 SDLoc dl(N);
7427
Hal Finkel940ab932014-02-28 00:27:01 +00007428 // If we're tracking CR bits, we need to be careful that we don't have:
7429 // zext(binary-ops(trunc(x), trunc(y)))
7430 // or
7431 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
7432 // such that we're unnecessarily moving things into CR bits that can more
7433 // efficiently stay in GPRs. Note that if we're not certain that the high
7434 // bits are set as required by the final extension, we still may need to do
7435 // some masking to get the proper behavior.
7436
Hal Finkel46043ed2014-03-01 21:36:57 +00007437 // This same functionality is important on PPC64 when dealing with
7438 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
7439 // the return values of functions. Because it is so similar, it is handled
7440 // here as well.
7441
Hal Finkel940ab932014-02-28 00:27:01 +00007442 if (N->getValueType(0) != MVT::i32 &&
7443 N->getValueType(0) != MVT::i64)
7444 return SDValue();
7445
Hal Finkel46043ed2014-03-01 21:36:57 +00007446 if (!((N->getOperand(0).getValueType() == MVT::i1 &&
7447 PPCSubTarget.useCRBits()) ||
7448 (N->getOperand(0).getValueType() == MVT::i32 &&
7449 PPCSubTarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +00007450 return SDValue();
7451
7452 if (N->getOperand(0).getOpcode() != ISD::AND &&
7453 N->getOperand(0).getOpcode() != ISD::OR &&
7454 N->getOperand(0).getOpcode() != ISD::XOR &&
7455 N->getOperand(0).getOpcode() != ISD::SELECT &&
7456 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
7457 return SDValue();
7458
7459 SmallVector<SDValue, 4> Inputs;
7460 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
7461 SmallPtrSet<SDNode *, 16> Visited;
7462
7463 // Visit all inputs, collect all binary operations (and, or, xor and
7464 // select) that are all fed by truncations.
7465 while (!BinOps.empty()) {
7466 SDValue BinOp = BinOps.back();
7467 BinOps.pop_back();
7468
7469 if (!Visited.insert(BinOp.getNode()))
7470 continue;
7471
7472 PromOps.push_back(BinOp);
7473
7474 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7475 // The condition of the select is not promoted.
7476 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7477 continue;
7478 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7479 continue;
7480
7481 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7482 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7483 Inputs.push_back(BinOp.getOperand(i));
7484 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7485 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7486 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7487 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7488 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
7489 BinOps.push_back(BinOp.getOperand(i));
7490 } else {
7491 // We have an input that is not a truncation or another binary
7492 // operation; we'll abort this transformation.
7493 return SDValue();
7494 }
7495 }
7496 }
7497
7498 // Make sure that this is a self-contained cluster of operations (which
7499 // is not quite the same thing as saying that everything has only one
7500 // use).
7501 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7502 if (isa<ConstantSDNode>(Inputs[i]))
7503 continue;
7504
7505 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7506 UE = Inputs[i].getNode()->use_end();
7507 UI != UE; ++UI) {
7508 SDNode *User = *UI;
7509 if (User != N && !Visited.count(User))
7510 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007511
7512 // Make sure that we're not going to promote the non-output-value
7513 // operand(s) or SELECT or SELECT_CC.
7514 // FIXME: Although we could sometimes handle this, and it does occur in
7515 // practice that one of the condition inputs to the select is also one of
7516 // the outputs, we currently can't deal with this.
7517 if (User->getOpcode() == ISD::SELECT) {
7518 if (User->getOperand(0) == Inputs[i])
7519 return SDValue();
7520 } else if (User->getOpcode() == ISD::SELECT_CC) {
7521 if (User->getOperand(0) == Inputs[i] ||
7522 User->getOperand(1) == Inputs[i])
7523 return SDValue();
7524 }
Hal Finkel940ab932014-02-28 00:27:01 +00007525 }
7526 }
7527
7528 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7529 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7530 UE = PromOps[i].getNode()->use_end();
7531 UI != UE; ++UI) {
7532 SDNode *User = *UI;
7533 if (User != N && !Visited.count(User))
7534 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007535
7536 // Make sure that we're not going to promote the non-output-value
7537 // operand(s) or SELECT or SELECT_CC.
7538 // FIXME: Although we could sometimes handle this, and it does occur in
7539 // practice that one of the condition inputs to the select is also one of
7540 // the outputs, we currently can't deal with this.
7541 if (User->getOpcode() == ISD::SELECT) {
7542 if (User->getOperand(0) == PromOps[i])
7543 return SDValue();
7544 } else if (User->getOpcode() == ISD::SELECT_CC) {
7545 if (User->getOperand(0) == PromOps[i] ||
7546 User->getOperand(1) == PromOps[i])
7547 return SDValue();
7548 }
Hal Finkel940ab932014-02-28 00:27:01 +00007549 }
7550 }
7551
Hal Finkel46043ed2014-03-01 21:36:57 +00007552 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +00007553 bool ReallyNeedsExt = false;
7554 if (N->getOpcode() != ISD::ANY_EXTEND) {
7555 // If all of the inputs are not already sign/zero extended, then
7556 // we'll still need to do that at the end.
7557 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7558 if (isa<ConstantSDNode>(Inputs[i]))
7559 continue;
7560
7561 unsigned OpBits =
7562 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +00007563 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
7564
Hal Finkel940ab932014-02-28 00:27:01 +00007565 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
7566 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00007567 APInt::getHighBitsSet(OpBits,
7568 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +00007569 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +00007570 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
7571 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +00007572 ReallyNeedsExt = true;
7573 break;
7574 }
7575 }
7576 }
7577
7578 // Replace all inputs, either with the truncation operand, or a
7579 // truncation or extension to the final output type.
7580 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7581 // Constant inputs need to be replaced with the to-be-promoted nodes that
7582 // use them because they might have users outside of the cluster of
7583 // promoted nodes.
7584 if (isa<ConstantSDNode>(Inputs[i]))
7585 continue;
7586
7587 SDValue InSrc = Inputs[i].getOperand(0);
7588 if (Inputs[i].getValueType() == N->getValueType(0))
7589 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
7590 else if (N->getOpcode() == ISD::SIGN_EXTEND)
7591 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7592 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
7593 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7594 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7595 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
7596 else
7597 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7598 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
7599 }
7600
7601 // Replace all operations (these are all the same, but have a different
7602 // (promoted) return type). DAG.getNode will validate that the types of
7603 // a binary operator match, so go through the list in reverse so that
7604 // we've likely promoted both operands first.
7605 while (!PromOps.empty()) {
7606 SDValue PromOp = PromOps.back();
7607 PromOps.pop_back();
7608
7609 unsigned C;
7610 switch (PromOp.getOpcode()) {
7611 default: C = 0; break;
7612 case ISD::SELECT: C = 1; break;
7613 case ISD::SELECT_CC: C = 2; break;
7614 }
7615
7616 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7617 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
7618 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7619 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
7620 // The to-be-promoted operands of this node have not yet been
7621 // promoted (this should be rare because we're going through the
7622 // list backward, but if one of the operands has several users in
7623 // this cluster of to-be-promoted nodes, it is possible).
7624 PromOps.insert(PromOps.begin(), PromOp);
7625 continue;
7626 }
7627
7628 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7629 PromOp.getNode()->op_end());
7630
7631 // If this node has constant inputs, then they'll need to be promoted here.
7632 for (unsigned i = 0; i < 2; ++i) {
7633 if (!isa<ConstantSDNode>(Ops[C+i]))
7634 continue;
7635 if (Ops[C+i].getValueType() == N->getValueType(0))
7636 continue;
7637
7638 if (N->getOpcode() == ISD::SIGN_EXTEND)
7639 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7640 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7641 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7642 else
7643 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7644 }
7645
7646 DAG.ReplaceAllUsesOfValueWith(PromOp,
7647 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0),
7648 Ops.data(), Ops.size()));
7649 }
7650
7651 // Now we're left with the initial extension itself.
7652 if (!ReallyNeedsExt)
7653 return N->getOperand(0);
7654
Hal Finkel46043ed2014-03-01 21:36:57 +00007655 // To zero extend, just mask off everything except for the first bit (in the
7656 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +00007657 if (N->getOpcode() == ISD::ZERO_EXTEND)
7658 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00007659 DAG.getConstant(APInt::getLowBitsSet(
7660 N->getValueSizeInBits(0), PromBits),
7661 N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +00007662
7663 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
7664 "Invalid extension type");
7665 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0));
7666 SDValue ShiftCst =
Hal Finkel46043ed2014-03-01 21:36:57 +00007667 DAG.getConstant(N->getValueSizeInBits(0)-PromBits, ShiftAmountTy);
Hal Finkel940ab932014-02-28 00:27:01 +00007668 return DAG.getNode(ISD::SRA, dl, N->getValueType(0),
7669 DAG.getNode(ISD::SHL, dl, N->getValueType(0),
7670 N->getOperand(0), ShiftCst), ShiftCst);
7671}
7672
Duncan Sandsdc2dac12008-11-24 14:53:14 +00007673SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
7674 DAGCombinerInfo &DCI) const {
Dan Gohman57c732b2010-04-21 01:34:56 +00007675 const TargetMachine &TM = getTargetMachine();
Chris Lattnerf4184352006-03-01 04:57:39 +00007676 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007677 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00007678 switch (N->getOpcode()) {
7679 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00007680 case PPCISD::SHL:
7681 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007682 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007683 return N->getOperand(0);
7684 }
7685 break;
7686 case PPCISD::SRL:
7687 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007688 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007689 return N->getOperand(0);
7690 }
7691 break;
7692 case PPCISD::SRA:
7693 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007694 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007695 C->isAllOnesValue()) // -1 >>s V -> -1.
7696 return N->getOperand(0);
7697 }
7698 break;
Hal Finkel940ab932014-02-28 00:27:01 +00007699 case ISD::SIGN_EXTEND:
7700 case ISD::ZERO_EXTEND:
7701 case ISD::ANY_EXTEND:
7702 return DAGCombineExtBoolTrunc(N, DCI);
7703 case ISD::TRUNCATE:
7704 case ISD::SETCC:
7705 case ISD::SELECT_CC:
7706 return DAGCombineTruncBoolExt(N, DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007707 case ISD::FDIV: {
7708 assert(TM.Options.UnsafeFPMath &&
7709 "Reciprocal estimates require UnsafeFPMath");
Scott Michelcf0da6c2009-02-17 22:15:04 +00007710
Hal Finkel2e103312013-04-03 04:01:11 +00007711 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007712 SDValue RV =
7713 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007714 if (RV.getNode() != 0) {
7715 DCI.AddToWorklist(RV.getNode());
7716 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7717 N->getOperand(0), RV);
7718 }
Hal Finkelf96c18e2013-04-04 22:44:12 +00007719 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
7720 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7721 SDValue RV =
7722 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7723 DCI);
7724 if (RV.getNode() != 0) {
7725 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007726 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007727 N->getValueType(0), RV);
7728 DCI.AddToWorklist(RV.getNode());
7729 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7730 N->getOperand(0), RV);
7731 }
7732 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
7733 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7734 SDValue RV =
7735 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7736 DCI);
7737 if (RV.getNode() != 0) {
7738 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007739 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007740 N->getValueType(0), RV,
7741 N->getOperand(1).getOperand(1));
7742 DCI.AddToWorklist(RV.getNode());
7743 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7744 N->getOperand(0), RV);
7745 }
Hal Finkel2e103312013-04-03 04:01:11 +00007746 }
7747
Hal Finkelb0c810f2013-04-03 17:44:56 +00007748 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007749 if (RV.getNode() != 0) {
7750 DCI.AddToWorklist(RV.getNode());
7751 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7752 N->getOperand(0), RV);
7753 }
7754
7755 }
7756 break;
7757 case ISD::FSQRT: {
7758 assert(TM.Options.UnsafeFPMath &&
7759 "Reciprocal estimates require UnsafeFPMath");
7760
7761 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
7762 // reciprocal sqrt.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007763 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007764 if (RV.getNode() != 0) {
7765 DCI.AddToWorklist(RV.getNode());
Hal Finkelb0c810f2013-04-03 17:44:56 +00007766 RV = DAGCombineFastRecip(RV, DCI);
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007767 if (RV.getNode() != 0) {
7768 // Unfortunately, RV is now NaN if the input was exactly 0. Select out
7769 // this case and force the answer to 0.
7770
7771 EVT VT = RV.getValueType();
7772
7773 SDValue Zero = DAG.getConstantFP(0.0, VT.getScalarType());
7774 if (VT.isVector()) {
7775 assert(VT.getVectorNumElements() == 4 && "Unknown vector type");
7776 Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Zero, Zero, Zero, Zero);
7777 }
7778
7779 SDValue ZeroCmp =
7780 DAG.getSetCC(dl, getSetCCResultType(*DAG.getContext(), VT),
7781 N->getOperand(0), Zero, ISD::SETEQ);
7782 DCI.AddToWorklist(ZeroCmp.getNode());
7783 DCI.AddToWorklist(RV.getNode());
7784
7785 RV = DAG.getNode(VT.isVector() ? ISD::VSELECT : ISD::SELECT, dl, VT,
7786 ZeroCmp, Zero, RV);
Hal Finkel2e103312013-04-03 04:01:11 +00007787 return RV;
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007788 }
Hal Finkel2e103312013-04-03 04:01:11 +00007789 }
7790
7791 }
7792 break;
Chris Lattnerf4184352006-03-01 04:57:39 +00007793 case ISD::SINT_TO_FP:
Chris Lattnera35f3062006-06-16 17:34:12 +00007794 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007795 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
7796 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
7797 // We allow the src/dst to be either f32/f64, but the intermediate
7798 // type must be i64.
Owen Anderson9f944592009-08-11 20:47:22 +00007799 if (N->getOperand(0).getValueType() == MVT::i64 &&
7800 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007801 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007802 if (Val.getValueType() == MVT::f32) {
7803 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007804 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007805 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007806
Owen Anderson9f944592009-08-11 20:47:22 +00007807 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007808 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007809 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007810 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007811 if (N->getValueType(0) == MVT::f32) {
7812 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner72733e52008-01-17 07:00:52 +00007813 DAG.getIntPtrConstant(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007814 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007815 }
7816 return Val;
Owen Anderson9f944592009-08-11 20:47:22 +00007817 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007818 // If the intermediate type is i32, we can avoid the load/store here
7819 // too.
Chris Lattnerf4184352006-03-01 04:57:39 +00007820 }
Chris Lattnerf4184352006-03-01 04:57:39 +00007821 }
7822 }
7823 break;
Chris Lattner27f53452006-03-01 05:50:56 +00007824 case ISD::STORE:
7825 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
7826 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnerf5b46f72008-01-18 16:54:56 +00007827 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00007828 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00007829 N->getOperand(1).getValueType() == MVT::i32 &&
7830 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007831 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007832 if (Val.getValueType() == MVT::f32) {
7833 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007834 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007835 }
Owen Anderson9f944592009-08-11 20:47:22 +00007836 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007837 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007838
Hal Finkel60c75102013-04-01 15:37:53 +00007839 SDValue Ops[] = {
7840 N->getOperand(0), Val, N->getOperand(2),
7841 DAG.getValueType(N->getOperand(1).getValueType())
7842 };
7843
7844 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
7845 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
7846 cast<StoreSDNode>(N)->getMemoryVT(),
7847 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00007848 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007849 return Val;
7850 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007851
Chris Lattnera7976d32006-07-10 20:56:58 +00007852 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00007853 if (cast<StoreSDNode>(N)->isUnindexed() &&
7854 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00007855 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00007856 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00007857 N->getOperand(1).getValueType() == MVT::i16 ||
7858 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00007859 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007860 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007861 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00007862 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00007863 if (BSwapOp.getValueType() == MVT::i16)
7864 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00007865
Dan Gohman48b185d2009-09-25 20:36:54 +00007866 SDValue Ops[] = {
7867 N->getOperand(0), BSwapOp, N->getOperand(2),
7868 DAG.getValueType(N->getOperand(1).getValueType())
7869 };
7870 return
7871 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
7872 Ops, array_lengthof(Ops),
7873 cast<StoreSDNode>(N)->getMemoryVT(),
7874 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00007875 }
7876 break;
Hal Finkelcf2e9082013-05-24 23:00:14 +00007877 case ISD::LOAD: {
7878 LoadSDNode *LD = cast<LoadSDNode>(N);
7879 EVT VT = LD->getValueType(0);
7880 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
7881 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
7882 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
7883 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
Hal Finkel40c34782013-09-15 22:09:58 +00007884 (VT == MVT::v16i8 || VT == MVT::v8i16 ||
7885 VT == MVT::v4i32 || VT == MVT::v4f32) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +00007886 LD->getAlignment() < ABIAlignment) {
7887 // This is a type-legal unaligned Altivec load.
7888 SDValue Chain = LD->getChain();
7889 SDValue Ptr = LD->getBasePtr();
7890
7891 // This implements the loading of unaligned vectors as described in
7892 // the venerable Apple Velocity Engine overview. Specifically:
7893 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
7894 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
7895 //
7896 // The general idea is to expand a sequence of one or more unaligned
7897 // loads into a alignment-based permutation-control instruction (lvsl),
7898 // a series of regular vector loads (which always truncate their
7899 // input address to an aligned address), and a series of permutations.
7900 // The results of these permutations are the requested loaded values.
7901 // The trick is that the last "extra" load is not taken from the address
7902 // you might suspect (sizeof(vector) bytes after the last requested
7903 // load), but rather sizeof(vector) - 1 bytes after the last
7904 // requested vector. The point of this is to avoid a page fault if the
Alp Tokercb402912014-01-24 17:20:08 +00007905 // base address happened to be aligned. This works because if the base
Hal Finkelcf2e9082013-05-24 23:00:14 +00007906 // address is aligned, then adding less than a full vector length will
7907 // cause the last vector in the sequence to be (re)loaded. Otherwise,
7908 // the next vector will be fetched as you might suspect was necessary.
7909
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007910 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00007911 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007912 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
7913 // optimization later.
Hal Finkelcf2e9082013-05-24 23:00:14 +00007914 SDValue PermCntl = BuildIntrinsicOp(Intrinsic::ppc_altivec_lvsl, Ptr,
7915 DAG, dl, MVT::v16i8);
7916
7917 // Refine the alignment of the original load (a "new" load created here
7918 // which was identical to the first except for the alignment would be
7919 // merged with the existing node regardless).
7920 MachineFunction &MF = DAG.getMachineFunction();
7921 MachineMemOperand *MMO =
7922 MF.getMachineMemOperand(LD->getPointerInfo(),
7923 LD->getMemOperand()->getFlags(),
7924 LD->getMemoryVT().getStoreSize(),
7925 ABIAlignment);
7926 LD->refineAlignment(MMO);
7927 SDValue BaseLoad = SDValue(LD, 0);
7928
7929 // Note that the value of IncOffset (which is provided to the next
7930 // load's pointer info offset value, and thus used to calculate the
7931 // alignment), and the value of IncValue (which is actually used to
7932 // increment the pointer value) are different! This is because we
7933 // require the next load to appear to be aligned, even though it
7934 // is actually offset from the base pointer by a lesser amount.
7935 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +00007936 int IncValue = IncOffset;
7937
7938 // Walk (both up and down) the chain looking for another load at the real
7939 // (aligned) offset (the alignment of the other load does not matter in
7940 // this case). If found, then do not use the offset reduction trick, as
7941 // that will prevent the loads from being later combined (as they would
7942 // otherwise be duplicates).
7943 if (!findConsecutiveLoad(LD, DAG))
7944 --IncValue;
7945
Hal Finkelcf2e9082013-05-24 23:00:14 +00007946 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
7947 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
7948
Hal Finkelcf2e9082013-05-24 23:00:14 +00007949 SDValue ExtraLoad =
7950 DAG.getLoad(VT, dl, Chain, Ptr,
7951 LD->getPointerInfo().getWithOffset(IncOffset),
7952 LD->isVolatile(), LD->isNonTemporal(),
7953 LD->isInvariant(), ABIAlignment);
7954
7955 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7956 BaseLoad.getValue(1), ExtraLoad.getValue(1));
7957
7958 if (BaseLoad.getValueType() != MVT::v4i32)
7959 BaseLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, BaseLoad);
7960
7961 if (ExtraLoad.getValueType() != MVT::v4i32)
7962 ExtraLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ExtraLoad);
7963
7964 SDValue Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
7965 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
7966
7967 if (VT != MVT::v4i32)
7968 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
7969
7970 // Now we need to be really careful about how we update the users of the
7971 // original load. We cannot just call DCI.CombineTo (or
7972 // DAG.ReplaceAllUsesWith for that matter), because the load still has
7973 // uses created here (the permutation for example) that need to stay.
7974 SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
7975 while (UI != UE) {
7976 SDUse &Use = UI.getUse();
7977 SDNode *User = *UI;
7978 // Note: BaseLoad is checked here because it might not be N, but a
7979 // bitcast of N.
7980 if (User == Perm.getNode() || User == BaseLoad.getNode() ||
7981 User == TF.getNode() || Use.getResNo() > 1) {
7982 ++UI;
7983 continue;
7984 }
7985
7986 SDValue To = Use.getResNo() ? TF : Perm;
7987 ++UI;
7988
7989 SmallVector<SDValue, 8> Ops;
7990 for (SDNode::op_iterator O = User->op_begin(),
7991 OE = User->op_end(); O != OE; ++O) {
7992 if (*O == Use)
7993 Ops.push_back(To);
7994 else
7995 Ops.push_back(*O);
7996 }
7997
7998 DAG.UpdateNodeOperands(User, Ops.data(), Ops.size());
7999 }
8000
8001 return SDValue(N, 0);
8002 }
8003 }
8004 break;
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008005 case ISD::INTRINSIC_WO_CHAIN:
8006 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() ==
8007 Intrinsic::ppc_altivec_lvsl &&
8008 N->getOperand(1)->getOpcode() == ISD::ADD) {
8009 SDValue Add = N->getOperand(1);
8010
8011 if (DAG.MaskedValueIsZero(Add->getOperand(1),
8012 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
8013 Add.getValueType().getScalarType().getSizeInBits()))) {
8014 SDNode *BasePtr = Add->getOperand(0).getNode();
8015 for (SDNode::use_iterator UI = BasePtr->use_begin(),
8016 UE = BasePtr->use_end(); UI != UE; ++UI) {
8017 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8018 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
8019 Intrinsic::ppc_altivec_lvsl) {
8020 // We've found another LVSL, and this address if an aligned
8021 // multiple of that one. The results will be the same, so use the
8022 // one we've just found instead.
8023
8024 return SDValue(*UI, 0);
8025 }
8026 }
8027 }
8028 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +00008029
8030 break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008031 case ISD::BSWAP:
8032 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008033 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +00008034 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008035 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
8036 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008037 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008038 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008039 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00008040 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +00008041 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008042 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +00008043 LD->getChain(), // Chain
8044 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008045 DAG.getValueType(N->getValueType(0)) // VT
8046 };
Dan Gohman48b185d2009-09-25 20:36:54 +00008047 SDValue BSLoad =
8048 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +00008049 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
8050 MVT::i64 : MVT::i32, MVT::Other),
Hal Finkel93492fa2013-03-28 19:43:12 +00008051 Ops, 3, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008052
Scott Michelcf0da6c2009-02-17 22:15:04 +00008053 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008054 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +00008055 if (N->getValueType(0) == MVT::i16)
8056 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008057
Chris Lattnera7976d32006-07-10 20:56:58 +00008058 // First, combine the bswap away. This makes the value produced by the
8059 // load dead.
8060 DCI.CombineTo(N, ResVal);
8061
8062 // Next, combine the load away, we give it a bogus result value but a real
8063 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008064 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00008065
Chris Lattnera7976d32006-07-10 20:56:58 +00008066 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008067 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008068 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008069
Chris Lattner27f53452006-03-01 05:50:56 +00008070 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008071 case PPCISD::VCMP: {
8072 // If a VCMPo node already exists with exactly the same operands as this
8073 // node, use its result instead of this node (VCMPo computes both a CR6 and
8074 // a normal output).
8075 //
8076 if (!N->getOperand(0).hasOneUse() &&
8077 !N->getOperand(1).hasOneUse() &&
8078 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00008079
Chris Lattnerd4058a52006-03-31 06:02:07 +00008080 // Scan all of the users of the LHS, looking for VCMPo's that match.
8081 SDNode *VCMPoNode = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008082
Gabor Greiff304a7a2008-08-28 21:40:38 +00008083 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +00008084 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
8085 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008086 if (UI->getOpcode() == PPCISD::VCMPo &&
8087 UI->getOperand(1) == N->getOperand(1) &&
8088 UI->getOperand(2) == N->getOperand(2) &&
8089 UI->getOperand(0) == N->getOperand(0)) {
8090 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008091 break;
8092 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008093
Chris Lattner518834c2006-04-18 18:28:22 +00008094 // If there is no VCMPo node, or if the flag value has a single use, don't
8095 // transform this.
8096 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
8097 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008098
8099 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +00008100 // chain, this transformation is more complex. Note that multiple things
8101 // could use the value result, which we should ignore.
8102 SDNode *FlagUser = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008103 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner518834c2006-04-18 18:28:22 +00008104 FlagUser == 0; ++UI) {
8105 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008106 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +00008107 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008108 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +00008109 FlagUser = User;
8110 break;
8111 }
8112 }
8113 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008114
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008115 // If the user is a MFOCRF instruction, we know this is safe.
8116 // Otherwise we give up for right now.
8117 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008118 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +00008119 }
8120 break;
8121 }
Hal Finkel940ab932014-02-28 00:27:01 +00008122 case ISD::BRCOND: {
8123 SDValue Cond = N->getOperand(1);
8124 SDValue Target = N->getOperand(2);
8125
8126 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8127 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
8128 Intrinsic::ppc_is_decremented_ctr_nonzero) {
8129
8130 // We now need to make the intrinsic dead (it cannot be instruction
8131 // selected).
8132 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
8133 assert(Cond.getNode()->hasOneUse() &&
8134 "Counter decrement has more than one use");
8135
8136 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
8137 N->getOperand(0), Target);
8138 }
8139 }
8140 break;
Chris Lattner9754d142006-04-18 17:59:36 +00008141 case ISD::BR_CC: {
8142 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008143 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +00008144 // lowering is done pre-legalize, because the legalizer lowers the predicate
8145 // compare down to code that is difficult to reassemble.
8146 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008147 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +00008148
8149 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
8150 // value. If so, pass-through the AND to get to the intrinsic.
8151 if (LHS.getOpcode() == ISD::AND &&
8152 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8153 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
8154 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8155 isa<ConstantSDNode>(LHS.getOperand(1)) &&
8156 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
8157 isZero())
8158 LHS = LHS.getOperand(0);
8159
8160 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8161 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
8162 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8163 isa<ConstantSDNode>(RHS)) {
8164 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
8165 "Counter decrement comparison is not EQ or NE");
8166
8167 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
8168 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
8169 (CC == ISD::SETNE && !Val);
8170
8171 // We now need to make the intrinsic dead (it cannot be instruction
8172 // selected).
8173 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
8174 assert(LHS.getNode()->hasOneUse() &&
8175 "Counter decrement has more than one use");
8176
8177 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
8178 N->getOperand(0), N->getOperand(4));
8179 }
8180
Chris Lattner9754d142006-04-18 17:59:36 +00008181 int CompareOpc;
8182 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008183
Chris Lattner9754d142006-04-18 17:59:36 +00008184 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8185 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
8186 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
8187 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00008188
Chris Lattner9754d142006-04-18 17:59:36 +00008189 // If this is a comparison against something other than 0/1, then we know
8190 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008191 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00008192 if (Val != 0 && Val != 1) {
8193 if (CC == ISD::SETEQ) // Cond never true, remove branch.
8194 return N->getOperand(0);
8195 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +00008196 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +00008197 N->getOperand(0), N->getOperand(4));
8198 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008199
Chris Lattner9754d142006-04-18 17:59:36 +00008200 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008201
Chris Lattner9754d142006-04-18 17:59:36 +00008202 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008203 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008204 LHS.getOperand(2), // LHS of compare
8205 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +00008206 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008207 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00008208 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00008209 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008210
Chris Lattner9754d142006-04-18 17:59:36 +00008211 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008212 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +00008213 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +00008214 default: // Can't happen, don't crash on invalid number though.
8215 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008216 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +00008217 break;
8218 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008219 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +00008220 break;
8221 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008222 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +00008223 break;
8224 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008225 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +00008226 break;
8227 }
8228
Owen Anderson9f944592009-08-11 20:47:22 +00008229 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
8230 DAG.getConstant(CompOpc, MVT::i32),
8231 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +00008232 N->getOperand(4), CompNode.getValue(1));
8233 }
8234 break;
8235 }
Chris Lattnerf4184352006-03-01 04:57:39 +00008236 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008237
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008238 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +00008239}
8240
Chris Lattner4211ca92006-04-14 06:01:58 +00008241//===----------------------------------------------------------------------===//
8242// Inline Assembly Support
8243//===----------------------------------------------------------------------===//
8244
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008245void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Scott Michelcf0da6c2009-02-17 22:15:04 +00008246 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +00008247 APInt &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +00008248 const SelectionDAG &DAG,
Chris Lattnerc5287c02006-04-02 06:26:07 +00008249 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00008250 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +00008251 switch (Op.getOpcode()) {
8252 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008253 case PPCISD::LBRX: {
8254 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +00008255 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +00008256 KnownZero = 0xFFFF0000;
8257 break;
8258 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008259 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00008260 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +00008261 default: break;
8262 case Intrinsic::ppc_altivec_vcmpbfp_p:
8263 case Intrinsic::ppc_altivec_vcmpeqfp_p:
8264 case Intrinsic::ppc_altivec_vcmpequb_p:
8265 case Intrinsic::ppc_altivec_vcmpequh_p:
8266 case Intrinsic::ppc_altivec_vcmpequw_p:
8267 case Intrinsic::ppc_altivec_vcmpgefp_p:
8268 case Intrinsic::ppc_altivec_vcmpgtfp_p:
8269 case Intrinsic::ppc_altivec_vcmpgtsb_p:
8270 case Intrinsic::ppc_altivec_vcmpgtsh_p:
8271 case Intrinsic::ppc_altivec_vcmpgtsw_p:
8272 case Intrinsic::ppc_altivec_vcmpgtub_p:
8273 case Intrinsic::ppc_altivec_vcmpgtuh_p:
8274 case Intrinsic::ppc_altivec_vcmpgtuw_p:
8275 KnownZero = ~1U; // All bits but the low one are known to be zero.
8276 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008277 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008278 }
8279 }
8280}
8281
8282
Chris Lattnerd6855142007-03-25 02:14:49 +00008283/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +00008284/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008285PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00008286PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
8287 if (Constraint.size() == 1) {
8288 switch (Constraint[0]) {
8289 default: break;
8290 case 'b':
8291 case 'r':
8292 case 'f':
8293 case 'v':
8294 case 'y':
8295 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +00008296 case 'Z':
8297 // FIXME: While Z does indicate a memory constraint, it specifically
8298 // indicates an r+r address (used in conjunction with the 'y' modifier
8299 // in the replacement string). Currently, we're forcing the base
8300 // register to be r0 in the asm printer (which is interpreted as zero)
8301 // and forming the complete address in the second register. This is
8302 // suboptimal.
8303 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +00008304 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008305 } else if (Constraint == "wc") { // individual CR bits.
8306 return C_RegisterClass;
Chris Lattnerd6855142007-03-25 02:14:49 +00008307 }
8308 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +00008309}
8310
John Thompsone8360b72010-10-29 17:29:13 +00008311/// Examine constraint type and operand type and determine a weight value.
8312/// This object must already have been set up with the operand type
8313/// and the current alternative constraint selected.
8314TargetLowering::ConstraintWeight
8315PPCTargetLowering::getSingleConstraintMatchWeight(
8316 AsmOperandInfo &info, const char *constraint) const {
8317 ConstraintWeight weight = CW_Invalid;
8318 Value *CallOperandVal = info.CallOperandVal;
8319 // If we don't have a value, we can't do a match,
8320 // but allow it at the lowest weight.
8321 if (CallOperandVal == NULL)
8322 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00008323 Type *type = CallOperandVal->getType();
Hal Finkel6aca2372014-03-02 18:23:39 +00008324
John Thompsone8360b72010-10-29 17:29:13 +00008325 // Look at the constraint type.
Hal Finkel6aca2372014-03-02 18:23:39 +00008326 if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
8327 return CW_Register; // an individual CR bit.
8328
John Thompsone8360b72010-10-29 17:29:13 +00008329 switch (*constraint) {
8330 default:
8331 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8332 break;
8333 case 'b':
8334 if (type->isIntegerTy())
8335 weight = CW_Register;
8336 break;
8337 case 'f':
8338 if (type->isFloatTy())
8339 weight = CW_Register;
8340 break;
8341 case 'd':
8342 if (type->isDoubleTy())
8343 weight = CW_Register;
8344 break;
8345 case 'v':
8346 if (type->isVectorTy())
8347 weight = CW_Register;
8348 break;
8349 case 'y':
8350 weight = CW_Register;
8351 break;
Hal Finkel4f24c622012-11-05 18:18:42 +00008352 case 'Z':
8353 weight = CW_Memory;
8354 break;
John Thompsone8360b72010-10-29 17:29:13 +00008355 }
8356 return weight;
8357}
8358
Scott Michelcf0da6c2009-02-17 22:15:04 +00008359std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner584a11a2006-11-02 01:44:04 +00008360PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00008361 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +00008362 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +00008363 // GCC RS6000 Constraint Letters
8364 switch (Constraint[0]) {
8365 case 'b': // R1-R31
Hal Finkel638a9fa2013-03-19 18:51:05 +00008366 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
8367 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
8368 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008369 case 'r': // R0-R31
Owen Anderson9f944592009-08-11 20:47:22 +00008370 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +00008371 return std::make_pair(0U, &PPC::G8RCRegClass);
8372 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008373 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008374 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +00008375 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008376 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +00008377 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008378 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008379 case 'v':
Craig Topperabadc662012-04-20 06:31:50 +00008380 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008381 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +00008382 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008383 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008384 } else if (Constraint == "wc") { // an individual CR bit.
8385 return std::make_pair(0U, &PPC::CRBITRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008386 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008387
Hal Finkelb176acb2013-08-03 12:25:10 +00008388 std::pair<unsigned, const TargetRegisterClass*> R =
8389 TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8390
8391 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
8392 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
8393 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
8394 // register.
8395 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
8396 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
8397 if (R.first && VT == MVT::i64 && PPCSubTarget.isPPC64() &&
8398 PPC::GPRCRegClass.contains(R.first)) {
8399 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
8400 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +00008401 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +00008402 &PPC::G8RCRegClass);
8403 }
8404
8405 return R;
Chris Lattner01513612006-01-31 19:20:21 +00008406}
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008407
Chris Lattner584a11a2006-11-02 01:44:04 +00008408
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008409/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +00008410/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +00008411void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00008412 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008413 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00008414 SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008415 SDValue Result(0,0);
Eric Christopher0713a9d2011-06-08 23:55:35 +00008416
Eric Christopherde9399b2011-06-02 23:16:42 +00008417 // Only support length 1 constraints.
8418 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008419
Eric Christopherde9399b2011-06-02 23:16:42 +00008420 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008421 switch (Letter) {
8422 default: break;
8423 case 'I':
8424 case 'J':
8425 case 'K':
8426 case 'L':
8427 case 'M':
8428 case 'N':
8429 case 'O':
8430 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +00008431 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008432 if (!CST) return; // Must be an immediate to match.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008433 unsigned Value = CST->getZExtValue();
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008434 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008435 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008436 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008437 if ((short)Value == (int)Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008438 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008439 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008440 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
8441 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008442 if ((short)Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008443 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008444 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008445 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008446 if ((Value >> 16) == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008447 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008448 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008449 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008450 if (Value > 31)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008451 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008452 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008453 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008454 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008455 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008456 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008457 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008458 if (Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008459 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008460 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008461 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008462 if ((short)-Value == (int)-Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008463 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008464 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008465 }
8466 break;
8467 }
8468 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008469
Gabor Greiff304a7a2008-08-28 21:40:38 +00008470 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008471 Ops.push_back(Result);
8472 return;
8473 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008474
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008475 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +00008476 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008477}
Evan Cheng2dd2c652006-03-13 23:20:37 +00008478
Chris Lattner1eb94d92007-03-30 23:15:24 +00008479// isLegalAddressingMode - Return true if the addressing mode represented
8480// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008481bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00008482 Type *Ty) const {
Chris Lattner1eb94d92007-03-30 23:15:24 +00008483 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelcf0da6c2009-02-17 22:15:04 +00008484
Chris Lattner1eb94d92007-03-30 23:15:24 +00008485 // PPC allows a sign-extended 16-bit immediate field.
8486 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
8487 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008488
Chris Lattner1eb94d92007-03-30 23:15:24 +00008489 // No global is ever allowed as a base.
8490 if (AM.BaseGV)
8491 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008492
8493 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +00008494 switch (AM.Scale) {
8495 case 0: // "r+i" or just "i", depending on HasBaseReg.
8496 break;
8497 case 1:
8498 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
8499 return false;
8500 // Otherwise we have r+r or r+i.
8501 break;
8502 case 2:
8503 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
8504 return false;
8505 // Allow 2*r as r+r.
8506 break;
Chris Lattner19ccd622007-04-09 22:10:05 +00008507 default:
8508 // No other scales are supported.
8509 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +00008510 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008511
Chris Lattner1eb94d92007-03-30 23:15:24 +00008512 return true;
8513}
8514
Dan Gohman21cea8a2010-04-17 15:26:15 +00008515SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
8516 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00008517 MachineFunction &MF = DAG.getMachineFunction();
8518 MachineFrameInfo *MFI = MF.getFrameInfo();
8519 MFI->setReturnAddressIsTaken(true);
8520
Bill Wendling908bf812014-01-06 00:43:20 +00008521 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008522 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008523
Andrew Trickef9de2a2013-05-25 02:42:55 +00008524 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008525 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +00008526
Dale Johannesen81bfca72010-05-03 22:59:34 +00008527 // Make sure the function does not optimize away the store of the RA to
8528 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +00008529 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008530 FuncInfo->setLRStoreRequired();
8531 bool isPPC64 = PPCSubTarget.isPPC64();
8532 bool isDarwinABI = PPCSubTarget.isDarwinABI();
8533
8534 if (Depth > 0) {
8535 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8536 SDValue Offset =
Wesley Peck527da1b2010-11-23 03:31:01 +00008537
Anton Korobeynikov2f931282011-01-10 12:39:04 +00008538 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen81bfca72010-05-03 22:59:34 +00008539 isPPC64? MVT::i64 : MVT::i32);
8540 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
8541 DAG.getNode(ISD::ADD, dl, getPointerTy(),
8542 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008543 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008544 }
Chris Lattnerf6a81562007-12-08 06:59:59 +00008545
Chris Lattnerf6a81562007-12-08 06:59:59 +00008546 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008547 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008548 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008549 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +00008550}
8551
Dan Gohman21cea8a2010-04-17 15:26:15 +00008552SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
8553 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008554 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008555 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00008556
Owen Anderson53aa7a92009-08-10 22:56:29 +00008557 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00008558 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008559
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008560 MachineFunction &MF = DAG.getMachineFunction();
8561 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008562 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +00008563
8564 // Naked functions never have a frame pointer, and so we use r1. For all
8565 // other functions, this decision must be delayed until during PEI.
8566 unsigned FrameReg;
8567 if (MF.getFunction()->getAttributes().hasAttribute(
8568 AttributeSet::FunctionIndex, Attribute::Naked))
8569 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
8570 else
8571 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
8572
Dale Johannesen81bfca72010-05-03 22:59:34 +00008573 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
8574 PtrVT);
8575 while (Depth--)
8576 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008577 FrameAddr, MachinePointerInfo(), false, false,
8578 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008579 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008580}
Dan Gohmanc14e5222008-10-21 03:41:46 +00008581
8582bool
8583PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8584 // The PowerPC target isn't yet aware of offsets.
8585 return false;
8586}
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008587
Evan Chengd9929f02010-04-01 20:10:42 +00008588/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +00008589/// and store operations as a result of memset, memcpy, and memmove
8590/// lowering. If DstAlign is zero that means it's safe to destination
8591/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
8592/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +00008593/// probably because the source does not need to be loaded. If 'IsMemset' is
8594/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
8595/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
8596/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +00008597/// It returns EVT::Other if the type should be determined using generic
8598/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +00008599EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
8600 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00008601 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00008602 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +00008603 MachineFunction &MF) const {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008604 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +00008605 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008606 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00008607 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008608 }
8609}
Hal Finkel88ed4e32012-04-01 19:23:08 +00008610
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008611bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +00008612 unsigned,
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008613 bool *Fast) const {
8614 if (DisablePPCUnaligned)
8615 return false;
8616
8617 // PowerPC supports unaligned memory access for simple non-vector types.
8618 // Although accessing unaligned addresses is not as efficient as accessing
8619 // aligned addresses, it is generally more efficient than manual expansion,
8620 // and generally only traps for software emulation when crossing page
8621 // boundaries.
8622
8623 if (!VT.isSimple())
8624 return false;
8625
8626 if (VT.getSimpleVT().isVector())
8627 return false;
8628
8629 if (VT == MVT::ppcf128)
8630 return false;
8631
8632 if (Fast)
8633 *Fast = true;
8634
8635 return true;
8636}
8637
Stephen Lin73de7bf2013-07-09 18:16:56 +00008638bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
8639 VT = VT.getScalarType();
8640
Hal Finkel0a479ae2012-06-22 00:49:52 +00008641 if (!VT.isSimple())
8642 return false;
8643
8644 switch (VT.getSimpleVT().SimpleTy) {
8645 case MVT::f32:
8646 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +00008647 return true;
8648 default:
8649 break;
8650 }
8651
8652 return false;
8653}
8654
Hal Finkel88ed4e32012-04-01 19:23:08 +00008655Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel21442b22013-09-11 23:05:25 +00008656 if (DisableILPPref || PPCSubTarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008657 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +00008658
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008659 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +00008660}
8661
Bill Schmidt0cf702f2013-07-30 00:50:39 +00008662// Create a fast isel object.
8663FastISel *
8664PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
8665 const TargetLibraryInfo *LibInfo) const {
8666 return PPC::createFastISel(FuncInfo, LibInfo);
8667}