blob: 214c86920609eb25f50db74341b9a8a3ade7b054 [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"
Hal Finkel0d8db462014-05-11 19:29:11 +000021#include "llvm/ADT/StringSwitch.h"
Chris Lattner4f2e4e02007-03-06 00:59:59 +000022#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner9b577f12005-08-26 21:23:58 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000027#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000028#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/CallingConv.h"
30#include "llvm/IR/Constants.h"
31#include "llvm/IR/DerivedTypes.h"
32#include "llvm/IR/Function.h"
33#include "llvm/IR/Intrinsics.h"
Chris Lattnerce645542006-11-10 02:08:47 +000034#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000035#include "llvm/Support/ErrorHandling.h"
Craig Topperb25fda92012-03-17 18:46:09 +000036#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000037#include "llvm/Support/raw_ostream.h"
Craig Topperb25fda92012-03-17 18:46:09 +000038#include "llvm/Target/TargetOptions.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000039using namespace llvm;
40
Hal Finkel595817e2012-06-04 02:21:00 +000041static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
42cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000043
Hal Finkel4e9f1a82012-06-10 19:32:29 +000044static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
45cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
46
Hal Finkel8d7fbc92013-03-15 15:27:13 +000047static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
48cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
49
Hal Finkel940ab932014-02-28 00:27:01 +000050// FIXME: Remove this once the bug has been fixed!
51extern cl::opt<bool> ANDIGlueBug;
52
Chris Lattner5e693ed2009-07-28 03:13:23 +000053static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) {
54 if (TM.getSubtargetImpl()->isDarwin())
Bill Wendlingbbcaa402010-03-15 21:09:38 +000055 return new TargetLoweringObjectFileMachO();
Bill Wendlingdd3fe942010-03-12 02:00:43 +000056
Bill Schmidt22d40dc2013-05-13 19:34:37 +000057 if (TM.getSubtargetImpl()->isSVR4ABI())
58 return new PPC64LinuxTargetObjectFile();
59
Bruno Cardoso Lopes62e6a8b2009-08-13 23:30:21 +000060 return new TargetLoweringObjectFileELF();
Chris Lattner5e693ed2009-07-28 03:13:23 +000061}
62
Chris Lattner584a11a2006-11-02 01:44:04 +000063PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Chris Lattner5e693ed2009-07-28 03:13:23 +000064 : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) {
Evan Cheng39e90022012-07-02 22:39:56 +000065 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
Scott Michelcf0da6c2009-02-17 22:15:04 +000066
Nate Begeman4dd38312005-10-21 00:02:42 +000067 setPow2DivIsCheap();
Dale Johannesenc31eb202008-07-31 18:13:12 +000068
Chris Lattnera028e7a2005-09-27 22:18:25 +000069 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000070 setUseUnderscoreSetJmp(true);
71 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000072
Chris Lattnerd10babf2010-10-10 18:34:00 +000073 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
74 // arguments are at least 4/8 bytes aligned.
Evan Cheng39e90022012-07-02 22:39:56 +000075 bool isPPC64 = Subtarget->isPPC64();
76 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000077
Chris Lattnerf22556d2005-08-16 17:14:42 +000078 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000079 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
80 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
81 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +000082
Evan Cheng5d9fd972006-10-04 00:56:09 +000083 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson9f944592009-08-11 20:47:22 +000084 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
85 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sands95d46ef2008-01-23 20:39:46 +000086
Owen Anderson9f944592009-08-11 20:47:22 +000087 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000088
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000089 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000090 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
91 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
92 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
93 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
94 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
95 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
96 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
97 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
98 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
99 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +0000100
Hal Finkel940ab932014-02-28 00:27:01 +0000101 if (Subtarget->useCRBits()) {
102 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
103
Hal Finkel6a56b212014-03-05 22:14:00 +0000104 if (isPPC64 || Subtarget->hasFPCVT()) {
105 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
106 AddPromotedToType (ISD::SINT_TO_FP, MVT::i1,
107 isPPC64 ? MVT::i64 : MVT::i32);
108 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
109 AddPromotedToType (ISD::UINT_TO_FP, MVT::i1,
110 isPPC64 ? MVT::i64 : MVT::i32);
111 } else {
112 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom);
113 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom);
114 }
Hal Finkel940ab932014-02-28 00:27:01 +0000115
116 // PowerPC does not support direct load / store of condition registers
117 setOperationAction(ISD::LOAD, MVT::i1, Custom);
118 setOperationAction(ISD::STORE, MVT::i1, Custom);
119
120 // FIXME: Remove this once the ANDI glue bug is fixed:
121 if (ANDIGlueBug)
122 setOperationAction(ISD::TRUNCATE, MVT::i1, Custom);
123
124 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
125 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
126 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
127 setTruncStoreAction(MVT::i32, MVT::i1, Expand);
128 setTruncStoreAction(MVT::i16, MVT::i1, Expand);
129 setTruncStoreAction(MVT::i8, MVT::i1, Expand);
130
131 addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass);
132 }
133
Dale Johannesen666323e2007-10-10 01:01:31 +0000134 // This is used in the ppcf128->int sequence. Note it has different semantics
135 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +0000136 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000137
Roman Divacky1faf5b02012-08-16 18:19:29 +0000138 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000139 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
140 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
141 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
142 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
143 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000144 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000145
Chris Lattnerf22556d2005-08-16 17:14:42 +0000146 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000147 setOperationAction(ISD::SREM, MVT::i32, Expand);
148 setOperationAction(ISD::UREM, MVT::i32, Expand);
149 setOperationAction(ISD::SREM, MVT::i64, Expand);
150 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000151
152 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000153 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
154 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
155 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
156 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
157 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
158 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
159 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
160 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000161
Dan Gohman482732a2007-10-11 23:21:31 +0000162 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000163 setOperationAction(ISD::FSIN , MVT::f64, Expand);
164 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000165 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000166 setOperationAction(ISD::FREM , MVT::f64, Expand);
167 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000168 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000169 setOperationAction(ISD::FSIN , MVT::f32, Expand);
170 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000171 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000172 setOperationAction(ISD::FREM , MVT::f32, Expand);
173 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000174 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000175
Owen Anderson9f944592009-08-11 20:47:22 +0000176 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000177
Chris Lattnerf22556d2005-08-16 17:14:42 +0000178 // If we're enabling GP optimizations, use hardware square root
Hal Finkel2e103312013-04-03 04:01:11 +0000179 if (!Subtarget->hasFSQRT() &&
180 !(TM.Options.UnsafeFPMath &&
181 Subtarget->hasFRSQRTE() && Subtarget->hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000182 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000183
184 if (!Subtarget->hasFSQRT() &&
185 !(TM.Options.UnsafeFPMath &&
186 Subtarget->hasFRSQRTES() && Subtarget->hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000187 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000188
Hal Finkeldbc78e12013-08-19 05:01:02 +0000189 if (Subtarget->hasFCPSGN()) {
190 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
191 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
192 } else {
193 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
194 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
195 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000196
Hal Finkelc20a08d2013-03-29 08:57:48 +0000197 if (Subtarget->hasFPRND()) {
198 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
199 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
200 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000201 setOperationAction(ISD::FROUND, MVT::f64, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000202
203 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
204 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
205 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000206 setOperationAction(ISD::FROUND, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000207 }
208
Nate Begeman2fba8a32006-01-14 03:14:10 +0000209 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000210 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000211 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000212 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
213 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000214 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000215 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000216 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
217 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000218
Hal Finkela4d07482013-03-28 13:29:47 +0000219 if (Subtarget->hasPOPCNTD()) {
Hal Finkel290376d2013-04-01 15:58:15 +0000220 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000221 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
222 } else {
223 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
224 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
225 }
226
Nate Begeman1b8121b2006-01-11 21:21:00 +0000227 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000228 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
229 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000230
Hal Finkel940ab932014-02-28 00:27:01 +0000231 if (!Subtarget->useCRBits()) {
232 // PowerPC does not have Select
233 setOperationAction(ISD::SELECT, MVT::i32, Expand);
234 setOperationAction(ISD::SELECT, MVT::i64, Expand);
235 setOperationAction(ISD::SELECT, MVT::f32, Expand);
236 setOperationAction(ISD::SELECT, MVT::f64, Expand);
237 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000238
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000239 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000240 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
241 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000242
Nate Begeman7e7f4392006-02-01 07:19:44 +0000243 // PowerPC wants to optimize integer setcc a bit
Hal Finkel940ab932014-02-28 00:27:01 +0000244 if (!Subtarget->useCRBits())
245 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000246
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000247 // PowerPC does not have BRCOND which requires SetCC
Hal Finkel940ab932014-02-28 00:27:01 +0000248 if (!Subtarget->useCRBits())
249 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000250
Owen Anderson9f944592009-08-11 20:47:22 +0000251 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000252
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000253 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000254 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000255
Jim Laskey6267b2c2005-08-17 00:40:22 +0000256 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000257 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
258 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000259
Wesley Peck527da1b2010-11-23 03:31:01 +0000260 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
261 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
262 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
263 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattnerc46fc242005-12-23 05:13:35 +0000264
Chris Lattner84b49d52006-04-28 21:56:10 +0000265 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000266 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000267
Hal Finkel1996f3d2013-03-27 19:10:42 +0000268 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000269 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
270 // support continuation, user-level threading, and etc.. As a result, no
271 // other SjLj exception interfaces are implemented and please don't build
272 // your own exception handling based on them.
273 // LLVM/Clang supports zero-cost DWARF exception handling.
274 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
275 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000276
277 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000278 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000279 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
280 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000281 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000282 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
283 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
284 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
285 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000286 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000287 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
288 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000289
Nate Begemanf69d13b2008-08-11 17:36:31 +0000290 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000291 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000292
293 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000294 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
295 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000296
Nate Begemane74795c2006-01-25 18:21:52 +0000297 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000298 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000299
Evan Cheng39e90022012-07-02 22:39:56 +0000300 if (Subtarget->isSVR4ABI()) {
301 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000302 // VAARG always uses double-word chunks, so promote anything smaller.
303 setOperationAction(ISD::VAARG, MVT::i1, Promote);
304 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
305 setOperationAction(ISD::VAARG, MVT::i8, Promote);
306 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
307 setOperationAction(ISD::VAARG, MVT::i16, Promote);
308 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
309 setOperationAction(ISD::VAARG, MVT::i32, Promote);
310 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
311 setOperationAction(ISD::VAARG, MVT::Other, Expand);
312 } else {
313 // VAARG is custom lowered with the 32-bit SVR4 ABI.
314 setOperationAction(ISD::VAARG, MVT::Other, Custom);
315 setOperationAction(ISD::VAARG, MVT::i64, Custom);
316 }
Roman Divacky4394e682011-06-28 15:30:42 +0000317 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000318 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000319
Roman Divackyc3825df2013-07-25 21:36:47 +0000320 if (Subtarget->isSVR4ABI() && !isPPC64)
321 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
322 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
323 else
324 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
325
Chris Lattner5bd514d2006-01-15 09:02:48 +0000326 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000327 setOperationAction(ISD::VAEND , MVT::Other, Expand);
328 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
329 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
330 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
331 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000332
Chris Lattner6961fc72006-03-26 10:06:40 +0000333 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000334 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000335
Hal Finkel25c19922013-05-15 21:37:41 +0000336 // To handle counter-based loop conditions.
337 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
338
Dale Johannesen160be0f2008-11-07 22:54:33 +0000339 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000340 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
341 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
342 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
343 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
344 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
345 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
346 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
347 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
348 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
349 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
350 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
351 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000352
Evan Cheng39e90022012-07-02 22:39:56 +0000353 if (Subtarget->has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000354 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000355 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
356 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
357 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
358 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000359 // This is just the low 32 bits of a (signed) fp->i64 conversion.
360 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000361 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000362
Hal Finkelf6d45f22013-04-01 17:52:07 +0000363 if (PPCSubTarget.hasLFIWAX() || Subtarget->isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000364 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000365 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000366 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000367 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000368 }
369
Hal Finkelf6d45f22013-04-01 17:52:07 +0000370 // With the instructions enabled under FPCVT, we can do everything.
371 if (PPCSubTarget.hasFPCVT()) {
372 if (Subtarget->has64BitSupport()) {
373 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
374 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
375 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
376 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
377 }
378
379 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
380 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
381 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
382 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
383 }
384
Evan Cheng39e90022012-07-02 22:39:56 +0000385 if (Subtarget->use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000386 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000387 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000388 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000389 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000390 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000391 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
392 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
393 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000394 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000395 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000396 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
397 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
398 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000399 }
Evan Cheng19264272006-03-01 01:11:20 +0000400
Evan Cheng39e90022012-07-02 22:39:56 +0000401 if (Subtarget->hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000402 // First set operation action for all vector types to expand. Then we
403 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson9f944592009-08-11 20:47:22 +0000404 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
405 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
406 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands13237ac2008-06-06 12:08:01 +0000407
Chris Lattner06a21ba2006-04-16 01:37:57 +0000408 // add/sub are legal for all supported vector VT's.
Duncan Sands13237ac2008-06-06 12:08:01 +0000409 setOperationAction(ISD::ADD , VT, Legal);
410 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000411
Chris Lattner95c7adc2006-04-04 17:25:31 +0000412 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000413 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000414 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000415
416 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000417 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000418 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000419 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000420 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000421 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000422 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000423 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000424 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000425 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000426 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000427 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000428 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000429
Chris Lattner06a21ba2006-04-16 01:37:57 +0000430 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000431 setOperationAction(ISD::MUL , VT, Expand);
432 setOperationAction(ISD::SDIV, VT, Expand);
433 setOperationAction(ISD::SREM, VT, Expand);
434 setOperationAction(ISD::UDIV, VT, Expand);
435 setOperationAction(ISD::UREM, VT, Expand);
436 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000437 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000438 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000439 setOperationAction(ISD::FSQRT, VT, Expand);
440 setOperationAction(ISD::FLOG, VT, Expand);
441 setOperationAction(ISD::FLOG10, VT, Expand);
442 setOperationAction(ISD::FLOG2, VT, Expand);
443 setOperationAction(ISD::FEXP, VT, Expand);
444 setOperationAction(ISD::FEXP2, VT, Expand);
445 setOperationAction(ISD::FSIN, VT, Expand);
446 setOperationAction(ISD::FCOS, VT, Expand);
447 setOperationAction(ISD::FABS, VT, Expand);
448 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000449 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000450 setOperationAction(ISD::FCEIL, VT, Expand);
451 setOperationAction(ISD::FTRUNC, VT, Expand);
452 setOperationAction(ISD::FRINT, VT, Expand);
453 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000454 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
455 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
456 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
457 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
458 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
459 setOperationAction(ISD::UDIVREM, VT, Expand);
460 setOperationAction(ISD::SDIVREM, VT, Expand);
461 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
462 setOperationAction(ISD::FPOW, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000463 setOperationAction(ISD::BSWAP, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000464 setOperationAction(ISD::CTPOP, VT, Expand);
465 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000466 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000467 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000468 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000469 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000470 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
471
472 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
473 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
474 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
475 setTruncStoreAction(VT, InnerVT, Expand);
476 }
477 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
478 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
479 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000480 }
481
Chris Lattner95c7adc2006-04-04 17:25:31 +0000482 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
483 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000484 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000485
Owen Anderson9f944592009-08-11 20:47:22 +0000486 setOperationAction(ISD::AND , MVT::v4i32, Legal);
487 setOperationAction(ISD::OR , MVT::v4i32, Legal);
488 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
489 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Hal Finkel940ab932014-02-28 00:27:01 +0000490 setOperationAction(ISD::SELECT, MVT::v4i32,
491 Subtarget->useCRBits() ? Legal : Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000492 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000493 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
494 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
495 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
496 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000497 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
498 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
499 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
500 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000501
Craig Topperabadc662012-04-20 06:31:50 +0000502 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
503 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
504 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
505 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000506
Owen Anderson9f944592009-08-11 20:47:22 +0000507 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000508 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000509
Hal Finkel27774d92014-03-13 07:58:58 +0000510 if (TM.Options.UnsafeFPMath || Subtarget->hasVSX()) {
Hal Finkel2e103312013-04-03 04:01:11 +0000511 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
512 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
513 }
514
Owen Anderson9f944592009-08-11 20:47:22 +0000515 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
516 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
517 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000518
Owen Anderson9f944592009-08-11 20:47:22 +0000519 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
520 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000521
Owen Anderson9f944592009-08-11 20:47:22 +0000522 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
523 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
524 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
525 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000526
527 // Altivec does not contain unordered floating-point compare instructions
528 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
529 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
530 setCondCodeAction(ISD::SETUGT, MVT::v4f32, Expand);
531 setCondCodeAction(ISD::SETUGE, MVT::v4f32, Expand);
532 setCondCodeAction(ISD::SETULT, MVT::v4f32, Expand);
533 setCondCodeAction(ISD::SETULE, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000534
535 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
536 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000537
538 if (Subtarget->hasVSX()) {
539 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
Hal Finkel82569b62014-03-27 22:22:48 +0000540 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
Hal Finkel27774d92014-03-13 07:58:58 +0000541
542 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
543 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
544 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
545 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
546 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
547
548 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
549
550 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
551 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
552
553 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
554 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
555
Hal Finkel732f0f72014-03-26 12:49:28 +0000556 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
557 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal);
558 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
559 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
560 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
561
Hal Finkel27774d92014-03-13 07:58:58 +0000562 // Share the Altivec comparison restrictions.
563 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand);
564 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand);
565 setCondCodeAction(ISD::SETUGT, MVT::v2f64, Expand);
566 setCondCodeAction(ISD::SETUGE, MVT::v2f64, Expand);
567 setCondCodeAction(ISD::SETULT, MVT::v2f64, Expand);
568 setCondCodeAction(ISD::SETULE, MVT::v2f64, Expand);
569
570 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand);
571 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand);
572
Hal Finkel9281c9a2014-03-26 18:26:30 +0000573 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
574 setOperationAction(ISD::STORE, MVT::v2f64, Legal);
575
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000576 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal);
577
Hal Finkel19be5062014-03-29 05:29:01 +0000578 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000579
580 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass);
581 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass);
Hal Finkela6c8b512014-03-26 16:12:58 +0000582
583 // VSX v2i64 only supports non-arithmetic operations.
584 setOperationAction(ISD::ADD, MVT::v2i64, Expand);
585 setOperationAction(ISD::SUB, MVT::v2i64, Expand);
586
Hal Finkelad801b72014-03-27 21:26:33 +0000587 setOperationAction(ISD::SHL, MVT::v2i64, Expand);
588 setOperationAction(ISD::SRA, MVT::v2i64, Expand);
589 setOperationAction(ISD::SRL, MVT::v2i64, Expand);
590
Hal Finkel777c9dd2014-03-29 16:04:40 +0000591 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
592
Hal Finkel9281c9a2014-03-26 18:26:30 +0000593 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
594 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64);
595 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
596 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64);
597
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000598 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal);
599
Hal Finkel7279f4b2014-03-26 19:13:54 +0000600 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
601 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
602 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
603 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
604
Hal Finkel5c0d1452014-03-30 13:22:59 +0000605 // Vector operation legalization checks the result type of
606 // SIGN_EXTEND_INREG, overall legalization checks the inner type.
607 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal);
608 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal);
609 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
610 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
611
Hal Finkela6c8b512014-03-26 16:12:58 +0000612 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000613 }
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000614 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000615
Hal Finkel70381a72012-08-04 14:10:46 +0000616 if (Subtarget->has64BitSupport()) {
Hal Finkel322e41a2012-04-01 20:08:17 +0000617 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel70381a72012-08-04 14:10:46 +0000618 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
619 }
Hal Finkel322e41a2012-04-01 20:08:17 +0000620
Eli Friedman7dfa7912011-08-29 18:23:02 +0000621 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
622 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkel1b5ff082012-12-25 17:22:53 +0000623 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
624 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000625
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000626 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidta76bf5a2013-04-23 18:49:44 +0000627 // Altivec instructions set fields to all zeros or all ones.
628 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000629
Evan Cheng39e90022012-07-02 22:39:56 +0000630 if (isPPC64) {
Chris Lattner454436d2006-10-18 01:20:43 +0000631 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000632 setExceptionPointerRegister(PPC::X3);
633 setExceptionSelectorRegister(PPC::X4);
634 } else {
Chris Lattner454436d2006-10-18 01:20:43 +0000635 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000636 setExceptionPointerRegister(PPC::R3);
637 setExceptionSelectorRegister(PPC::R4);
638 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000639
Chris Lattnerf4184352006-03-01 04:57:39 +0000640 // We have target-specific dag combine patterns for the following nodes:
641 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000642 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000643 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000644 setTargetDAGCombine(ISD::BR_CC);
Hal Finkel940ab932014-02-28 00:27:01 +0000645 if (Subtarget->useCRBits())
646 setTargetDAGCombine(ISD::BRCOND);
Chris Lattnera7976d32006-07-10 20:56:58 +0000647 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000648 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000649
Hal Finkel46043ed2014-03-01 21:36:57 +0000650 setTargetDAGCombine(ISD::SIGN_EXTEND);
651 setTargetDAGCombine(ISD::ZERO_EXTEND);
652 setTargetDAGCombine(ISD::ANY_EXTEND);
653
Hal Finkel940ab932014-02-28 00:27:01 +0000654 if (Subtarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000655 setTargetDAGCombine(ISD::TRUNCATE);
656 setTargetDAGCombine(ISD::SETCC);
657 setTargetDAGCombine(ISD::SELECT_CC);
658 }
659
Hal Finkel2e103312013-04-03 04:01:11 +0000660 // Use reciprocal estimates.
661 if (TM.Options.UnsafeFPMath) {
662 setTargetDAGCombine(ISD::FDIV);
663 setTargetDAGCombine(ISD::FSQRT);
664 }
665
Dale Johannesen10432e52007-10-19 00:59:18 +0000666 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng39e90022012-07-02 22:39:56 +0000667 if (Subtarget->isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000668 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000669 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
670 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000671 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
672 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000673 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
674 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
675 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
676 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
677 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000678 }
679
Hal Finkel940ab932014-02-28 00:27:01 +0000680 // With 32 condition bits, we don't need to sink (and duplicate) compares
681 // aggressively in CodeGenPrep.
682 if (Subtarget->useCRBits())
683 setHasMultipleConditionRegisters();
684
Hal Finkel65298572011-10-17 18:53:03 +0000685 setMinFunctionAlignment(2);
686 if (PPCSubTarget.isDarwin())
687 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000688
Evan Cheng39e90022012-07-02 22:39:56 +0000689 if (isPPC64 && Subtarget->isJITCodeModel())
690 // Temporary workaround for the inability of PPC64 JIT to handle jump
691 // tables.
692 setSupportJumpTables(false);
693
Eli Friedman30a49e92011-08-03 21:06:02 +0000694 setInsertFencesForAtomic(true);
695
Hal Finkel21442b22013-09-11 23:05:25 +0000696 if (Subtarget->enableMachineScheduler())
697 setSchedulingPreference(Sched::Source);
698 else
699 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000700
Chris Lattnerf22556d2005-08-16 17:14:42 +0000701 computeRegisterProperties();
Hal Finkel742b5352012-08-28 16:12:39 +0000702
703 // The Freescale cores does better with aggressive inlining of memcpy and
704 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
705 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
706 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000707 MaxStoresPerMemset = 32;
708 MaxStoresPerMemsetOptSize = 16;
709 MaxStoresPerMemcpy = 32;
710 MaxStoresPerMemcpyOptSize = 8;
711 MaxStoresPerMemmove = 32;
712 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel742b5352012-08-28 16:12:39 +0000713
714 setPrefFunctionAlignment(4);
Hal Finkel742b5352012-08-28 16:12:39 +0000715 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000716}
717
Hal Finkel262a2242013-09-12 23:20:06 +0000718/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
719/// the desired ByVal argument alignment.
720static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
721 unsigned MaxMaxAlign) {
722 if (MaxAlign == MaxMaxAlign)
723 return;
724 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
725 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
726 MaxAlign = 32;
727 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
728 MaxAlign = 16;
729 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
730 unsigned EltAlign = 0;
731 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
732 if (EltAlign > MaxAlign)
733 MaxAlign = EltAlign;
734 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
735 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
736 unsigned EltAlign = 0;
737 getMaxByValAlign(STy->getElementType(i), EltAlign, MaxMaxAlign);
738 if (EltAlign > MaxAlign)
739 MaxAlign = EltAlign;
740 if (MaxAlign == MaxMaxAlign)
741 break;
742 }
743 }
744}
745
Dale Johannesencbde4c22008-02-28 22:31:51 +0000746/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
747/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000748unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000749 // Darwin passes everything on 4 byte boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000750 if (PPCSubTarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000751 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000752
753 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000754 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000755 unsigned Align = PPCSubTarget.isPPC64() ? 8 : 4;
756 if (PPCSubTarget.hasAltivec() || PPCSubTarget.hasQPX())
757 getMaxByValAlign(Ty, Align, PPCSubTarget.hasQPX() ? 32 : 16);
758 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000759}
760
Chris Lattner347ed8a2006-01-09 23:52:17 +0000761const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
762 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000763 default: return nullptr;
Evan Cheng32e376f2008-07-12 02:23:19 +0000764 case PPCISD::FSEL: return "PPCISD::FSEL";
765 case PPCISD::FCFID: return "PPCISD::FCFID";
766 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
767 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000768 case PPCISD::FRE: return "PPCISD::FRE";
769 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000770 case PPCISD::STFIWX: return "PPCISD::STFIWX";
771 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
772 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
773 case PPCISD::VPERM: return "PPCISD::VPERM";
774 case PPCISD::Hi: return "PPCISD::Hi";
775 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000776 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller79fef932009-12-18 13:00:15 +0000777 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
778 case PPCISD::LOAD: return "PPCISD::LOAD";
779 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000780 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
781 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
782 case PPCISD::SRL: return "PPCISD::SRL";
783 case PPCISD::SRA: return "PPCISD::SRA";
784 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000785 case PPCISD::CALL: return "PPCISD::CALL";
786 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000787 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000788 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng32e376f2008-07-12 02:23:19 +0000789 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel756810f2013-03-21 21:37:52 +0000790 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
791 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000792 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng32e376f2008-07-12 02:23:19 +0000793 case PPCISD::VCMP: return "PPCISD::VCMP";
794 case PPCISD::VCMPo: return "PPCISD::VCMPo";
795 case PPCISD::LBRX: return "PPCISD::LBRX";
796 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng32e376f2008-07-12 02:23:19 +0000797 case PPCISD::LARX: return "PPCISD::LARX";
798 case PPCISD::STCX: return "PPCISD::STCX";
799 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +0000800 case PPCISD::BDNZ: return "PPCISD::BDNZ";
801 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000802 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000803 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000804 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +0000805 case PPCISD::CR6SET: return "PPCISD::CR6SET";
806 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34627e32012-11-27 17:35:46 +0000807 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
808 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
809 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Roman Divacky32143e22013-12-20 18:08:54 +0000810 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000811 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
812 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000813 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000814 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
815 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
816 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000817 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
818 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
819 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
820 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
821 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +0000822 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +0000823 case PPCISD::SC: return "PPCISD::SC";
Chris Lattner347ed8a2006-01-09 23:52:17 +0000824 }
825}
826
Matt Arsenault758659232013-05-18 00:21:46 +0000827EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000828 if (!VT.isVector())
Hal Finkel940ab932014-02-28 00:27:01 +0000829 return PPCSubTarget.useCRBits() ? MVT::i1 : MVT::i32;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000830 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000831}
832
Chris Lattner4211ca92006-04-14 06:01:58 +0000833//===----------------------------------------------------------------------===//
834// Node matching predicates, for use by the tblgen matching code.
835//===----------------------------------------------------------------------===//
836
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000837/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000838static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000839 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000840 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000841 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000842 // Maybe this has already been legalized into the constant pool?
843 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000844 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000845 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000846 }
847 return false;
848}
849
Chris Lattnere8b83b42006-04-06 17:23:16 +0000850/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
851/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000852static bool isConstantOrUndef(int Op, int Val) {
853 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000854}
855
856/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
857/// VPKUHUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000858bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000859 if (!isUnary) {
860 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000861 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000862 return false;
863 } else {
864 for (unsigned i = 0; i != 8; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000865 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
866 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000867 return false;
868 }
Chris Lattner1d338192006-04-06 18:26:28 +0000869 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000870}
871
872/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
873/// VPKUWUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000874bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000875 if (!isUnary) {
876 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000877 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
878 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000879 return false;
880 } else {
881 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000882 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
883 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
884 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
885 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000886 return false;
887 }
Chris Lattner1d338192006-04-06 18:26:28 +0000888 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000889}
890
Chris Lattnerf38e0332006-04-06 22:02:42 +0000891/// isVMerge - Common function, used to match vmrg* shuffles.
892///
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000893static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +0000894 unsigned LHSStart, unsigned RHSStart) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000895 if (N->getValueType(0) != MVT::v16i8)
896 return false;
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000897 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
898 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000899
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000900 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
901 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000902 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000903 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000904 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000905 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000906 return false;
907 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000908 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +0000909}
910
911/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
912/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000913bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000914 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000915 if (!isUnary)
916 return isVMerge(N, UnitSize, 8, 24);
917 return isVMerge(N, UnitSize, 8, 8);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000918}
919
920/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
921/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000922bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000923 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000924 if (!isUnary)
925 return isVMerge(N, UnitSize, 0, 16);
926 return isVMerge(N, UnitSize, 0, 0);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000927}
928
929
Chris Lattner1d338192006-04-06 18:26:28 +0000930/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
931/// amount, otherwise return -1.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000932int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000933 if (N->getValueType(0) != MVT::v16i8)
Hal Finkela775e512014-04-08 19:00:27 +0000934 return -1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000935
936 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +0000937
Chris Lattner1d338192006-04-06 18:26:28 +0000938 // Find the first non-undef value in the shuffle mask.
939 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000940 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +0000941 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000942
Chris Lattner1d338192006-04-06 18:26:28 +0000943 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000944
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000945 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +0000946 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000947 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +0000948 if (ShiftAmt < i) return -1;
949 ShiftAmt -= i;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000950
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000951 if (!isUnary) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000952 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000953 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000954 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000955 return -1;
956 } else {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000957 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000958 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000959 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000960 return -1;
961 }
Chris Lattner1d338192006-04-06 18:26:28 +0000962 return ShiftAmt;
963}
Chris Lattnerffc47562006-03-20 06:33:01 +0000964
965/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
966/// specifies a splat of a single element that is suitable for input to
967/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000968bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +0000969 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +0000970 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000971
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000972 // This is a splat operation if each element of the permute is the same, and
973 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000974 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +0000975
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000976 // FIXME: Handle UNDEF elements too!
977 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +0000978 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000979
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000980 // Check that the indices are consecutive, in the case of a multi-byte element
981 // splatted with a v16i8 mask.
982 for (unsigned i = 1; i != EltSize; ++i)
983 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000984 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000985
Chris Lattner95c7adc2006-04-04 17:25:31 +0000986 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000987 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +0000988 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000989 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000990 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000991 }
Chris Lattner95c7adc2006-04-04 17:25:31 +0000992 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +0000993}
994
Evan Cheng581d2792007-07-30 07:51:22 +0000995/// isAllNegativeZeroVector - Returns true if all elements of build_vector
996/// are -0.0.
997bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000998 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
999
1000 APInt APVal, APUndef;
1001 unsigned BitSize;
1002 bool HasAnyUndefs;
Wesley Peck527da1b2010-11-23 03:31:01 +00001003
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00001004 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001005 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001006 return CFP->getValueAPF().isNegZero();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001007
Evan Cheng581d2792007-07-30 07:51:22 +00001008 return false;
1009}
1010
Chris Lattnerffc47562006-03-20 06:33:01 +00001011/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
1012/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner95c7adc2006-04-04 17:25:31 +00001013unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001014 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1015 assert(isSplatShuffleMask(SVOp, EltSize));
1016 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +00001017}
1018
Chris Lattner74cf9ff2006-04-12 17:37:20 +00001019/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001020/// by using a vspltis[bhw] instruction of the specified element size, return
1021/// the constant being splatted. The ByteSize field indicates the number of
1022/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001023SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001024 SDValue OpVal(nullptr, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001025
1026 // If ByteSize of the splat is bigger than the element size of the
1027 // build_vector, then we have a case where we are checking for a splat where
1028 // multiple elements of the buildvector are folded together into a single
1029 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
1030 unsigned EltSize = 16/N->getNumOperands();
1031 if (EltSize < ByteSize) {
1032 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001033 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001034 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001035
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001036 // See if all of the elements in the buildvector agree across.
1037 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1038 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1039 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001040 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001041
Scott Michelcf0da6c2009-02-17 22:15:04 +00001042
Craig Topper062a2ba2014-04-25 05:30:21 +00001043 if (!UniquedVals[i&(Multiple-1)].getNode())
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001044 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
1045 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001046 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001047 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001048
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001049 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
1050 // either constant or undef values that are identical for each chunk. See
1051 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001052
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001053 // Check to see if all of the leading entries are either 0 or -1. If
1054 // neither, then this won't fit into the immediate field.
1055 bool LeadingZero = true;
1056 bool LeadingOnes = true;
1057 for (unsigned i = 0; i != Multiple-1; ++i) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001058 if (!UniquedVals[i].getNode()) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001059
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001060 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
1061 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
1062 }
1063 // Finally, check the least significant entry.
1064 if (LeadingZero) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001065 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001066 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00001067 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001068 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +00001069 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001070 }
1071 if (LeadingOnes) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001072 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001073 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +00001074 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001075 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +00001076 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001077 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001078
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001079 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001080 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001081
Chris Lattner2771e2c2006-03-25 06:12:06 +00001082 // Check to see if this buildvec has a single non-undef value in its elements.
1083 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1084 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Craig Topper062a2ba2014-04-25 05:30:21 +00001085 if (!OpVal.getNode())
Chris Lattner2771e2c2006-03-25 06:12:06 +00001086 OpVal = N->getOperand(i);
1087 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001088 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001089 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001090
Craig Topper062a2ba2014-04-25 05:30:21 +00001091 if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001092
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001093 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001094 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001095 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001096 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001097 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001098 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001099 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001100 }
1101
1102 // If the splat value is larger than the element value, then we can never do
1103 // this splat. The only case that we could fit the replicated bits into our
1104 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001105 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001106
Chris Lattner2771e2c2006-03-25 06:12:06 +00001107 // If the element value is larger than the splat value, cut it in half and
1108 // check to see if the two halves are equal. Continue doing this until we
1109 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
1110 while (ValSizeInBytes > ByteSize) {
1111 ValSizeInBytes >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001112
Chris Lattner2771e2c2006-03-25 06:12:06 +00001113 // If the top half equals the bottom half, we're still ok.
Chris Lattner39cc7172006-04-05 17:39:25 +00001114 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
1115 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001116 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001117 }
1118
1119 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001120 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001121
Evan Chengb1ddc982006-03-26 09:52:32 +00001122 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001123 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001124
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001125 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001126 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +00001127 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001128 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001129}
1130
Chris Lattner4211ca92006-04-14 06:01:58 +00001131//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001132// Addressing Mode Selection
1133//===----------------------------------------------------------------------===//
1134
1135/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1136/// or 64-bit immediate, and if the value can be accurately represented as a
1137/// sign extension from a 16-bit value. If so, this returns true and the
1138/// immediate.
1139static bool isIntS16Immediate(SDNode *N, short &Imm) {
Adam Nemet571eb5f2014-05-20 17:20:34 +00001140 if (!isa<ConstantSDNode>(N))
Chris Lattnera801fced2006-11-08 02:15:41 +00001141 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001142
Dan Gohmaneffb8942008-09-12 16:56:44 +00001143 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001144 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001145 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001146 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001147 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001148}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001149static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001150 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001151}
1152
1153
1154/// SelectAddressRegReg - Given the specified addressed, check to see if it
1155/// can be represented as an indexed [r+r] operation. Returns false if it
1156/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001157bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1158 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001159 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001160 short imm = 0;
1161 if (N.getOpcode() == ISD::ADD) {
1162 if (isIntS16Immediate(N.getOperand(1), imm))
1163 return false; // r+i
1164 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1165 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001166
Chris Lattnera801fced2006-11-08 02:15:41 +00001167 Base = N.getOperand(0);
1168 Index = N.getOperand(1);
1169 return true;
1170 } else if (N.getOpcode() == ISD::OR) {
1171 if (isIntS16Immediate(N.getOperand(1), imm))
1172 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001173
Chris Lattnera801fced2006-11-08 02:15:41 +00001174 // If this is an or of disjoint bitfields, we can codegen this as an add
1175 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1176 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001177 APInt LHSKnownZero, LHSKnownOne;
1178 APInt RHSKnownZero, RHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001179 DAG.computeKnownBits(N.getOperand(0),
1180 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001181
Dan Gohmanf19609a2008-02-27 01:23:58 +00001182 if (LHSKnownZero.getBoolValue()) {
Jay Foada0653a32014-05-14 21:14:37 +00001183 DAG.computeKnownBits(N.getOperand(1),
1184 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001185 // If all of the bits are known zero on the LHS or RHS, the add won't
1186 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001187 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001188 Base = N.getOperand(0);
1189 Index = N.getOperand(1);
1190 return true;
1191 }
1192 }
1193 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001194
Chris Lattnera801fced2006-11-08 02:15:41 +00001195 return false;
1196}
1197
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001198// If we happen to be doing an i64 load or store into a stack slot that has
1199// less than a 4-byte alignment, then the frame-index elimination may need to
1200// use an indexed load or store instruction (because the offset may not be a
1201// multiple of 4). The extra register needed to hold the offset comes from the
1202// register scavenger, and it is possible that the scavenger will need to use
1203// an emergency spill slot. As a result, we need to make sure that a spill slot
1204// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1205// stack slot.
1206static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1207 // FIXME: This does not handle the LWA case.
1208 if (VT != MVT::i64)
1209 return;
1210
Hal Finkel7ab3db52013-07-10 15:29:01 +00001211 // NOTE: We'll exclude negative FIs here, which come from argument
1212 // lowering, because there are no known test cases triggering this problem
1213 // using packed structures (or similar). We can remove this exclusion if
1214 // we find such a test case. The reason why this is so test-case driven is
1215 // because this entire 'fixup' is only to prevent crashes (from the
1216 // register scavenger) on not-really-valid inputs. For example, if we have:
1217 // %a = alloca i1
1218 // %b = bitcast i1* %a to i64*
1219 // store i64* a, i64 b
1220 // then the store should really be marked as 'align 1', but is not. If it
1221 // were marked as 'align 1' then the indexed form would have been
1222 // instruction-selected initially, and the problem this 'fixup' is preventing
1223 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001224 if (FrameIdx < 0)
1225 return;
1226
1227 MachineFunction &MF = DAG.getMachineFunction();
1228 MachineFrameInfo *MFI = MF.getFrameInfo();
1229
1230 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1231 if (Align >= 4)
1232 return;
1233
1234 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1235 FuncInfo->setHasNonRISpills();
1236}
1237
Chris Lattnera801fced2006-11-08 02:15:41 +00001238/// Returns true if the address N can be represented by a base register plus
1239/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001240/// represented as reg+reg. If Aligned is true, only accept displacements
1241/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001242bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001243 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001244 SelectionDAG &DAG,
1245 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001246 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001247 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001248 // If this can be more profitably realized as r+r, fail.
1249 if (SelectAddressRegReg(N, Disp, Base, DAG))
1250 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001251
Chris Lattnera801fced2006-11-08 02:15:41 +00001252 if (N.getOpcode() == ISD::ADD) {
1253 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001254 if (isIntS16Immediate(N.getOperand(1), imm) &&
1255 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001256 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001257 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1258 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001259 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001260 } else {
1261 Base = N.getOperand(0);
1262 }
1263 return true; // [r+i]
1264 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1265 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001266 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001267 && "Cannot handle constant offsets yet!");
1268 Disp = N.getOperand(1).getOperand(0); // The global address.
1269 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001270 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001271 Disp.getOpcode() == ISD::TargetConstantPool ||
1272 Disp.getOpcode() == ISD::TargetJumpTable);
1273 Base = N.getOperand(0);
1274 return true; // [&g+r]
1275 }
1276 } else if (N.getOpcode() == ISD::OR) {
1277 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001278 if (isIntS16Immediate(N.getOperand(1), imm) &&
1279 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001280 // If this is an or of disjoint bitfields, we can codegen this as an add
1281 // (for better address arithmetic) if the LHS and RHS of the OR are
1282 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001283 APInt LHSKnownZero, LHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001284 DAG.computeKnownBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001285
Dan Gohmanf19609a2008-02-27 01:23:58 +00001286 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001287 // If all of the bits are known zero on the LHS or RHS, the add won't
1288 // carry.
1289 Base = N.getOperand(0);
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001290 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001291 return true;
1292 }
1293 }
1294 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1295 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001296
Chris Lattnera801fced2006-11-08 02:15:41 +00001297 // If this address fits entirely in a 16-bit sext immediate field, codegen
1298 // this as "d, 0"
1299 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001300 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001301 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkelf70c41e2013-03-21 23:45:03 +00001302 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1303 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001304 return true;
1305 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001306
1307 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001308 if ((CN->getValueType(0) == MVT::i32 ||
1309 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1310 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001311 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001312
Chris Lattnera801fced2006-11-08 02:15:41 +00001313 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001314 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001315
Owen Anderson9f944592009-08-11 20:47:22 +00001316 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1317 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001318 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001319 return true;
1320 }
1321 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001322
Chris Lattnera801fced2006-11-08 02:15:41 +00001323 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001324 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001325 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001326 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1327 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001328 Base = N;
1329 return true; // [r+0]
1330}
1331
1332/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1333/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001334bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1335 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001336 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001337 // Check to see if we can easily represent this as an [r+r] address. This
1338 // will fail if it thinks that the address is more profitably represented as
1339 // reg+imm, e.g. where imm = 0.
1340 if (SelectAddressRegReg(N, Base, Index, DAG))
1341 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001342
Chris Lattnera801fced2006-11-08 02:15:41 +00001343 // If the operand is an addition, always emit this as [r+r], since this is
1344 // better (for code size, and execution, as the memop does the add for free)
1345 // than emitting an explicit add.
1346 if (N.getOpcode() == ISD::ADD) {
1347 Base = N.getOperand(0);
1348 Index = N.getOperand(1);
1349 return true;
1350 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001351
Chris Lattnera801fced2006-11-08 02:15:41 +00001352 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkelf70c41e2013-03-21 23:45:03 +00001353 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1354 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001355 Index = N;
1356 return true;
1357}
1358
Chris Lattnera801fced2006-11-08 02:15:41 +00001359/// getPreIndexedAddressParts - returns true by value, base pointer and
1360/// offset pointer and addressing mode by reference if the node's address
1361/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001362bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1363 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001364 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001365 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001366 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001367
Ulrich Weigande90b0222013-03-22 14:58:48 +00001368 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001369 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001370 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001371 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001372 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1373 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001374 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001375 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001376 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001377 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001378 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001379 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001380 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001381 } else
1382 return false;
1383
Chris Lattner68371252006-11-14 01:38:31 +00001384 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands13237ac2008-06-06 12:08:01 +00001385 if (VT.isVector())
Chris Lattner68371252006-11-14 01:38:31 +00001386 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001387
Ulrich Weigande90b0222013-03-22 14:58:48 +00001388 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1389
1390 // Common code will reject creating a pre-inc form if the base pointer
1391 // is a frame index, or if N is a store and the base pointer is either
1392 // the same as or a predecessor of the value being stored. Check for
1393 // those situations here, and try with swapped Base/Offset instead.
1394 bool Swap = false;
1395
1396 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1397 Swap = true;
1398 else if (!isLoad) {
1399 SDValue Val = cast<StoreSDNode>(N)->getValue();
1400 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1401 Swap = true;
1402 }
1403
1404 if (Swap)
1405 std::swap(Base, Offset);
1406
Hal Finkelca542be2012-06-20 15:43:03 +00001407 AM = ISD::PRE_INC;
1408 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001409 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001410
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001411 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001412 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001413 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001414 return false;
1415 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001416 // LDU/STU need an address with at least 4-byte alignment.
1417 if (Alignment < 4)
1418 return false;
1419
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001420 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001421 return false;
1422 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001423
Chris Lattnerb314b152006-11-11 00:08:42 +00001424 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001425 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1426 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001427 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001428 LD->getExtensionType() == ISD::SEXTLOAD &&
1429 isa<ConstantSDNode>(Offset))
1430 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001431 }
1432
Chris Lattnerce645542006-11-10 02:08:47 +00001433 AM = ISD::PRE_INC;
1434 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001435}
1436
1437//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001438// LowerOperation implementation
1439//===----------------------------------------------------------------------===//
1440
Chris Lattneredb9d842010-11-15 02:46:57 +00001441/// GetLabelAccessInfo - Return true if we should reference labels using a
1442/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1443static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Craig Topper062a2ba2014-04-25 05:30:21 +00001444 unsigned &LoOpFlags,
1445 const GlobalValue *GV = nullptr) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001446 HiOpFlags = PPCII::MO_HA;
1447 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001448
Chris Lattneredb9d842010-11-15 02:46:57 +00001449 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1450 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peck527da1b2010-11-23 03:31:01 +00001451 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattneredb9d842010-11-15 02:46:57 +00001452 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattnerdd6df842010-11-15 03:13:19 +00001453 if (isPIC) {
1454 HiOpFlags |= PPCII::MO_PIC_FLAG;
1455 LoOpFlags |= PPCII::MO_PIC_FLAG;
1456 }
1457
1458 // If this is a reference to a global value that requires a non-lazy-ptr, make
1459 // sure that instruction lowering adds it.
1460 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1461 HiOpFlags |= PPCII::MO_NLP_FLAG;
1462 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001463
Chris Lattnerdd6df842010-11-15 03:13:19 +00001464 if (GV->hasHiddenVisibility()) {
1465 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1466 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1467 }
1468 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001469
Chris Lattneredb9d842010-11-15 02:46:57 +00001470 return isPIC;
1471}
1472
1473static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1474 SelectionDAG &DAG) {
1475 EVT PtrVT = HiPart.getValueType();
1476 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001477 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001478
1479 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1480 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001481
Chris Lattneredb9d842010-11-15 02:46:57 +00001482 // With PIC, the first instruction is actually "GR+hi(&G)".
1483 if (isPIC)
1484 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1485 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001486
Chris Lattneredb9d842010-11-15 02:46:57 +00001487 // Generate non-pic code that has direct accesses to the constant pool.
1488 // The address of the global is just (hi(&g)+lo(&g)).
1489 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1490}
1491
Scott Michelcf0da6c2009-02-17 22:15:04 +00001492SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001493 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001494 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001495 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001496 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001497
Roman Divackyace47072012-08-24 16:26:02 +00001498 // 64-bit SVR4 ABI code is always position-independent.
1499 // The actual address of the GlobalValue is stored in the TOC.
1500 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1501 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001502 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001503 DAG.getRegister(PPC::X2, MVT::i64));
1504 }
1505
Chris Lattneredb9d842010-11-15 02:46:57 +00001506 unsigned MOHiFlag, MOLoFlag;
1507 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1508 SDValue CPIHi =
1509 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1510 SDValue CPILo =
1511 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1512 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001513}
1514
Dan Gohman21cea8a2010-04-17 15:26:15 +00001515SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001516 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001517 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001518
Roman Divackyace47072012-08-24 16:26:02 +00001519 // 64-bit SVR4 ABI code is always position-independent.
1520 // The actual address of the GlobalValue is stored in the TOC.
1521 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1522 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001523 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001524 DAG.getRegister(PPC::X2, MVT::i64));
1525 }
1526
Chris Lattneredb9d842010-11-15 02:46:57 +00001527 unsigned MOHiFlag, MOLoFlag;
1528 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1529 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1530 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1531 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001532}
1533
Dan Gohman21cea8a2010-04-17 15:26:15 +00001534SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1535 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001536 EVT PtrVT = Op.getValueType();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001537
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001538 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peck527da1b2010-11-23 03:31:01 +00001539
Chris Lattneredb9d842010-11-15 02:46:57 +00001540 unsigned MOHiFlag, MOLoFlag;
1541 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001542 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1543 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001544 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1545}
1546
Roman Divackye3f15c982012-06-04 17:36:38 +00001547SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1548 SelectionDAG &DAG) const {
1549
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001550 // FIXME: TLS addresses currently use medium model code sequences,
1551 // which is the most useful form. Eventually support for small and
1552 // large models could be added if users need it, at the cost of
1553 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001554 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001555 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001556 const GlobalValue *GV = GA->getGlobal();
1557 EVT PtrVT = getPointerTy();
1558 bool is64bit = PPCSubTarget.isPPC64();
1559
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001560 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001561
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001562 if (Model == TLSModel::LocalExec) {
1563 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001564 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001565 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001566 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001567 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1568 is64bit ? MVT::i64 : MVT::i32);
1569 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1570 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1571 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001572
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001573 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001574 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001575 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1576 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00001577 SDValue GOTPtr;
1578 if (is64bit) {
1579 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1580 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1581 PtrVT, GOTReg, TGA);
1582 } else
1583 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001584 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00001585 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001586 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001587 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001588
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001589 if (Model == TLSModel::GeneralDynamic) {
1590 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1591 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1592 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1593 GOTReg, TGA);
1594 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1595 GOTEntryHi, TGA);
1596
1597 // We need a chain node, and don't have one handy. The underlying
1598 // call has no side effects, so using the function entry node
1599 // suffices.
1600 SDValue Chain = DAG.getEntryNode();
1601 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1602 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1603 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1604 PtrVT, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001605 // The return value from GET_TLS_ADDR really is in X3 already, but
1606 // some hacks are needed here to tie everything together. The extra
1607 // copies dissolve during subsequent transforms.
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001608 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1609 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1610 }
1611
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001612 if (Model == TLSModel::LocalDynamic) {
1613 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1614 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1615 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1616 GOTReg, TGA);
1617 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1618 GOTEntryHi, TGA);
1619
1620 // We need a chain node, and don't have one handy. The underlying
1621 // call has no side effects, so using the function entry node
1622 // suffices.
1623 SDValue Chain = DAG.getEntryNode();
1624 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1625 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1626 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1627 PtrVT, ParmReg, TGA);
1628 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1629 // some hacks are needed here to tie everything together. The extra
1630 // copies dissolve during subsequent transforms.
1631 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1632 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt9ed4dbc2012-12-13 20:57:10 +00001633 Chain, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001634 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1635 }
1636
1637 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00001638}
1639
Chris Lattneredb9d842010-11-15 02:46:57 +00001640SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1641 SelectionDAG &DAG) const {
1642 EVT PtrVT = Op.getValueType();
1643 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001644 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00001645 const GlobalValue *GV = GSDN->getGlobal();
1646
Chris Lattneredb9d842010-11-15 02:46:57 +00001647 // 64-bit SVR4 ABI code is always position-independent.
1648 // The actual address of the GlobalValue is stored in the TOC.
1649 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1650 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1651 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1652 DAG.getRegister(PPC::X2, MVT::i64));
1653 }
1654
Chris Lattnerdd6df842010-11-15 03:13:19 +00001655 unsigned MOHiFlag, MOLoFlag;
1656 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00001657
Chris Lattnerdd6df842010-11-15 03:13:19 +00001658 SDValue GAHi =
1659 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1660 SDValue GALo =
1661 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00001662
Chris Lattnerdd6df842010-11-15 03:13:19 +00001663 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00001664
Chris Lattnerdd6df842010-11-15 03:13:19 +00001665 // If the global reference is actually to a non-lazy-pointer, we have to do an
1666 // extra load to get the address of the global.
1667 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1668 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001669 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00001670 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00001671}
1672
Dan Gohman21cea8a2010-04-17 15:26:15 +00001673SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00001674 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001675 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001676
Hal Finkel777c9dd2014-03-29 16:04:40 +00001677 if (Op.getValueType() == MVT::v2i64) {
1678 // When the operands themselves are v2i64 values, we need to do something
1679 // special because VSX has no underlying comparison operations for these.
1680 if (Op.getOperand(0).getValueType() == MVT::v2i64) {
1681 // Equality can be handled by casting to the legal type for Altivec
1682 // comparisons, everything else needs to be expanded.
1683 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
1684 return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
1685 DAG.getSetCC(dl, MVT::v4i32,
1686 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)),
1687 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)),
1688 CC));
1689 }
1690
1691 return SDValue();
1692 }
1693
1694 // We handle most of these in the usual way.
1695 return Op;
1696 }
1697
Chris Lattner4211ca92006-04-14 06:01:58 +00001698 // If we're comparing for equality to zero, expose the fact that this is
1699 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1700 // fold the new nodes.
1701 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1702 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001703 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001704 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001705 if (VT.bitsLT(MVT::i32)) {
1706 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001707 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001708 }
Duncan Sands13237ac2008-06-06 12:08:01 +00001709 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001710 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1711 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00001712 DAG.getConstant(Log2b, MVT::i32));
1713 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00001714 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001715 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00001716 // optimized. FIXME: revisit this when we can custom lower all setcc
1717 // optimizations.
1718 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001719 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001720 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001721
Chris Lattner4211ca92006-04-14 06:01:58 +00001722 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00001723 // by xor'ing the rhs with the lhs, which is faster than setting a
1724 // condition register, reading it back out, and masking the correct bit. The
1725 // normal approach here uses sub to do this instead of xor. Using xor exposes
1726 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001727 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00001728 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001729 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001730 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00001731 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001732 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00001733 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001734 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001735}
1736
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001737SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001738 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00001739 SDNode *Node = Op.getNode();
1740 EVT VT = Node->getValueType(0);
1741 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1742 SDValue InChain = Node->getOperand(0);
1743 SDValue VAListPtr = Node->getOperand(1);
1744 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001745 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001746
Roman Divacky4394e682011-06-28 15:30:42 +00001747 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1748
1749 // gpr_index
1750 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1751 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1752 false, false, 0);
1753 InChain = GprIndex.getValue(1);
1754
1755 if (VT == MVT::i64) {
1756 // Check if GprIndex is even
1757 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1758 DAG.getConstant(1, MVT::i32));
1759 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1760 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1761 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1762 DAG.getConstant(1, MVT::i32));
1763 // Align GprIndex to be even if it isn't
1764 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1765 GprIndex);
1766 }
1767
1768 // fpr index is 1 byte after gpr
1769 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1770 DAG.getConstant(1, MVT::i32));
1771
1772 // fpr
1773 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1774 FprPtr, MachinePointerInfo(SV), MVT::i8,
1775 false, false, 0);
1776 InChain = FprIndex.getValue(1);
1777
1778 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1779 DAG.getConstant(8, MVT::i32));
1780
1781 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1782 DAG.getConstant(4, MVT::i32));
1783
1784 // areas
1785 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001786 MachinePointerInfo(), false, false,
1787 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001788 InChain = OverflowArea.getValue(1);
1789
1790 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001791 MachinePointerInfo(), false, false,
1792 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001793 InChain = RegSaveArea.getValue(1);
1794
1795 // select overflow_area if index > 8
1796 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1797 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1798
Roman Divacky4394e682011-06-28 15:30:42 +00001799 // adjustment constant gpr_index * 4/8
1800 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1801 VT.isInteger() ? GprIndex : FprIndex,
1802 DAG.getConstant(VT.isInteger() ? 4 : 8,
1803 MVT::i32));
1804
1805 // OurReg = RegSaveArea + RegConstant
1806 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1807 RegConstant);
1808
1809 // Floating types are 32 bytes into RegSaveArea
1810 if (VT.isFloatingPoint())
1811 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1812 DAG.getConstant(32, MVT::i32));
1813
1814 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1815 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1816 VT.isInteger() ? GprIndex : FprIndex,
1817 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1818 MVT::i32));
1819
1820 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1821 VT.isInteger() ? VAListPtr : FprPtr,
1822 MachinePointerInfo(SV),
1823 MVT::i8, false, false, 0);
1824
1825 // determine if we should load from reg_save_area or overflow_area
1826 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1827
1828 // increase overflow_area by 4/8 if gpr/fpr > 8
1829 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1830 DAG.getConstant(VT.isInteger() ? 4 : 8,
1831 MVT::i32));
1832
1833 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1834 OverflowAreaPlusN);
1835
1836 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1837 OverflowAreaPtr,
1838 MachinePointerInfo(),
1839 MVT::i32, false, false, 0);
1840
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00001841 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001842 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001843}
1844
Roman Divackyc3825df2013-07-25 21:36:47 +00001845SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
1846 const PPCSubtarget &Subtarget) const {
1847 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
1848
1849 // We have to copy the entire va_list struct:
1850 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
1851 return DAG.getMemcpy(Op.getOperand(0), Op,
1852 Op.getOperand(1), Op.getOperand(2),
1853 DAG.getConstant(12, MVT::i32), 8, false, true,
1854 MachinePointerInfo(), MachinePointerInfo());
1855}
1856
Duncan Sandsa0984362011-09-06 13:37:06 +00001857SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1858 SelectionDAG &DAG) const {
1859 return Op.getOperand(0);
1860}
1861
1862SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1863 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00001864 SDValue Chain = Op.getOperand(0);
1865 SDValue Trmp = Op.getOperand(1); // trampoline
1866 SDValue FPtr = Op.getOperand(2); // nested function
1867 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00001868 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00001869
Owen Anderson53aa7a92009-08-10 22:56:29 +00001870 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00001871 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00001872 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001873 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00001874 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00001875
Scott Michelcf0da6c2009-02-17 22:15:04 +00001876 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00001877 TargetLowering::ArgListEntry Entry;
1878
1879 Entry.Ty = IntPtrTy;
1880 Entry.Node = Trmp; Args.push_back(Entry);
1881
1882 // TrampSize == (isPPC64 ? 48 : 40);
1883 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00001884 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00001885 Args.push_back(Entry);
1886
1887 Entry.Node = FPtr; Args.push_back(Entry);
1888 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001889
Bill Wendling95e1af22008-09-17 00:30:57 +00001890 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001891 TargetLowering::CallLoweringInfo CLI(DAG);
1892 CLI.setDebugLoc(dl).setChain(Chain)
1893 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
1894 DAG.getExternalSymbol("__trampoline_setup", PtrVT), &Args, 0);
Bill Wendling95e1af22008-09-17 00:30:57 +00001895
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001896 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Duncan Sandsa0984362011-09-06 13:37:06 +00001897 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00001898}
1899
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001900SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001901 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001902 MachineFunction &MF = DAG.getMachineFunction();
1903 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1904
Andrew Trickef9de2a2013-05-25 02:42:55 +00001905 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001906
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001907 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001908 // vastart just stores the address of the VarArgsFrameIndex slot into the
1909 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001910 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00001911 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001912 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00001913 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1914 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00001915 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001916 }
1917
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001918 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001919 // We suppose the given va_list is already allocated.
1920 //
1921 // typedef struct {
1922 // char gpr; /* index into the array of 8 GPRs
1923 // * stored in the register save area
1924 // * gpr=0 corresponds to r3,
1925 // * gpr=1 to r4, etc.
1926 // */
1927 // char fpr; /* index into the array of 8 FPRs
1928 // * stored in the register save area
1929 // * fpr=0 corresponds to f1,
1930 // * fpr=1 to f2, etc.
1931 // */
1932 // char *overflow_arg_area;
1933 // /* location on stack that holds
1934 // * the next overflow argument
1935 // */
1936 // char *reg_save_area;
1937 // /* where r3:r10 and f1:f8 (if saved)
1938 // * are stored
1939 // */
1940 // } va_list[1];
1941
1942
Dan Gohman31ae5862010-04-17 14:41:14 +00001943 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1944 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001945
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001946
Owen Anderson53aa7a92009-08-10 22:56:29 +00001947 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001948
Dan Gohman31ae5862010-04-17 14:41:14 +00001949 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1950 PtrVT);
1951 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1952 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001953
Duncan Sands13237ac2008-06-06 12:08:01 +00001954 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001955 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001956
Duncan Sands13237ac2008-06-06 12:08:01 +00001957 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001958 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001959
1960 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001961 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001962
Dan Gohman2d489b52008-02-06 22:27:42 +00001963 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001964
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001965 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001966 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00001967 Op.getOperand(1),
1968 MachinePointerInfo(SV),
1969 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001970 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001971 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001972 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001973
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001974 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001975 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00001976 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1977 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00001978 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001979 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001980 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001981
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001982 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001983 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00001984 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1985 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001986 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001987 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001988 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001989
1990 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00001991 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1992 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001993 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001994
Chris Lattner4211ca92006-04-14 06:01:58 +00001995}
1996
Chris Lattner4f2e4e02007-03-06 00:59:59 +00001997#include "PPCGenCallingConv.inc"
1998
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001999// Function whose sole purpose is to kill compiler warnings
2000// stemming from unused functions included from PPCGenCallingConv.inc.
2001CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00002002 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002003}
2004
Bill Schmidt230b4512013-06-12 16:39:22 +00002005bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
2006 CCValAssign::LocInfo &LocInfo,
2007 ISD::ArgFlagsTy &ArgFlags,
2008 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002009 return true;
2010}
2011
Bill Schmidt230b4512013-06-12 16:39:22 +00002012bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
2013 MVT &LocVT,
2014 CCValAssign::LocInfo &LocInfo,
2015 ISD::ArgFlagsTy &ArgFlags,
2016 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002017 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002018 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2019 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2020 };
2021 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002022
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002023 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
2024
2025 // Skip one register if the first unallocated register has an even register
2026 // number and there are still argument registers available which have not been
2027 // allocated yet. RegNum is actually an index into ArgRegs, which means we
2028 // need to skip a register if RegNum is odd.
2029 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
2030 State.AllocateReg(ArgRegs[RegNum]);
2031 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002032
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002033 // Always return false here, as this function only makes sure that the first
2034 // unallocated register has an odd register number and does not actually
2035 // allocate a register for the current argument.
2036 return false;
2037}
2038
Bill Schmidt230b4512013-06-12 16:39:22 +00002039bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
2040 MVT &LocVT,
2041 CCValAssign::LocInfo &LocInfo,
2042 ISD::ArgFlagsTy &ArgFlags,
2043 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002044 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002045 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2046 PPC::F8
2047 };
2048
2049 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002050
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002051 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
2052
2053 // If there is only one Floating-point register left we need to put both f64
2054 // values of a split ppc_fp128 value on the stack.
2055 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
2056 State.AllocateReg(ArgRegs[RegNum]);
2057 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002058
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002059 // Always return false here, as this function only makes sure that the two f64
2060 // values a ppc_fp128 value is split into are both passed in registers or both
2061 // passed on the stack and does not actually allocate a register for the
2062 // current argument.
2063 return false;
2064}
2065
Chris Lattner43df5b32007-02-25 05:34:32 +00002066/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002067/// on Darwin.
Craig Topper840beec2014-04-04 05:16:06 +00002068static const MCPhysReg *GetFPR() {
2069 static const MCPhysReg FPR[] = {
Chris Lattner43df5b32007-02-25 05:34:32 +00002070 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002071 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner43df5b32007-02-25 05:34:32 +00002072 };
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002073
Chris Lattner43df5b32007-02-25 05:34:32 +00002074 return FPR;
2075}
2076
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002077/// CalculateStackSlotSize - Calculates the size reserved for this argument on
2078/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002079static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002080 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00002081 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002082 if (Flags.isByVal())
2083 ArgSize = Flags.getByValSize();
2084 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2085
2086 return ArgSize;
2087}
2088
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002089SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002090PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002091 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002092 const SmallVectorImpl<ISD::InputArg>
2093 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002094 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002095 SmallVectorImpl<SDValue> &InVals)
2096 const {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002097 if (PPCSubTarget.isSVR4ABI()) {
2098 if (PPCSubTarget.isPPC64())
2099 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
2100 dl, DAG, InVals);
2101 else
2102 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2103 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002104 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002105 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2106 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002107 }
2108}
2109
2110SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002111PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002112 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002113 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002114 const SmallVectorImpl<ISD::InputArg>
2115 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002116 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002117 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002118
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002119 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002120 // +-----------------------------------+
2121 // +--> | Back chain |
2122 // | +-----------------------------------+
2123 // | | Floating-point register save area |
2124 // | +-----------------------------------+
2125 // | | General register save area |
2126 // | +-----------------------------------+
2127 // | | CR save word |
2128 // | +-----------------------------------+
2129 // | | VRSAVE save word |
2130 // | +-----------------------------------+
2131 // | | Alignment padding |
2132 // | +-----------------------------------+
2133 // | | Vector register save area |
2134 // | +-----------------------------------+
2135 // | | Local variable space |
2136 // | +-----------------------------------+
2137 // | | Parameter list area |
2138 // | +-----------------------------------+
2139 // | | LR save word |
2140 // | +-----------------------------------+
2141 // SP--> +--- | Back chain |
2142 // +-----------------------------------+
2143 //
2144 // Specifications:
2145 // System V Application Binary Interface PowerPC Processor Supplement
2146 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002147
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002148 MachineFunction &MF = DAG.getMachineFunction();
2149 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002150 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002151
Owen Anderson53aa7a92009-08-10 22:56:29 +00002152 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002153 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002154 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2155 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002156 unsigned PtrByteSize = 4;
2157
2158 // Assign locations to all of the incoming arguments.
2159 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002160 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002161 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002162
2163 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002164 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002165
Bill Schmidtef17c142013-02-06 17:33:58 +00002166 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002167
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002168 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2169 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002170
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002171 // Arguments stored in registers.
2172 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002173 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002174 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002175
Owen Anderson9f944592009-08-11 20:47:22 +00002176 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002177 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002178 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002179 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002180 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002181 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002182 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002183 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00002184 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002185 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002186 case MVT::f64:
Hal Finkel19be5062014-03-29 05:29:01 +00002187 if (PPCSubTarget.hasVSX())
2188 RC = &PPC::VSFRCRegClass;
2189 else
2190 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002191 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002192 case MVT::v16i8:
2193 case MVT::v8i16:
2194 case MVT::v4i32:
2195 case MVT::v4f32:
Hal Finkel7811c612014-03-28 19:58:11 +00002196 RC = &PPC::VRRCRegClass;
2197 break;
Hal Finkel27774d92014-03-13 07:58:58 +00002198 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002199 case MVT::v2i64:
Hal Finkel7811c612014-03-28 19:58:11 +00002200 RC = &PPC::VSHRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002201 break;
2202 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002203
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002204 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002205 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002206 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2207 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2208
2209 if (ValVT == MVT::i1)
2210 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002211
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002212 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002213 } else {
2214 // Argument stored in memory.
2215 assert(VA.isMemLoc());
2216
Hal Finkel940ab932014-02-28 00:27:01 +00002217 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002218 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002219 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002220
2221 // Create load nodes to retrieve arguments from the stack.
2222 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002223 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2224 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002225 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002226 }
2227 }
2228
2229 // Assign locations to all of the incoming aggregate by value arguments.
2230 // Aggregates passed by value are stored in the local variable space of the
2231 // caller's stack frame, right above the parameter list area.
2232 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002233 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002234 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002235
2236 // Reserve stack space for the allocations in CCInfo.
2237 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2238
Bill Schmidtef17c142013-02-06 17:33:58 +00002239 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002240
2241 // Area that is at least reserved in the caller of this function.
2242 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00002243
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002244 // Set the size that is at least reserved in caller of this function. Tail
2245 // call optimized function's reserved stack space needs to be aligned so that
2246 // taking the difference between two stack areas will result in an aligned
2247 // stack.
2248 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2249
2250 MinReservedArea =
2251 std::max(MinReservedArea,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002252 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peck527da1b2010-11-23 03:31:01 +00002253
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002254 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002255 getStackAlignment();
2256 unsigned AlignMask = TargetAlign-1;
2257 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peck527da1b2010-11-23 03:31:01 +00002258
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002259 FI->setMinReservedArea(MinReservedArea);
2260
2261 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002262
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002263 // If the function takes variable number of arguments, make a frame index for
2264 // the start of the first vararg value... for expansion of llvm.va_start.
2265 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +00002266 static const MCPhysReg GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002267 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2268 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2269 };
2270 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2271
Craig Topper840beec2014-04-04 05:16:06 +00002272 static const MCPhysReg FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002273 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2274 PPC::F8
2275 };
2276 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2277
Dan Gohman31ae5862010-04-17 14:41:14 +00002278 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2279 NumGPArgRegs));
2280 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2281 NumFPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002282
2283 // Make room for NumGPArgRegs and NumFPArgRegs.
2284 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson9f944592009-08-11 20:47:22 +00002285 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002286
Dan Gohman31ae5862010-04-17 14:41:14 +00002287 FuncInfo->setVarArgsStackOffset(
2288 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002289 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002290
Dan Gohman31ae5862010-04-17 14:41:14 +00002291 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2292 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002293
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002294 // The fixed integer arguments of a variadic function are stored to the
2295 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2296 // the result of va_next.
2297 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2298 // Get an existing live-in vreg, or add a new one.
2299 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2300 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002301 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002302
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002303 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002304 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2305 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002306 MemOps.push_back(Store);
2307 // Increment the address by four for the next argument to store
2308 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2309 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2310 }
2311
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002312 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2313 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002314 // The double arguments are stored to the VarArgsFrameIndex
2315 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002316 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2317 // Get an existing live-in vreg, or add a new one.
2318 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2319 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002320 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002321
Owen Anderson9f944592009-08-11 20:47:22 +00002322 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002323 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2324 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002325 MemOps.push_back(Store);
2326 // Increment the address by eight for the next argument to store
Owen Anderson9f944592009-08-11 20:47:22 +00002327 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002328 PtrVT);
2329 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2330 }
2331 }
2332
2333 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002334 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002335
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002336 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002337}
2338
Bill Schmidt57d6de52012-10-23 15:51:16 +00002339// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2340// value to MVT::i64 and then truncate to the correct register size.
2341SDValue
2342PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2343 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002344 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002345 if (Flags.isSExt())
2346 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2347 DAG.getValueType(ObjectVT));
2348 else if (Flags.isZExt())
2349 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2350 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002351
Hal Finkel940ab932014-02-28 00:27:01 +00002352 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002353}
2354
2355// Set the size that is at least reserved in caller of this function. Tail
2356// call optimized functions' reserved stack space needs to be aligned so that
2357// taking the difference between two stack areas will result in an aligned
2358// stack.
2359void
2360PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2361 unsigned nAltivecParamsAtEnd,
2362 unsigned MinReservedArea,
2363 bool isPPC64) const {
2364 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2365 // Add the Altivec parameters at the end, if needed.
2366 if (nAltivecParamsAtEnd) {
2367 MinReservedArea = ((MinReservedArea+15)/16)*16;
2368 MinReservedArea += 16*nAltivecParamsAtEnd;
2369 }
2370 MinReservedArea =
2371 std::max(MinReservedArea,
2372 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2373 unsigned TargetAlign
2374 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2375 getStackAlignment();
2376 unsigned AlignMask = TargetAlign-1;
2377 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2378 FI->setMinReservedArea(MinReservedArea);
2379}
2380
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002381SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002382PPCTargetLowering::LowerFormalArguments_64SVR4(
2383 SDValue Chain,
2384 CallingConv::ID CallConv, bool isVarArg,
2385 const SmallVectorImpl<ISD::InputArg>
2386 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002387 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002388 SmallVectorImpl<SDValue> &InVals) const {
2389 // TODO: add description of PPC stack frame format, or at least some docs.
2390 //
2391 MachineFunction &MF = DAG.getMachineFunction();
2392 MachineFrameInfo *MFI = MF.getFrameInfo();
2393 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2394
2395 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2396 // Potential tail calls could cause overwriting of argument stack slots.
2397 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2398 (CallConv == CallingConv::Fast));
2399 unsigned PtrByteSize = 8;
2400
2401 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2402 // Area that is at least reserved in caller of this function.
2403 unsigned MinReservedArea = ArgOffset;
2404
Craig Topper840beec2014-04-04 05:16:06 +00002405 static const MCPhysReg GPR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002406 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2407 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2408 };
2409
Craig Topper840beec2014-04-04 05:16:06 +00002410 static const MCPhysReg *FPR = GetFPR();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002411
Craig Topper840beec2014-04-04 05:16:06 +00002412 static const MCPhysReg VR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002413 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2414 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2415 };
Craig Topper840beec2014-04-04 05:16:06 +00002416 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00002417 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
2418 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
2419 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002420
2421 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2422 const unsigned Num_FPR_Regs = 13;
2423 const unsigned Num_VR_Regs = array_lengthof(VR);
2424
2425 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2426
2427 // Add DAG nodes to load the arguments or copy them out of registers. On
2428 // entry to a function on PPC, the arguments start after the linkage area,
2429 // although the first ones are often in registers.
2430
2431 SmallVector<SDValue, 8> MemOps;
2432 unsigned nAltivecParamsAtEnd = 0;
2433 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002434 unsigned CurArgIdx = 0;
2435 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002436 SDValue ArgVal;
2437 bool needsLoad = false;
2438 EVT ObjectVT = Ins[ArgNo].VT;
Hal Finkel940ab932014-02-28 00:27:01 +00002439 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002440 unsigned ArgSize = ObjSize;
2441 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt6631e942013-02-20 17:31:41 +00002442 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2443 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002444
2445 unsigned CurArgOffset = ArgOffset;
2446
2447 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2448 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
Hal Finkel27774d92014-03-13 07:58:58 +00002449 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8 ||
Hal Finkela6c8b512014-03-26 16:12:58 +00002450 ObjectVT==MVT::v2f64 || ObjectVT==MVT::v2i64) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002451 if (isVarArg) {
2452 MinReservedArea = ((MinReservedArea+15)/16)*16;
2453 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2454 Flags,
2455 PtrByteSize);
2456 } else
2457 nAltivecParamsAtEnd++;
2458 } else
2459 // Calculate min reserved area.
2460 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2461 Flags,
2462 PtrByteSize);
2463
2464 // FIXME the codegen can be much improved in some cases.
2465 // We do not have to keep everything in memory.
2466 if (Flags.isByVal()) {
2467 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2468 ObjSize = Flags.getByValSize();
2469 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002470 // Empty aggregate parameters do not take up registers. Examples:
2471 // struct { } a;
2472 // union { } b;
2473 // int c[0];
2474 // etc. However, we have to provide a place-holder in InVals, so
2475 // pretend we have an 8-byte item at the current address for that
2476 // purpose.
2477 if (!ObjSize) {
2478 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2479 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2480 InVals.push_back(FIN);
2481 continue;
2482 }
Hal Finkel262a2242013-09-12 23:20:06 +00002483
2484 unsigned BVAlign = Flags.getByValAlign();
2485 if (BVAlign > 8) {
2486 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
2487 CurArgOffset = ArgOffset;
2488 }
2489
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002490 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt48081ca2012-10-16 13:30:53 +00002491 if (ObjSize < PtrByteSize)
2492 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002493 // The value of the object is its address.
2494 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2495 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2496 InVals.push_back(FIN);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002497
2498 if (ObjSize < 8) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002499 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002500 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002501 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002502 SDValue Store;
2503
2504 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2505 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2506 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2507 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002508 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002509 ObjType, false, false, 0);
2510 } else {
2511 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2512 // store the whole register as-is to the parameter save area
2513 // slot. The address of the parameter was already calculated
2514 // above (InVals.push_back(FIN)) to be the right-justified
2515 // offset within the slot. For this store, we need a new
2516 // frame index that points at the beginning of the slot.
2517 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2518 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2519 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002520 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002521 false, false, 0);
2522 }
2523
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002524 MemOps.push_back(Store);
2525 ++GPR_idx;
2526 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002527 // Whether we copied from a register or not, advance the offset
2528 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002529 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002530 continue;
2531 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002532
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002533 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2534 // Store whatever pieces of the object are in registers
2535 // to memory. ArgOffset will be the address of the beginning
2536 // of the object.
2537 if (GPR_idx != Num_GPR_Regs) {
2538 unsigned VReg;
2539 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2540 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2541 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2542 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002543 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002544 MachinePointerInfo(FuncArg, j),
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002545 false, false, 0);
2546 MemOps.push_back(Store);
2547 ++GPR_idx;
2548 ArgOffset += PtrByteSize;
2549 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00002550 ArgOffset += ArgSize - j;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002551 break;
2552 }
2553 }
2554 continue;
2555 }
2556
2557 switch (ObjectVT.getSimpleVT().SimpleTy) {
2558 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00002559 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002560 case MVT::i32:
2561 case MVT::i64:
2562 if (GPR_idx != Num_GPR_Regs) {
2563 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2564 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2565
Hal Finkel940ab932014-02-28 00:27:01 +00002566 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002567 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2568 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002569 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002570
2571 ++GPR_idx;
2572 } else {
2573 needsLoad = true;
2574 ArgSize = PtrByteSize;
2575 }
2576 ArgOffset += 8;
2577 break;
2578
2579 case MVT::f32:
2580 case MVT::f64:
2581 // Every 8 bytes of argument space consumes one of the GPRs available for
2582 // argument passing.
2583 if (GPR_idx != Num_GPR_Regs) {
2584 ++GPR_idx;
2585 }
2586 if (FPR_idx != Num_FPR_Regs) {
2587 unsigned VReg;
2588
2589 if (ObjectVT == MVT::f32)
2590 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2591 else
Hal Finkel19be5062014-03-29 05:29:01 +00002592 VReg = MF.addLiveIn(FPR[FPR_idx], PPCSubTarget.hasVSX() ?
2593 &PPC::VSFRCRegClass :
2594 &PPC::F8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002595
2596 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2597 ++FPR_idx;
2598 } else {
2599 needsLoad = true;
Bill Schmidt22162472012-10-11 15:38:20 +00002600 ArgSize = PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002601 }
2602
2603 ArgOffset += 8;
2604 break;
2605 case MVT::v4f32:
2606 case MVT::v4i32:
2607 case MVT::v8i16:
2608 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00002609 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002610 case MVT::v2i64:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002611 // Note that vector arguments in registers don't reserve stack space,
2612 // except in varargs functions.
2613 if (VR_idx != Num_VR_Regs) {
Hal Finkel7811c612014-03-28 19:58:11 +00002614 unsigned VReg = (ObjectVT == MVT::v2f64 || ObjectVT == MVT::v2i64) ?
2615 MF.addLiveIn(VSRH[VR_idx], &PPC::VSHRCRegClass) :
2616 MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002617 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2618 if (isVarArg) {
2619 while ((ArgOffset % 16) != 0) {
2620 ArgOffset += PtrByteSize;
2621 if (GPR_idx != Num_GPR_Regs)
2622 GPR_idx++;
2623 }
2624 ArgOffset += 16;
2625 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2626 }
2627 ++VR_idx;
2628 } else {
2629 // Vectors are aligned.
2630 ArgOffset = ((ArgOffset+15)/16)*16;
2631 CurArgOffset = ArgOffset;
2632 ArgOffset += 16;
2633 needsLoad = true;
2634 }
2635 break;
2636 }
2637
2638 // We need to load the argument to a virtual register if we determined
2639 // above that we ran out of physical registers of the appropriate type.
2640 if (needsLoad) {
2641 int FI = MFI->CreateFixedObject(ObjSize,
2642 CurArgOffset + (ArgSize - ObjSize),
2643 isImmutable);
2644 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2645 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2646 false, false, false, 0);
2647 }
2648
2649 InVals.push_back(ArgVal);
2650 }
2651
2652 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002653 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002654 // taking the difference between two stack areas will result in an aligned
2655 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002656 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002657
2658 // If the function takes variable number of arguments, make a frame index for
2659 // the start of the first vararg value... for expansion of llvm.va_start.
2660 if (isVarArg) {
2661 int Depth = ArgOffset;
2662
2663 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00002664 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002665 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2666
2667 // If this function is vararg, store any remaining integer argument regs
2668 // to their spots on the stack so that they may be loaded by deferencing the
2669 // result of va_next.
2670 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2671 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2672 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2673 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2674 MachinePointerInfo(), false, false, 0);
2675 MemOps.push_back(Store);
2676 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00002677 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002678 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2679 }
2680 }
2681
2682 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002683 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002684
2685 return Chain;
2686}
2687
2688SDValue
2689PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002690 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002691 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002692 const SmallVectorImpl<ISD::InputArg>
2693 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002694 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002695 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002696 // TODO: add description of PPC stack frame format, or at least some docs.
2697 //
2698 MachineFunction &MF = DAG.getMachineFunction();
2699 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002700 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002701
Owen Anderson53aa7a92009-08-10 22:56:29 +00002702 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002703 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002704 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002705 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2706 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00002707 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey48850c12006-11-16 22:43:37 +00002708
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002709 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002710 // Area that is at least reserved in caller of this function.
2711 unsigned MinReservedArea = ArgOffset;
2712
Craig Topper840beec2014-04-04 05:16:06 +00002713 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002714 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2715 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2716 };
Craig Topper840beec2014-04-04 05:16:06 +00002717 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00002718 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2719 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2720 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00002721
Craig Topper840beec2014-04-04 05:16:06 +00002722 static const MCPhysReg *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002723
Craig Topper840beec2014-04-04 05:16:06 +00002724 static const MCPhysReg VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002725 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2726 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2727 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00002728
Owen Andersone2f23a32007-09-07 04:06:50 +00002729 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002730 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00002731 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00002732
2733 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002734
Craig Topper840beec2014-04-04 05:16:06 +00002735 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002736
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002737 // In 32-bit non-varargs functions, the stack space for vectors is after the
2738 // stack space for non-vectors. We do not use this space unless we have
2739 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00002740 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002741 // that out...for the pathological case, compute VecArgOffset as the
2742 // start of the vector parameter area. Computing VecArgOffset is the
2743 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002744 unsigned VecArgOffset = ArgOffset;
2745 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002746 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002747 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002748 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002749 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002750
Duncan Sandsd97eea32008-03-21 09:14:45 +00002751 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002752 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00002753 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002754 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002755 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2756 VecArgOffset += ArgSize;
2757 continue;
2758 }
2759
Owen Anderson9f944592009-08-11 20:47:22 +00002760 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002761 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002762 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002763 case MVT::i32:
2764 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002765 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002766 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002767 case MVT::i64: // PPC64
2768 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002769 // FIXME: We are guaranteed to be !isPPC64 at this point.
2770 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002771 VecArgOffset += 8;
2772 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002773 case MVT::v4f32:
2774 case MVT::v4i32:
2775 case MVT::v8i16:
2776 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002777 // Nothing to do, we're only looking at Nonvector args here.
2778 break;
2779 }
2780 }
2781 }
2782 // We've found where the vector parameter area in memory is. Skip the
2783 // first 12 parameters; these don't use that memory.
2784 VecArgOffset = ((VecArgOffset+15)/16)*16;
2785 VecArgOffset += 12*16;
2786
Chris Lattner4302e8f2006-05-16 18:18:50 +00002787 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00002788 // entry to a function on PPC, the arguments start after the linkage area,
2789 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00002790
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002791 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002792 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00002793 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002794 unsigned CurArgIdx = 0;
2795 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002796 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002797 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002798 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00002799 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00002800 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002801 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002802 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2803 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002804
Chris Lattner318f0d22006-05-16 18:51:52 +00002805 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002806
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002807 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002808 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2809 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002810 if (isVarArg || isPPC64) {
2811 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002812 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002813 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002814 PtrByteSize);
2815 } else nAltivecParamsAtEnd++;
2816 } else
2817 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002818 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002819 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002820 PtrByteSize);
2821
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002822 // FIXME the codegen can be much improved in some cases.
2823 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002824 if (Flags.isByVal()) {
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002825 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002826 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002827 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002828 // Objects of size 1 and 2 are right justified, everything else is
2829 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00002830 if (ObjSize==1 || ObjSize==2) {
2831 CurArgOffset = CurArgOffset + (4 - ObjSize);
2832 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002833 // The value of the object is its address.
Evan Cheng0664a672010-07-03 00:40:23 +00002834 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002835 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002836 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002837 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00002838 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002839 unsigned VReg;
2840 if (isPPC64)
2841 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2842 else
2843 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002844 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002845 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002846 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002847 MachinePointerInfo(FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002848 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00002849 MemOps.push_back(Store);
2850 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00002851 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002852
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002853 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00002854
Dale Johannesen21a8f142008-03-08 01:41:42 +00002855 continue;
2856 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002857 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2858 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002859 // to memory. ArgOffset will be the address of the beginning
2860 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002861 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002862 unsigned VReg;
2863 if (isPPC64)
2864 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2865 else
2866 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00002867 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002868 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002869 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002870 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002871 MachinePointerInfo(FuncArg, j),
David Greene87a5abe2010-02-15 16:56:53 +00002872 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002873 MemOps.push_back(Store);
2874 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002875 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002876 } else {
2877 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2878 break;
2879 }
2880 }
2881 continue;
2882 }
2883
Owen Anderson9f944592009-08-11 20:47:22 +00002884 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002885 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002886 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002887 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00002888 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00002889 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002890 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002891 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Hal Finkel7f908e82014-03-06 00:45:19 +00002892
2893 if (ObjectVT == MVT::i1)
2894 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal);
2895
Bill Wendling968f32c2008-03-07 20:49:02 +00002896 ++GPR_idx;
2897 } else {
2898 needsLoad = true;
2899 ArgSize = PtrByteSize;
2900 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002901 // All int arguments reserve stack space in the Darwin ABI.
2902 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00002903 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002904 }
Bill Wendling968f32c2008-03-07 20:49:02 +00002905 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00002906 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00002907 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002908 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002909 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00002910
Hal Finkel940ab932014-02-28 00:27:01 +00002911 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00002912 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00002913 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002914 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00002915
Chris Lattnerec78cad2006-06-26 22:48:35 +00002916 ++GPR_idx;
2917 } else {
2918 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00002919 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002920 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002921 // All int arguments reserve stack space in the Darwin ABI.
2922 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002923 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002924
Owen Anderson9f944592009-08-11 20:47:22 +00002925 case MVT::f32:
2926 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00002927 // Every 4 bytes of argument space consumes one of the GPRs available for
2928 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002929 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002930 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00002931 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002932 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00002933 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002934 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002935 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002936
Owen Anderson9f944592009-08-11 20:47:22 +00002937 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00002938 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002939 else
Devang Patelf3292b22011-02-21 23:21:26 +00002940 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002941
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002942 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002943 ++FPR_idx;
2944 } else {
2945 needsLoad = true;
2946 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002947
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002948 // All FP arguments reserve stack space in the Darwin ABI.
2949 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002950 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002951 case MVT::v4f32:
2952 case MVT::v4i32:
2953 case MVT::v8i16:
2954 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00002955 // Note that vector arguments in registers don't reserve stack space,
2956 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002957 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002958 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002959 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00002960 if (isVarArg) {
2961 while ((ArgOffset % 16) != 0) {
2962 ArgOffset += PtrByteSize;
2963 if (GPR_idx != Num_GPR_Regs)
2964 GPR_idx++;
2965 }
2966 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002967 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00002968 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002969 ++VR_idx;
2970 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002971 if (!isVarArg && !isPPC64) {
2972 // Vectors go after all the nonvectors.
2973 CurArgOffset = VecArgOffset;
2974 VecArgOffset += 16;
2975 } else {
2976 // Vectors are aligned.
2977 ArgOffset = ((ArgOffset+15)/16)*16;
2978 CurArgOffset = ArgOffset;
2979 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00002980 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002981 needsLoad = true;
2982 }
2983 break;
2984 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002985
Chris Lattner4302e8f2006-05-16 18:18:50 +00002986 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002987 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002988 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002989 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002990 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00002991 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002992 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002993 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002994 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002995 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002996
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002997 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002998 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002999
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003000 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003001 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003002 // taking the difference between two stack areas will result in an aligned
3003 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003004 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003005
Chris Lattner4302e8f2006-05-16 18:18:50 +00003006 // If the function takes variable number of arguments, make a frame index for
3007 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003008 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003009 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003010
Dan Gohman31ae5862010-04-17 14:41:14 +00003011 FuncInfo->setVarArgsFrameIndex(
3012 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00003013 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00003014 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003015
Chris Lattner4302e8f2006-05-16 18:18:50 +00003016 // If this function is vararg, store any remaining integer argument regs
3017 // to their spots on the stack so that they may be loaded by deferencing the
3018 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003019 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00003020 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00003021
Chris Lattner2cca3852006-11-18 01:57:19 +00003022 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00003023 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003024 else
Devang Patelf3292b22011-02-21 23:21:26 +00003025 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003026
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003027 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00003028 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3029 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003030 MemOps.push_back(Store);
3031 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003032 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00003033 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003034 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003035 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003036
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003037 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003038 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003039
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003040 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003041}
3042
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003043/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
3044/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003045static unsigned
3046CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
3047 bool isPPC64,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003048 bool isVarArg,
3049 unsigned CC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003050 const SmallVectorImpl<ISD::OutputArg>
3051 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003052 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003053 unsigned &nAltivecParamsAtEnd) {
3054 // Count how many bytes are to be pushed on the stack, including the linkage
3055 // area, and parameter passing area. We start with 24/48 bytes, which is
3056 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003057 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003058 unsigned NumOps = Outs.size();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003059 unsigned PtrByteSize = isPPC64 ? 8 : 4;
3060
3061 // Add up all the space actually used.
3062 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
3063 // they all go in registers, but we must reserve stack space for them for
3064 // possible use by the caller. In varargs or 64-bit calls, parameters are
3065 // assigned stack space in order, with padding so Altivec parameters are
3066 // 16-byte aligned.
3067 nAltivecParamsAtEnd = 0;
3068 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003069 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003070 EVT ArgVT = Outs[i].VT;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003071 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00003072 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
Hal Finkel27774d92014-03-13 07:58:58 +00003073 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8 ||
Hal Finkela6c8b512014-03-26 16:12:58 +00003074 ArgVT==MVT::v2f64 || ArgVT==MVT::v2i64) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003075 if (!isVarArg && !isPPC64) {
3076 // Non-varargs Altivec parameters go after all the non-Altivec
3077 // parameters; handle those later so we know how much padding we need.
3078 nAltivecParamsAtEnd++;
3079 continue;
3080 }
3081 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
3082 NumBytes = ((NumBytes+15)/16)*16;
3083 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003084 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003085 }
3086
3087 // Allow for Altivec parameters at the end, if needed.
3088 if (nAltivecParamsAtEnd) {
3089 NumBytes = ((NumBytes+15)/16)*16;
3090 NumBytes += 16*nAltivecParamsAtEnd;
3091 }
3092
3093 // The prolog code of the callee may store up to 8 GPR argument registers to
3094 // the stack, allowing va_start to index over them in memory if its varargs.
3095 // Because we cannot tell if this is needed on the caller side, we have to
3096 // conservatively assume that it is needed. As such, make sure we have at
3097 // least enough stack space for the caller to store the 8 GPRs.
3098 NumBytes = std::max(NumBytes,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003099 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003100
3101 // Tail call needs the stack to be aligned.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003102 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
3103 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
3104 getFrameLowering()->getStackAlignment();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003105 unsigned AlignMask = TargetAlign-1;
3106 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
3107 }
3108
3109 return NumBytes;
3110}
3111
3112/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003113/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00003114static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003115 unsigned ParamSize) {
3116
Dale Johannesen86dcae12009-11-24 01:09:07 +00003117 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003118
3119 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
3120 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3121 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3122 // Remember only if the new adjustement is bigger.
3123 if (SPDiff < FI->getTailCallSPDelta())
3124 FI->setTailCallSPDelta(SPDiff);
3125
3126 return SPDiff;
3127}
3128
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003129/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3130/// for tail call optimization. Targets which want to do tail call
3131/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003132bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003133PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003134 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003135 bool isVarArg,
3136 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003137 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003138 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00003139 return false;
3140
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003141 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003142 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003143 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003144
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003145 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00003146 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003147 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
3148 // Functions containing by val parameters are not supported.
3149 for (unsigned i = 0; i != Ins.size(); i++) {
3150 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3151 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003152 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003153
Alp Tokerf907b892013-12-05 05:44:44 +00003154 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003155 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
3156 return true;
3157
3158 // At the moment we can only do local tail calls (in same module, hidden
3159 // or protected) if we are generating PIC.
3160 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
3161 return G->getGlobal()->hasHiddenVisibility()
3162 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003163 }
3164
3165 return false;
3166}
3167
Chris Lattnereb755fc2006-05-17 19:00:46 +00003168/// isCallCompatibleAddress - Return the immediate to use if the specified
3169/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003170static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00003171 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003172 if (!C) return nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003173
Dan Gohmaneffb8942008-09-12 16:56:44 +00003174 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003175 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00003176 SignExtend32<26>(Addr) != Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +00003177 return nullptr; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003178
Dan Gohmaneffb8942008-09-12 16:56:44 +00003179 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00003180 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003181}
3182
Dan Gohmand78c4002008-05-13 00:00:25 +00003183namespace {
3184
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003185struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003186 SDValue Arg;
3187 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003188 int FrameIdx;
3189
3190 TailCallArgumentInfo() : FrameIdx(0) {}
3191};
3192
Dan Gohmand78c4002008-05-13 00:00:25 +00003193}
3194
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003195/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3196static void
3197StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003198 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003199 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3200 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003201 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003202 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003203 SDValue Arg = TailCallArgs[i].Arg;
3204 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003205 int FI = TailCallArgs[i].FrameIdx;
3206 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003207 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003208 MachinePointerInfo::getFixedStack(FI),
3209 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003210 }
3211}
3212
3213/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3214/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003215static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003216 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003217 SDValue Chain,
3218 SDValue OldRetAddr,
3219 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003220 int SPDiff,
3221 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003222 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003223 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003224 if (SPDiff) {
3225 // Calculate the new stack slot for the return address.
3226 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003227 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003228 isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003229 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003230 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003231 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003232 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003233 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003234 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003235 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003236
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003237 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3238 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003239 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003240 int NewFPLoc =
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003241 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene1fbe0542009-11-12 20:49:22 +00003242 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003243 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003244 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3245 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003246 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003247 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003248 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003249 }
3250 return Chain;
3251}
3252
3253/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3254/// the position of the argument.
3255static void
3256CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003257 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003258 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003259 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003260 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003261 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003262 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003263 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003264 TailCallArgumentInfo Info;
3265 Info.Arg = Arg;
3266 Info.FrameIdxOp = FIN;
3267 Info.FrameIdx = FI;
3268 TailCallArguments.push_back(Info);
3269}
3270
3271/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3272/// stack slot. Returns the chain as result and the loaded frame pointers in
3273/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003274SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003275 int SPDiff,
3276 SDValue Chain,
3277 SDValue &LROpOut,
3278 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003279 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003280 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003281 if (SPDiff) {
3282 // Load the LR and FP stack slot for later adjusting.
Owen Anderson9f944592009-08-11 20:47:22 +00003283 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003284 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003285 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003286 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003287 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003288
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003289 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3290 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003291 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003292 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003293 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003294 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003295 Chain = SDValue(FPOpOut.getNode(), 1);
3296 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003297 }
3298 return Chain;
3299}
3300
Dale Johannesen85d41a12008-03-04 23:17:14 +00003301/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003302/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003303/// specified by the specific parameter attribute. The copy will be passed as
3304/// a byval function parameter.
3305/// Sometimes what we are copying is the end of a larger object, the part that
3306/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003307static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003308CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003309 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003310 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003311 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003312 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003313 false, false, MachinePointerInfo(),
3314 MachinePointerInfo());
Dale Johannesen85d41a12008-03-04 23:17:14 +00003315}
Chris Lattner43df5b32007-02-25 05:34:32 +00003316
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003317/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3318/// tail calls.
3319static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003320LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3321 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003322 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003323 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3324 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003325 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003326 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003327 if (!isTailCall) {
3328 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003329 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003330 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003331 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003332 else
Owen Anderson9f944592009-08-11 20:47:22 +00003333 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003334 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003335 DAG.getConstant(ArgOffset, PtrVT));
3336 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003337 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3338 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003339 // Calculate and remember argument location.
3340 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3341 TailCallArguments);
3342}
3343
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003344static
3345void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003346 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003347 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003348 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003349 MachineFunction &MF = DAG.getMachineFunction();
3350
3351 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3352 // might overwrite each other in case of tail call optimization.
3353 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003354 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003355 InFlag = SDValue();
3356 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3357 MemOpChains2, dl);
3358 if (!MemOpChains2.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003359 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003360
3361 // Store the return address to the appropriate stack slot.
3362 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3363 isPPC64, isDarwinABI, dl);
3364
3365 // Emit callseq_end just before tailcall node.
3366 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003367 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003368 InFlag = Chain.getValue(1);
3369}
3370
3371static
3372unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003373 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003374 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3375 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003376 const PPCSubtarget &PPCSubTarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003377
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003378 bool isPPC64 = PPCSubTarget.isPPC64();
3379 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3380
Owen Anderson53aa7a92009-08-10 22:56:29 +00003381 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003382 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003383 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003384
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003385 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003386
Torok Edwin31e90d22010-08-04 20:47:44 +00003387 bool needIndirectCall = true;
3388 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003389 // If this is an absolute destination address, use the munged value.
3390 Callee = SDValue(Dest, 0);
Torok Edwin31e90d22010-08-04 20:47:44 +00003391 needIndirectCall = false;
3392 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003393
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003394 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3395 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3396 // Use indirect calls for ALL functions calls in JIT mode, since the
3397 // far-call stubs may be outside relocation limits for a BL instruction.
3398 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3399 unsigned OpFlags = 0;
3400 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003401 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003402 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003403 (G->getGlobal()->isDeclaration() ||
3404 G->getGlobal()->isWeakForLinker())) {
3405 // PC-relative references to external symbols should go through $stub,
3406 // unless we're building with the leopard linker or later, which
3407 // automatically synthesizes these stubs.
3408 OpFlags = PPCII::MO_DARWIN_STUB;
3409 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003410
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003411 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3412 // every direct call is) turn it into a TargetGlobalAddress /
3413 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin31e90d22010-08-04 20:47:44 +00003414 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003415 Callee.getValueType(),
3416 0, OpFlags);
Torok Edwin31e90d22010-08-04 20:47:44 +00003417 needIndirectCall = false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003418 }
Torok Edwin31e90d22010-08-04 20:47:44 +00003419 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003420
Torok Edwin31e90d22010-08-04 20:47:44 +00003421 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003422 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003423
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003424 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003425 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003426 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003427 // PC-relative references to external symbols should go through $stub,
3428 // unless we're building with the leopard linker or later, which
3429 // automatically synthesizes these stubs.
3430 OpFlags = PPCII::MO_DARWIN_STUB;
3431 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003432
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003433 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3434 OpFlags);
3435 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003436 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003437
Torok Edwin31e90d22010-08-04 20:47:44 +00003438 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003439 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3440 // to do the call, we can't use PPCISD::CALL.
3441 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003442
3443 if (isSVR4ABI && isPPC64) {
3444 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3445 // entry point, but to the function descriptor (the function entry point
3446 // address is part of the function descriptor though).
3447 // The function descriptor is a three doubleword structure with the
3448 // following fields: function entry point, TOC base address and
3449 // environment pointer.
3450 // Thus for a call through a function pointer, the following actions need
3451 // to be performed:
3452 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003453 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003454 // 2. Load the address of the function entry point from the function
3455 // descriptor.
3456 // 3. Load the TOC of the callee from the function descriptor into r2.
3457 // 4. Load the environment pointer from the function descriptor into
3458 // r11.
3459 // 5. Branch to the function entry point address.
3460 // 6. On return of the callee, the TOC of the caller needs to be
3461 // restored (this is done in FinishCall()).
3462 //
3463 // All those operations are flagged together to ensure that no other
3464 // operations can be scheduled in between. E.g. without flagging the
3465 // operations together, a TOC access in the caller could be scheduled
3466 // between the load of the callee TOC and the branch to the callee, which
3467 // results in the TOC access going through the TOC of the callee instead
3468 // of going through the TOC of the caller, which leads to incorrect code.
3469
3470 // Load the address of the function entry point from the function
3471 // descriptor.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003472 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00003473 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00003474 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
Tilmann Scheller79fef932009-12-18 13:00:15 +00003475 Chain = LoadFuncPtr.getValue(1);
3476 InFlag = LoadFuncPtr.getValue(2);
3477
3478 // Load environment pointer into r11.
3479 // Offset of the environment pointer within the function descriptor.
3480 SDValue PtrOff = DAG.getIntPtrConstant(16);
3481
3482 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3483 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3484 InFlag);
3485 Chain = LoadEnvPtr.getValue(1);
3486 InFlag = LoadEnvPtr.getValue(2);
3487
3488 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3489 InFlag);
3490 Chain = EnvVal.getValue(0);
3491 InFlag = EnvVal.getValue(1);
3492
3493 // Load TOC of the callee into r2. We are using a target-specific load
3494 // with r2 hard coded, because the result of a target-independent load
3495 // would never go directly into r2, since r2 is a reserved register (which
3496 // prevents the register allocator from allocating it), resulting in an
3497 // additional register being allocated and an unnecessary move instruction
3498 // being generated.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003499 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003500 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3501 Callee, InFlag);
3502 Chain = LoadTOCPtr.getValue(0);
3503 InFlag = LoadTOCPtr.getValue(1);
3504
3505 MTCTROps[0] = Chain;
3506 MTCTROps[1] = LoadFuncPtr;
3507 MTCTROps[2] = InFlag;
3508 }
3509
Craig Topper48d114b2014-04-26 18:35:24 +00003510 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00003511 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003512 InFlag = Chain.getValue(1);
3513
3514 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00003515 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003516 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003517 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003518 CallOpc = PPCISD::BCTRL;
Craig Topper062a2ba2014-04-25 05:30:21 +00003519 Callee.setNode(nullptr);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003520 // Add use of X11 (holding environment pointer)
3521 if (isSVR4ABI && isPPC64)
3522 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003523 // Add CTR register as callee so a bctr can be emitted later.
3524 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00003525 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003526 }
3527
3528 // If this is a direct call, pass the chain and the callee.
3529 if (Callee.getNode()) {
3530 Ops.push_back(Chain);
3531 Ops.push_back(Callee);
3532 }
3533 // If this is a tail call add stack pointer delta.
3534 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00003535 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003536
3537 // Add argument registers to the end of the list so that they are known live
3538 // into the call.
3539 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3540 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3541 RegsToPass[i].second.getValueType()));
3542
3543 return CallOpc;
3544}
3545
Roman Divacky76293062012-09-18 16:47:58 +00003546static
3547bool isLocalCall(const SDValue &Callee)
3548{
3549 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00003550 return !G->getGlobal()->isDeclaration() &&
3551 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00003552 return false;
3553}
3554
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003555SDValue
3556PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003557 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003558 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003559 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003560 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003561
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003562 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003563 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003564 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003565 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003566
3567 // Copy all of the result registers out of their specified physreg.
3568 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3569 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003570 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00003571
3572 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3573 VA.getLocReg(), VA.getLocVT(), InFlag);
3574 Chain = Val.getValue(1);
3575 InFlag = Val.getValue(2);
3576
3577 switch (VA.getLocInfo()) {
3578 default: llvm_unreachable("Unknown loc info!");
3579 case CCValAssign::Full: break;
3580 case CCValAssign::AExt:
3581 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3582 break;
3583 case CCValAssign::ZExt:
3584 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3585 DAG.getValueType(VA.getValVT()));
3586 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3587 break;
3588 case CCValAssign::SExt:
3589 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3590 DAG.getValueType(VA.getValVT()));
3591 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3592 break;
3593 }
3594
3595 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003596 }
3597
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003598 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003599}
3600
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003601SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00003602PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003603 bool isTailCall, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003604 SelectionDAG &DAG,
3605 SmallVector<std::pair<unsigned, SDValue>, 8>
3606 &RegsToPass,
3607 SDValue InFlag, SDValue Chain,
3608 SDValue &Callee,
3609 int SPDiff, unsigned NumBytes,
3610 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003611 SmallVectorImpl<SDValue> &InVals) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003612 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003613 SmallVector<SDValue, 8> Ops;
3614 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3615 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003616 PPCSubTarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003617
Hal Finkel5ab37802012-08-28 02:10:27 +00003618 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3619 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3620 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3621
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003622 // When performing tail call optimization the callee pops its arguments off
3623 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00003624 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003625 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003626 (CallConv == CallingConv::Fast &&
3627 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003628
Roman Divackyef21be22012-03-06 16:41:49 +00003629 // Add a register mask operand representing the call-preserved registers.
3630 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3631 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3632 assert(Mask && "Missing call preserved mask for calling convention");
3633 Ops.push_back(DAG.getRegisterMask(Mask));
3634
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003635 if (InFlag.getNode())
3636 Ops.push_back(InFlag);
3637
3638 // Emit tail call.
3639 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003640 assert(((Callee.getOpcode() == ISD::Register &&
3641 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3642 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3643 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3644 isa<ConstantSDNode>(Callee)) &&
3645 "Expecting an global address, external symbol, absolute value or register");
3646
Craig Topper48d114b2014-04-26 18:35:24 +00003647 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003648 }
3649
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003650 // Add a NOP immediately after the branch instruction when using the 64-bit
3651 // SVR4 ABI. At link time, if caller and callee are in a different module and
3652 // thus have a different TOC, the call will be replaced with a call to a stub
3653 // function which saves the current TOC, loads the TOC of the callee and
3654 // branches to the callee. The NOP will be replaced with a load instruction
3655 // which restores the TOC of the caller from the TOC save slot of the current
3656 // stack frame. If caller and callee belong to the same module (and have the
3657 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00003658
3659 bool needsTOCRestore = false;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003660 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003661 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003662 // This is a call through a function pointer.
3663 // Restore the caller TOC from the save area into R2.
3664 // See PrepareCall() for more information about calls through function
3665 // pointers in the 64-bit SVR4 ABI.
3666 // We are using a target-specific load with r2 hard coded, because the
3667 // result of a target-independent load would never go directly into r2,
3668 // since r2 is a reserved register (which prevents the register allocator
3669 // from allocating it), resulting in an additional register being
3670 // allocated and an unnecessary move instruction being generated.
Hal Finkel51861b42012-03-31 14:45:15 +00003671 needsTOCRestore = true;
Bill Schmidtcea15962013-09-26 17:09:28 +00003672 } else if ((CallOpc == PPCISD::CALL) &&
3673 (!isLocalCall(Callee) ||
3674 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Roman Divacky76293062012-09-18 16:47:58 +00003675 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003676 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller79fef932009-12-18 13:00:15 +00003677 }
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003678 }
3679
Craig Topper48d114b2014-04-26 18:35:24 +00003680 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Hal Finkel51861b42012-03-31 14:45:15 +00003681 InFlag = Chain.getValue(1);
3682
3683 if (needsTOCRestore) {
3684 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3685 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3686 InFlag = Chain.getValue(1);
3687 }
3688
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003689 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3690 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003691 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003692 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003693 InFlag = Chain.getValue(1);
3694
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003695 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3696 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003697}
3698
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003699SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00003700PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003701 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00003702 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00003703 SDLoc &dl = CLI.DL;
3704 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3705 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3706 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00003707 SDValue Chain = CLI.Chain;
3708 SDValue Callee = CLI.Callee;
3709 bool &isTailCall = CLI.IsTailCall;
3710 CallingConv::ID CallConv = CLI.CallConv;
3711 bool isVarArg = CLI.IsVarArg;
3712
Evan Cheng67a69dd2010-01-27 00:07:07 +00003713 if (isTailCall)
3714 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3715 Ins, DAG);
3716
Reid Kleckner5772b772014-04-24 20:14:34 +00003717 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
3718 report_fatal_error("failed to perform tail call elimination on a call "
3719 "site marked musttail");
3720
Bill Schmidt57d6de52012-10-23 15:51:16 +00003721 if (PPCSubTarget.isSVR4ABI()) {
3722 if (PPCSubTarget.isPPC64())
3723 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3724 isTailCall, Outs, OutVals, Ins,
3725 dl, DAG, InVals);
3726 else
3727 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3728 isTailCall, Outs, OutVals, Ins,
3729 dl, DAG, InVals);
3730 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003731
Bill Schmidt57d6de52012-10-23 15:51:16 +00003732 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3733 isTailCall, Outs, OutVals, Ins,
3734 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003735}
3736
3737SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003738PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3739 CallingConv::ID CallConv, bool isVarArg,
3740 bool isTailCall,
3741 const SmallVectorImpl<ISD::OutputArg> &Outs,
3742 const SmallVectorImpl<SDValue> &OutVals,
3743 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003744 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003745 SmallVectorImpl<SDValue> &InVals) const {
3746 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003747 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003748
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003749 assert((CallConv == CallingConv::C ||
3750 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003751
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003752 unsigned PtrByteSize = 4;
3753
3754 MachineFunction &MF = DAG.getMachineFunction();
3755
3756 // Mark this function as potentially containing a function that contains a
3757 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3758 // and restoring the callers stack pointer in this functions epilog. This is
3759 // done because by tail calling the called function might overwrite the value
3760 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003761 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3762 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003763 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00003764
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003765 // Count how many bytes are to be pushed on the stack, including the linkage
3766 // area, parameter list area and the part of the local variable space which
3767 // contains copies of aggregates which are passed by value.
3768
3769 // Assign locations to all of the outgoing arguments.
3770 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003771 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003772 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003773
3774 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003775 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003776
3777 if (isVarArg) {
3778 // Handle fixed and variable vector arguments differently.
3779 // Fixed vector arguments go into registers as long as registers are
3780 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003781 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00003782
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003783 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003784 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003785 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003786 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00003787
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003788 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00003789 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3790 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003791 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00003792 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3793 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003794 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003795
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003796 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003797#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00003798 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00003799 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003800#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003801 llvm_unreachable(nullptr);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003802 }
3803 }
3804 } else {
3805 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00003806 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003807 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003808
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003809 // Assign locations to all of the outgoing aggregate by value arguments.
3810 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003811 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003812 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003813
3814 // Reserve stack space for the allocations in CCInfo.
3815 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3816
Bill Schmidtef17c142013-02-06 17:33:58 +00003817 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003818
3819 // Size of the linkage area, parameter list area and the part of the local
3820 // space variable where copies of aggregates which are passed by value are
3821 // stored.
3822 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003823
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003824 // Calculate by how many bytes the stack has to be adjusted in case of tail
3825 // call optimization.
3826 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3827
3828 // Adjust the stack pointer for the new arguments...
3829 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003830 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3831 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003832 SDValue CallSeqStart = Chain;
3833
3834 // Load the return address and frame pointer so it can be moved somewhere else
3835 // later.
3836 SDValue LROp, FPOp;
3837 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3838 dl);
3839
3840 // Set up a copy of the stack pointer for use loading and storing any
3841 // arguments that may not fit in the registers available for argument
3842 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00003843 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00003844
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003845 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3846 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3847 SmallVector<SDValue, 8> MemOpChains;
3848
Roman Divacky71038e72011-08-30 17:04:16 +00003849 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003850 // Walk the register/memloc assignments, inserting copies/loads.
3851 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3852 i != e;
3853 ++i) {
3854 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003855 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003856 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00003857
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003858 if (Flags.isByVal()) {
3859 // Argument is an aggregate which is passed by value, thus we need to
3860 // create a copy of it in the local variable space of the current stack
3861 // frame (which is the stack frame of the caller) and pass the address of
3862 // this copy to the callee.
3863 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3864 CCValAssign &ByValVA = ByValArgLocs[j++];
3865 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00003866
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003867 // Memory reserved in the local variable space of the callers stack frame.
3868 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003869
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003870 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3871 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00003872
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003873 // Create a copy of the argument in the local area of the current
3874 // stack frame.
3875 SDValue MemcpyCall =
3876 CreateCopyOfByValArgument(Arg, PtrOff,
3877 CallSeqStart.getNode()->getOperand(0),
3878 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00003879
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003880 // This must go outside the CALLSEQ_START..END.
3881 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003882 CallSeqStart.getNode()->getOperand(1),
3883 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003884 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3885 NewCallSeqStart.getNode());
3886 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00003887
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003888 // Pass the address of the aggregate copy on the stack either in a
3889 // physical register or in the parameter list area of the current stack
3890 // frame to the callee.
3891 Arg = PtrOff;
3892 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003893
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003894 if (VA.isRegLoc()) {
Hal Finkel2a9d3182014-03-06 00:23:33 +00003895 if (Arg.getValueType() == MVT::i1)
3896 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg);
3897
Roman Divacky71038e72011-08-30 17:04:16 +00003898 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003899 // Put argument in a physical register.
3900 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3901 } else {
3902 // Put argument in the parameter list area of the current stack frame.
3903 assert(VA.isMemLoc());
3904 unsigned LocMemOffset = VA.getLocMemOffset();
3905
3906 if (!isTailCall) {
3907 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3908 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3909
3910 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00003911 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00003912 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003913 } else {
3914 // Calculate and remember argument location.
3915 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3916 TailCallArguments);
3917 }
3918 }
3919 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003920
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003921 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003922 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Wesley Peck527da1b2010-11-23 03:31:01 +00003923
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003924 // Build a sequence of copy-to-reg nodes chained together with token chain
3925 // and flag operands which copy the outgoing args into the appropriate regs.
3926 SDValue InFlag;
3927 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3928 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3929 RegsToPass[i].second, InFlag);
3930 InFlag = Chain.getValue(1);
3931 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003932
Hal Finkel5ab37802012-08-28 02:10:27 +00003933 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3934 // registers.
3935 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003936 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3937 SDValue Ops[] = { Chain, InFlag };
3938
Hal Finkel5ab37802012-08-28 02:10:27 +00003939 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00003940 dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1));
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003941
Hal Finkel5ab37802012-08-28 02:10:27 +00003942 InFlag = Chain.getValue(1);
3943 }
3944
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003945 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003946 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3947 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003948
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003949 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3950 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3951 Ins, InVals);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003952}
3953
Bill Schmidt57d6de52012-10-23 15:51:16 +00003954// Copy an argument into memory, being careful to do this outside the
3955// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003956SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00003957PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3958 SDValue CallSeqStart,
3959 ISD::ArgFlagsTy Flags,
3960 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003961 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003962 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3963 CallSeqStart.getNode()->getOperand(0),
3964 Flags, DAG, dl);
3965 // The MEMCPY must go outside the CALLSEQ_START..END.
3966 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003967 CallSeqStart.getNode()->getOperand(1),
3968 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00003969 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3970 NewCallSeqStart.getNode());
3971 return NewCallSeqStart;
3972}
3973
3974SDValue
3975PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003976 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003977 bool isTailCall,
3978 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003979 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003980 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003981 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003982 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003983
Bill Schmidt57d6de52012-10-23 15:51:16 +00003984 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003985
Bill Schmidt57d6de52012-10-23 15:51:16 +00003986 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3987 unsigned PtrByteSize = 8;
3988
3989 MachineFunction &MF = DAG.getMachineFunction();
3990
3991 // Mark this function as potentially containing a function that contains a
3992 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3993 // and restoring the callers stack pointer in this functions epilog. This is
3994 // done because by tail calling the called function might overwrite the value
3995 // in this function's (MF) stack pointer stack slot 0(SP).
3996 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3997 CallConv == CallingConv::Fast)
3998 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3999
4000 unsigned nAltivecParamsAtEnd = 0;
4001
4002 // Count how many bytes are to be pushed on the stack, including the linkage
4003 // area, and parameter passing area. We start with at least 48 bytes, which
4004 // is reserved space for [SP][CR][LR][3 x unused].
4005 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
4006 // of this call.
4007 unsigned NumBytes =
4008 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
4009 Outs, OutVals, nAltivecParamsAtEnd);
4010
4011 // Calculate by how many bytes the stack has to be adjusted in case of tail
4012 // call optimization.
4013 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4014
4015 // To protect arguments on the stack from being clobbered in a tail call,
4016 // force all the loads to happen before doing any other lowering.
4017 if (isTailCall)
4018 Chain = DAG.getStackArgumentTokenFactor(Chain);
4019
4020 // Adjust the stack pointer for the new arguments...
4021 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004022 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4023 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004024 SDValue CallSeqStart = Chain;
4025
4026 // Load the return address and frame pointer so it can be move somewhere else
4027 // later.
4028 SDValue LROp, FPOp;
4029 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4030 dl);
4031
4032 // Set up a copy of the stack pointer for use loading and storing any
4033 // arguments that may not fit in the registers available for argument
4034 // passing.
4035 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
4036
4037 // Figure out which arguments are going to go in registers, and which in
4038 // memory. Also, if this is a vararg function, floating point operations
4039 // must be stored to our stack, and loaded into integer regs as well, if
4040 // any integer regs are available for argument passing.
4041 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
4042 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
4043
Craig Topper840beec2014-04-04 05:16:06 +00004044 static const MCPhysReg GPR[] = {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004045 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4046 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4047 };
Craig Topper840beec2014-04-04 05:16:06 +00004048 static const MCPhysReg *FPR = GetFPR();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004049
Craig Topper840beec2014-04-04 05:16:06 +00004050 static const MCPhysReg VR[] = {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004051 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4052 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4053 };
Craig Topper840beec2014-04-04 05:16:06 +00004054 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00004055 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
4056 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
4057 };
4058
Bill Schmidt57d6de52012-10-23 15:51:16 +00004059 const unsigned NumGPRs = array_lengthof(GPR);
4060 const unsigned NumFPRs = 13;
4061 const unsigned NumVRs = array_lengthof(VR);
4062
4063 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4064 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4065
4066 SmallVector<SDValue, 8> MemOpChains;
4067 for (unsigned i = 0; i != NumOps; ++i) {
4068 SDValue Arg = OutVals[i];
4069 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4070
4071 // PtrOff will be used to store the current argument to the stack if a
4072 // register cannot be found for it.
4073 SDValue PtrOff;
4074
4075 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
4076
4077 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4078
4079 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00004080 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004081 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4082 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4083 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
4084 }
4085
4086 // FIXME memcpy is used way more than necessary. Correctness first.
4087 // Note: "by value" is code for passing a structure by value, not
4088 // basic types.
4089 if (Flags.isByVal()) {
4090 // Note: Size includes alignment padding, so
4091 // struct x { short a; char b; }
4092 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
4093 // These are the proper values we need for right-justifying the
4094 // aggregate in a parameter register.
4095 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00004096
4097 // An empty aggregate parameter takes up no storage and no
4098 // registers.
4099 if (Size == 0)
4100 continue;
4101
Hal Finkel262a2242013-09-12 23:20:06 +00004102 unsigned BVAlign = Flags.getByValAlign();
4103 if (BVAlign > 8) {
4104 if (BVAlign % PtrByteSize != 0)
4105 llvm_unreachable(
4106 "ByVal alignment is not a multiple of the pointer size");
4107
4108 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
4109 }
4110
Bill Schmidt57d6de52012-10-23 15:51:16 +00004111 // All aggregates smaller than 8 bytes must be passed right-justified.
4112 if (Size==1 || Size==2 || Size==4) {
4113 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
4114 if (GPR_idx != NumGPRs) {
4115 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
4116 MachinePointerInfo(), VT,
4117 false, false, 0);
4118 MemOpChains.push_back(Load.getValue(1));
4119 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4120
4121 ArgOffset += PtrByteSize;
4122 continue;
4123 }
4124 }
4125
4126 if (GPR_idx == NumGPRs && Size < 8) {
4127 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4128 PtrOff.getValueType());
4129 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4130 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4131 CallSeqStart,
4132 Flags, DAG, dl);
4133 ArgOffset += PtrByteSize;
4134 continue;
4135 }
4136 // Copy entire object into memory. There are cases where gcc-generated
4137 // code assumes it is there, even if it could be put entirely into
4138 // registers. (This is not what the doc says.)
4139
4140 // FIXME: The above statement is likely due to a misunderstanding of the
4141 // documents. All arguments must be copied into the parameter area BY
4142 // THE CALLEE in the event that the callee takes the address of any
4143 // formal argument. That has not yet been implemented. However, it is
4144 // reasonable to use the stack area as a staging area for the register
4145 // load.
4146
4147 // Skip this for small aggregates, as we will use the same slot for a
4148 // right-justified copy, below.
4149 if (Size >= 8)
4150 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4151 CallSeqStart,
4152 Flags, DAG, dl);
4153
4154 // When a register is available, pass a small aggregate right-justified.
4155 if (Size < 8 && GPR_idx != NumGPRs) {
4156 // The easiest way to get this right-justified in a register
4157 // is to copy the structure into the rightmost portion of a
4158 // local variable slot, then load the whole slot into the
4159 // register.
4160 // FIXME: The memcpy seems to produce pretty awful code for
4161 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00004162 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00004163 // parameter save area instead of a new local variable.
4164 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
4165 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4166 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4167 CallSeqStart,
4168 Flags, DAG, dl);
4169
4170 // Load the slot into the register.
4171 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
4172 MachinePointerInfo(),
4173 false, false, false, 0);
4174 MemOpChains.push_back(Load.getValue(1));
4175 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4176
4177 // Done with this argument.
4178 ArgOffset += PtrByteSize;
4179 continue;
4180 }
4181
4182 // For aggregates larger than PtrByteSize, copy the pieces of the
4183 // object that fit into registers from the parameter save area.
4184 for (unsigned j=0; j<Size; j+=PtrByteSize) {
4185 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
4186 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
4187 if (GPR_idx != NumGPRs) {
4188 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4189 MachinePointerInfo(),
4190 false, false, false, 0);
4191 MemOpChains.push_back(Load.getValue(1));
4192 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4193 ArgOffset += PtrByteSize;
4194 } else {
4195 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4196 break;
4197 }
4198 }
4199 continue;
4200 }
4201
Craig Topper56710102013-08-15 02:33:50 +00004202 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004203 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00004204 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004205 case MVT::i32:
4206 case MVT::i64:
4207 if (GPR_idx != NumGPRs) {
4208 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
4209 } else {
4210 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4211 true, isTailCall, false, MemOpChains,
4212 TailCallArguments, dl);
4213 }
4214 ArgOffset += PtrByteSize;
4215 break;
4216 case MVT::f32:
4217 case MVT::f64:
4218 if (FPR_idx != NumFPRs) {
4219 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4220
4221 if (isVarArg) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004222 // A single float or an aggregate containing only a single float
4223 // must be passed right-justified in the stack doubleword, and
4224 // in the GPR, if one is available.
4225 SDValue StoreOff;
Craig Topper56710102013-08-15 02:33:50 +00004226 if (Arg.getSimpleValueType().SimpleTy == MVT::f32) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004227 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4228 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4229 } else
4230 StoreOff = PtrOff;
4231
4232 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004233 MachinePointerInfo(), false, false, 0);
4234 MemOpChains.push_back(Store);
4235
4236 // Float varargs are always shadowed in available integer registers
4237 if (GPR_idx != NumGPRs) {
4238 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4239 MachinePointerInfo(), false, false,
4240 false, 0);
4241 MemOpChains.push_back(Load.getValue(1));
4242 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4243 }
4244 } else if (GPR_idx != NumGPRs)
4245 // If we have any FPRs remaining, we may also have GPRs remaining.
4246 ++GPR_idx;
4247 } else {
4248 // Single-precision floating-point values are mapped to the
4249 // second (rightmost) word of the stack doubleword.
4250 if (Arg.getValueType() == MVT::f32) {
4251 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4252 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4253 }
4254
4255 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4256 true, isTailCall, false, MemOpChains,
4257 TailCallArguments, dl);
4258 }
4259 ArgOffset += 8;
4260 break;
4261 case MVT::v4f32:
4262 case MVT::v4i32:
4263 case MVT::v8i16:
4264 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00004265 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00004266 case MVT::v2i64:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004267 if (isVarArg) {
4268 // These go aligned on the stack, or in the corresponding R registers
4269 // when within range. The Darwin PPC ABI doc claims they also go in
4270 // V registers; in fact gcc does this only for arguments that are
4271 // prototyped, not for those that match the ... We do it for all
4272 // arguments, seems to work.
4273 while (ArgOffset % 16 !=0) {
4274 ArgOffset += PtrByteSize;
4275 if (GPR_idx != NumGPRs)
4276 GPR_idx++;
4277 }
4278 // We could elide this store in the case where the object fits
4279 // entirely in R registers. Maybe later.
4280 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
4281 DAG.getConstant(ArgOffset, PtrVT));
4282 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4283 MachinePointerInfo(), false, false, 0);
4284 MemOpChains.push_back(Store);
4285 if (VR_idx != NumVRs) {
4286 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4287 MachinePointerInfo(),
4288 false, false, false, 0);
4289 MemOpChains.push_back(Load.getValue(1));
Hal Finkel7811c612014-03-28 19:58:11 +00004290
4291 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4292 Arg.getSimpleValueType() == MVT::v2i64) ?
4293 VSRH[VR_idx] : VR[VR_idx];
4294 ++VR_idx;
4295
4296 RegsToPass.push_back(std::make_pair(VReg, Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004297 }
4298 ArgOffset += 16;
4299 for (unsigned i=0; i<16; i+=PtrByteSize) {
4300 if (GPR_idx == NumGPRs)
4301 break;
4302 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4303 DAG.getConstant(i, PtrVT));
4304 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4305 false, false, false, 0);
4306 MemOpChains.push_back(Load.getValue(1));
4307 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4308 }
4309 break;
4310 }
4311
4312 // Non-varargs Altivec params generally go in registers, but have
4313 // stack space allocated at the end.
4314 if (VR_idx != NumVRs) {
4315 // Doesn't have GPR space allocated.
Hal Finkel7811c612014-03-28 19:58:11 +00004316 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4317 Arg.getSimpleValueType() == MVT::v2i64) ?
4318 VSRH[VR_idx] : VR[VR_idx];
4319 ++VR_idx;
4320
4321 RegsToPass.push_back(std::make_pair(VReg, Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004322 } else {
4323 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4324 true, isTailCall, true, MemOpChains,
4325 TailCallArguments, dl);
4326 ArgOffset += 16;
4327 }
4328 break;
4329 }
4330 }
4331
4332 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004333 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004334
4335 // Check if this is an indirect call (MTCTR/BCTRL).
4336 // See PrepareCall() for more information about calls through function
4337 // pointers in the 64-bit SVR4 ABI.
4338 if (!isTailCall &&
4339 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4340 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4341 !isBLACompatibleAddress(Callee, DAG)) {
4342 // Load r2 into a virtual register and store it to the TOC save area.
4343 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4344 // TOC save area offset.
4345 SDValue PtrOff = DAG.getIntPtrConstant(40);
4346 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4347 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4348 false, false, 0);
4349 // R12 must contain the address of an indirect callee. This does not
4350 // mean the MTCTR instruction must use R12; it's easier to model this
4351 // as an extra parameter, so do that.
4352 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4353 }
4354
4355 // Build a sequence of copy-to-reg nodes chained together with token chain
4356 // and flag operands which copy the outgoing args into the appropriate regs.
4357 SDValue InFlag;
4358 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4359 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4360 RegsToPass[i].second, InFlag);
4361 InFlag = Chain.getValue(1);
4362 }
4363
4364 if (isTailCall)
4365 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4366 FPOp, true, TailCallArguments);
4367
4368 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4369 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4370 Ins, InVals);
4371}
4372
4373SDValue
4374PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4375 CallingConv::ID CallConv, bool isVarArg,
4376 bool isTailCall,
4377 const SmallVectorImpl<ISD::OutputArg> &Outs,
4378 const SmallVectorImpl<SDValue> &OutVals,
4379 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004380 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004381 SmallVectorImpl<SDValue> &InVals) const {
4382
4383 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004384
Owen Anderson53aa7a92009-08-10 22:56:29 +00004385 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00004386 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004387 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004388
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004389 MachineFunction &MF = DAG.getMachineFunction();
4390
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004391 // Mark this function as potentially containing a function that contains a
4392 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4393 // and restoring the callers stack pointer in this functions epilog. This is
4394 // done because by tail calling the called function might overwrite the value
4395 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004396 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4397 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004398 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4399
4400 unsigned nAltivecParamsAtEnd = 0;
4401
Chris Lattneraa40ec12006-05-16 22:56:08 +00004402 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00004403 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00004404 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004405 unsigned NumBytes =
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004406 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004407 Outs, OutVals,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004408 nAltivecParamsAtEnd);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004409
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004410 // Calculate by how many bytes the stack has to be adjusted in case of tail
4411 // call optimization.
4412 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004413
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004414 // To protect arguments on the stack from being clobbered in a tail call,
4415 // force all the loads to happen before doing any other lowering.
4416 if (isTailCall)
4417 Chain = DAG.getStackArgumentTokenFactor(Chain);
4418
Chris Lattnerb7552a82006-05-17 00:15:40 +00004419 // Adjust the stack pointer for the new arguments...
4420 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004421 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4422 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004423 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004424
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004425 // Load the return address and frame pointer so it can be move somewhere else
4426 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004427 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004428 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4429 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004430
Chris Lattnerb7552a82006-05-17 00:15:40 +00004431 // Set up a copy of the stack pointer for use loading and storing any
4432 // arguments that may not fit in the registers available for argument
4433 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004434 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004435 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004436 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004437 else
Owen Anderson9f944592009-08-11 20:47:22 +00004438 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004439
Chris Lattnerb7552a82006-05-17 00:15:40 +00004440 // Figure out which arguments are going to go in registers, and which in
4441 // memory. Also, if this is a vararg function, floating point operations
4442 // must be stored to our stack, and loaded into integer regs as well, if
4443 // any integer regs are available for argument passing.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004444 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004445 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004446
Craig Topper840beec2014-04-04 05:16:06 +00004447 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004448 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4449 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4450 };
Craig Topper840beec2014-04-04 05:16:06 +00004451 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00004452 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4453 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4454 };
Craig Topper840beec2014-04-04 05:16:06 +00004455 static const MCPhysReg *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004456
Craig Topper840beec2014-04-04 05:16:06 +00004457 static const MCPhysReg VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004458 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4459 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4460 };
Owen Andersone2f23a32007-09-07 04:06:50 +00004461 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004462 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00004463 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004464
Craig Topper840beec2014-04-04 05:16:06 +00004465 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004466
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004467 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004468 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4469
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004470 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00004471 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004472 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004473 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004474
Chris Lattnerb7552a82006-05-17 00:15:40 +00004475 // PtrOff will be used to store the current argument to the stack if a
4476 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004477 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004478
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004479 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004480
Dale Johannesen679073b2009-02-04 02:34:38 +00004481 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004482
4483 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00004484 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00004485 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4486 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00004487 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004488 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00004489
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004490 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004491 // Note: "by value" is code for passing a structure by value, not
4492 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00004493 if (Flags.isByVal()) {
4494 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004495 // Very small objects are passed right-justified. Everything else is
4496 // passed left-justified.
4497 if (Size==1 || Size==2) {
4498 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004499 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00004500 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00004501 MachinePointerInfo(), VT,
4502 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004503 MemOpChains.push_back(Load.getValue(1));
4504 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004505
4506 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004507 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00004508 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4509 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004510 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004511 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4512 CallSeqStart,
4513 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004514 ArgOffset += PtrByteSize;
4515 }
4516 continue;
4517 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004518 // Copy entire object into memory. There are cases where gcc-generated
4519 // code assumes it is there, even if it could be put entirely into
4520 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004521 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4522 CallSeqStart,
4523 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004524
4525 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4526 // copy the pieces of the object that fit into registers from the
4527 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00004528 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004529 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004530 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00004531 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004532 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4533 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004534 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00004535 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00004536 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004537 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004538 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004539 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004540 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004541 }
4542 }
4543 continue;
4544 }
4545
Craig Topper56710102013-08-15 02:33:50 +00004546 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004547 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00004548 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00004549 case MVT::i32:
4550 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004551 if (GPR_idx != NumGPRs) {
Hal Finkel7f908e82014-03-06 00:45:19 +00004552 if (Arg.getValueType() == MVT::i1)
4553 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg);
4554
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004555 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004556 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004557 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4558 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004559 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00004560 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004561 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004562 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004563 case MVT::f32:
4564 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004565 if (FPR_idx != NumFPRs) {
4566 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4567
Chris Lattnerb7552a82006-05-17 00:15:40 +00004568 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00004569 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4570 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004571 MemOpChains.push_back(Store);
4572
Chris Lattnerb7552a82006-05-17 00:15:40 +00004573 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004574 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004575 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004576 MachinePointerInfo(), false, false,
4577 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004578 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004579 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004580 }
Owen Anderson9f944592009-08-11 20:47:22 +00004581 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004582 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004583 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00004584 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4585 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004586 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004587 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004588 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00004589 }
4590 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00004591 // If we have any FPRs remaining, we may also have GPRs remaining.
4592 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4593 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004594 if (GPR_idx != NumGPRs)
4595 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00004596 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004597 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4598 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004599 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004600 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004601 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4602 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004603 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004604 if (isPPC64)
4605 ArgOffset += 8;
4606 else
Owen Anderson9f944592009-08-11 20:47:22 +00004607 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004608 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004609 case MVT::v4f32:
4610 case MVT::v4i32:
4611 case MVT::v8i16:
4612 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00004613 if (isVarArg) {
4614 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00004615 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00004616 // V registers; in fact gcc does this only for arguments that are
4617 // prototyped, not for those that match the ... We do it for all
4618 // arguments, seems to work.
4619 while (ArgOffset % 16 !=0) {
4620 ArgOffset += PtrByteSize;
4621 if (GPR_idx != NumGPRs)
4622 GPR_idx++;
4623 }
4624 // We could elide this store in the case where the object fits
4625 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004626 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004627 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00004628 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4629 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004630 MemOpChains.push_back(Store);
4631 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004632 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00004633 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004634 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004635 MemOpChains.push_back(Load.getValue(1));
4636 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4637 }
4638 ArgOffset += 16;
4639 for (unsigned i=0; i<16; i+=PtrByteSize) {
4640 if (GPR_idx == NumGPRs)
4641 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00004642 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004643 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00004644 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004645 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004646 MemOpChains.push_back(Load.getValue(1));
4647 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4648 }
4649 break;
4650 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004651
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004652 // Non-varargs Altivec params generally go in registers, but have
4653 // stack space allocated at the end.
4654 if (VR_idx != NumVRs) {
4655 // Doesn't have GPR space allocated.
4656 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4657 } else if (nAltivecParamsAtEnd==0) {
4658 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004659 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4660 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004661 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004662 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00004663 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00004664 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004665 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00004666 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004667 // If all Altivec parameters fit in registers, as they usually do,
4668 // they get stack space following the non-Altivec parameters. We
4669 // don't track this here because nobody below needs it.
4670 // If there are more Altivec parameters than fit in registers emit
4671 // the stores here.
4672 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4673 unsigned j = 0;
4674 // Offset is aligned; skip 1st 12 params which go in V registers.
4675 ArgOffset = ((ArgOffset+15)/16)*16;
4676 ArgOffset += 12*16;
4677 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004678 SDValue Arg = OutVals[i];
4679 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00004680 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4681 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004682 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004683 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004684 // We are emitting Altivec params in order.
4685 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4686 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004687 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004688 ArgOffset += 16;
4689 }
4690 }
4691 }
4692 }
4693
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004694 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004695 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004696
Dale Johannesen90eab672010-03-09 20:15:42 +00004697 // On Darwin, R12 must contain the address of an indirect callee. This does
4698 // not mean the MTCTR instruction must use R12; it's easier to model this as
4699 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004700 if (!isTailCall &&
Dale Johannesen90eab672010-03-09 20:15:42 +00004701 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4702 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4703 !isBLACompatibleAddress(Callee, DAG))
4704 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4705 PPC::R12), Callee));
4706
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004707 // Build a sequence of copy-to-reg nodes chained together with token chain
4708 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004709 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004710 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00004711 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00004712 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004713 InFlag = Chain.getValue(1);
4714 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004715
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004716 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004717 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4718 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004719
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004720 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4721 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4722 Ins, InVals);
Chris Lattneraa40ec12006-05-16 22:56:08 +00004723}
4724
Hal Finkel450128a2011-10-14 19:51:36 +00004725bool
4726PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4727 MachineFunction &MF, bool isVarArg,
4728 const SmallVectorImpl<ISD::OutputArg> &Outs,
4729 LLVMContext &Context) const {
4730 SmallVector<CCValAssign, 16> RVLocs;
4731 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4732 RVLocs, Context);
4733 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4734}
4735
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004736SDValue
4737PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004738 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004739 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004740 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004741 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004742
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004743 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004744 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00004745 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004746 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004747
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004748 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004749 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004750
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004751 // Copy the result values into the output registers.
4752 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4753 CCValAssign &VA = RVLocs[i];
4754 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004755
4756 SDValue Arg = OutVals[i];
4757
4758 switch (VA.getLocInfo()) {
4759 default: llvm_unreachable("Unknown loc info!");
4760 case CCValAssign::Full: break;
4761 case CCValAssign::AExt:
4762 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4763 break;
4764 case CCValAssign::ZExt:
4765 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4766 break;
4767 case CCValAssign::SExt:
4768 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4769 break;
4770 }
4771
4772 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004773 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004774 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004775 }
4776
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004777 RetOps[0] = Chain; // Update chain.
4778
4779 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00004780 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004781 RetOps.push_back(Flag);
4782
Craig Topper48d114b2014-04-26 18:35:24 +00004783 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps);
Chris Lattner4211ca92006-04-14 06:01:58 +00004784}
4785
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004786SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004787 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00004788 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004789 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004790
Jim Laskeye4f4d042006-12-04 22:04:42 +00004791 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004792 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00004793
4794 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00004795 bool isPPC64 = Subtarget.isPPC64();
4796 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004797 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004798
4799 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004800 SDValue Chain = Op.getOperand(0);
4801 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004802
Jim Laskeye4f4d042006-12-04 22:04:42 +00004803 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00004804 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4805 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004806 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004807
Jim Laskeye4f4d042006-12-04 22:04:42 +00004808 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00004809 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004810
Jim Laskeye4f4d042006-12-04 22:04:42 +00004811 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00004812 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004813 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004814}
4815
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004816
4817
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004818SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004819PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004820 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004821 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004822 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004823 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004824
4825 // Get current frame pointer save index. The users of this index will be
4826 // primarily DYNALLOC instructions.
4827 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4828 int RASI = FI->getReturnAddrSaveIndex();
4829
4830 // If the frame pointer save index hasn't been defined yet.
4831 if (!RASI) {
4832 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004833 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004834 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004835 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004836 // Save the result.
4837 FI->setReturnAddrSaveIndex(RASI);
4838 }
4839 return DAG.getFrameIndex(RASI, PtrVT);
4840}
4841
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004842SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004843PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4844 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004845 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004846 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004847 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004848
4849 // Get current frame pointer save index. The users of this index will be
4850 // primarily DYNALLOC instructions.
4851 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4852 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004853
Jim Laskey48850c12006-11-16 22:43:37 +00004854 // If the frame pointer save index hasn't been defined yet.
4855 if (!FPSI) {
4856 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004857 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004858 isDarwinABI);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004859
Jim Laskey48850c12006-11-16 22:43:37 +00004860 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004861 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00004862 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004863 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00004864 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004865 return DAG.getFrameIndex(FPSI, PtrVT);
4866}
Jim Laskey48850c12006-11-16 22:43:37 +00004867
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004868SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004869 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004870 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004871 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004872 SDValue Chain = Op.getOperand(0);
4873 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004874 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004875
Jim Laskey48850c12006-11-16 22:43:37 +00004876 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004877 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004878 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004879 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00004880 DAG.getConstant(0, PtrVT), Size);
4881 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004882 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00004883 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004884 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00004885 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Craig Topper48d114b2014-04-26 18:35:24 +00004886 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops);
Jim Laskey48850c12006-11-16 22:43:37 +00004887}
4888
Hal Finkel756810f2013-03-21 21:37:52 +00004889SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
4890 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004891 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004892 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
4893 DAG.getVTList(MVT::i32, MVT::Other),
4894 Op.getOperand(0), Op.getOperand(1));
4895}
4896
4897SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
4898 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004899 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004900 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
4901 Op.getOperand(0), Op.getOperand(1));
4902}
4903
Hal Finkel940ab932014-02-28 00:27:01 +00004904SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
4905 assert(Op.getValueType() == MVT::i1 &&
4906 "Custom lowering only for i1 loads");
4907
4908 // First, load 8 bits into 32 bits, then truncate to 1 bit.
4909
4910 SDLoc dl(Op);
4911 LoadSDNode *LD = cast<LoadSDNode>(Op);
4912
4913 SDValue Chain = LD->getChain();
4914 SDValue BasePtr = LD->getBasePtr();
4915 MachineMemOperand *MMO = LD->getMemOperand();
4916
4917 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(), Chain,
4918 BasePtr, MVT::i8, MMO);
4919 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
4920
4921 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00004922 return DAG.getMergeValues(Ops, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00004923}
4924
4925SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
4926 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
4927 "Custom lowering only for i1 stores");
4928
4929 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
4930
4931 SDLoc dl(Op);
4932 StoreSDNode *ST = cast<StoreSDNode>(Op);
4933
4934 SDValue Chain = ST->getChain();
4935 SDValue BasePtr = ST->getBasePtr();
4936 SDValue Value = ST->getValue();
4937 MachineMemOperand *MMO = ST->getMemOperand();
4938
4939 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(), Value);
4940 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
4941}
4942
4943// FIXME: Remove this once the ANDI glue bug is fixed:
4944SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
4945 assert(Op.getValueType() == MVT::i1 &&
4946 "Custom lowering only for i1 results");
4947
4948 SDLoc DL(Op);
4949 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
4950 Op.getOperand(0));
4951}
4952
Chris Lattner4211ca92006-04-14 06:01:58 +00004953/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4954/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004955SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00004956 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00004957 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4958 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00004959 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004960
Hal Finkel81f87992013-04-07 22:11:09 +00004961 // We might be able to do better than this under some circumstances, but in
4962 // general, fsel-based lowering of select is a finite-math-only optimization.
4963 // For more information, see section F.3 of the 2.06 ISA specification.
4964 if (!DAG.getTarget().Options.NoInfsFPMath ||
4965 !DAG.getTarget().Options.NoNaNsFPMath)
4966 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004967
Hal Finkel81f87992013-04-07 22:11:09 +00004968 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004969
Owen Anderson53aa7a92009-08-10 22:56:29 +00004970 EVT ResVT = Op.getValueType();
4971 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004972 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4973 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004974 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004975
Chris Lattner4211ca92006-04-14 06:01:58 +00004976 // If the RHS of the comparison is a 0.0, we don't need to do the
4977 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00004978 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00004979 if (isFloatingPointZero(RHS))
4980 switch (CC) {
4981 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004982 case ISD::SETNE:
4983 std::swap(TV, FV);
4984 case ISD::SETEQ:
4985 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4986 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
4987 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
4988 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4989 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4990 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4991 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004992 case ISD::SETULT:
4993 case ISD::SETLT:
4994 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004995 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004996 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00004997 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4998 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004999 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005000 case ISD::SETUGT:
5001 case ISD::SETGT:
5002 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005003 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005004 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00005005 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5006 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005007 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005008 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005009 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005010
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005011 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00005012 switch (CC) {
5013 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00005014 case ISD::SETNE:
5015 std::swap(TV, FV);
5016 case ISD::SETEQ:
5017 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
5018 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5019 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
5020 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
5021 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
5022 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
5023 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5024 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005025 case ISD::SETULT:
5026 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005027 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005028 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5029 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005030 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005031 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005032 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005033 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005034 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5035 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005036 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005037 case ISD::SETUGT:
5038 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005039 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005040 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5041 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005042 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005043 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005044 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005045 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005046 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5047 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005048 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005049 }
Eli Friedman5806e182009-05-28 04:31:08 +00005050 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00005051}
5052
Chris Lattner57ee7c62007-11-28 18:44:47 +00005053// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005054SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005055 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00005056 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005057 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00005058 if (Src.getValueType() == MVT::f32)
5059 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00005060
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005061 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00005062 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005063 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00005064 case MVT::i32:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005065 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Hal Finkelf6d45f22013-04-01 17:52:07 +00005066 (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ :
5067 PPCISD::FCTIDZ),
Owen Anderson9f944592009-08-11 20:47:22 +00005068 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005069 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005070 case MVT::i64:
Hal Finkel3f88d082013-04-01 18:42:58 +00005071 assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) &&
5072 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00005073 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
5074 PPCISD::FCTIDUZ,
5075 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005076 break;
5077 }
Duncan Sands2a287912008-07-19 16:26:02 +00005078
Chris Lattner4211ca92006-04-14 06:01:58 +00005079 // Convert the FP value to an int value through memory.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005080 bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() &&
5081 (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT());
5082 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
5083 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
5084 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00005085
Chris Lattner06a49542007-10-15 20:14:52 +00005086 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005087 SDValue Chain;
5088 if (i32Stack) {
5089 MachineFunction &MF = DAG.getMachineFunction();
5090 MachineMemOperand *MMO =
5091 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
5092 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
5093 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00005094 DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005095 } else
5096 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
5097 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00005098
5099 // Result is a load from the stack slot. If loading 4 bytes, make sure to
5100 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005101 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00005102 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00005103 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkelf6d45f22013-04-01 17:52:07 +00005104 MPI = MachinePointerInfo();
5105 }
5106
5107 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooper82cd9e82011-11-08 18:42:53 +00005108 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005109}
5110
Hal Finkelf6d45f22013-04-01 17:52:07 +00005111SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005112 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005113 SDLoc dl(Op);
Dan Gohmand6819da2008-03-11 01:59:03 +00005114 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00005115 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005116 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00005117
Hal Finkel6a56b212014-03-05 22:14:00 +00005118 if (Op.getOperand(0).getValueType() == MVT::i1)
5119 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0),
5120 DAG.getConstantFP(1.0, Op.getValueType()),
5121 DAG.getConstantFP(0.0, Op.getValueType()));
5122
Hal Finkelf6d45f22013-04-01 17:52:07 +00005123 assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) &&
5124 "UINT_TO_FP is supported only with FPCVT");
5125
5126 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00005127 // Otherwise, convert to double-precision and then round.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005128 unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
5129 (Op.getOpcode() == ISD::UINT_TO_FP ?
5130 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
5131 (Op.getOpcode() == ISD::UINT_TO_FP ?
5132 PPCISD::FCFIDU : PPCISD::FCFID);
5133 MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
5134 MVT::f32 : MVT::f64;
5135
Owen Anderson9f944592009-08-11 20:47:22 +00005136 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005137 SDValue SINT = Op.getOperand(0);
5138 // When converting to single-precision, we actually need to convert
5139 // to double-precision first and then round to single-precision.
5140 // To avoid double-rounding effects during that operation, we have
5141 // to prepare the input operand. Bits that might be truncated when
5142 // converting to double-precision are replaced by a bit that won't
5143 // be lost at this stage, but is below the single-precision rounding
5144 // position.
5145 //
5146 // However, if -enable-unsafe-fp-math is in effect, accept double
5147 // rounding to avoid the extra overhead.
5148 if (Op.getValueType() == MVT::f32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005149 !PPCSubTarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005150 !DAG.getTarget().Options.UnsafeFPMath) {
5151
5152 // Twiddle input to make sure the low 11 bits are zero. (If this
5153 // is the case, we are guaranteed the value will fit into the 53 bit
5154 // mantissa of an IEEE double-precision value without rounding.)
5155 // If any of those low 11 bits were not zero originally, make sure
5156 // bit 12 (value 2048) is set instead, so that the final rounding
5157 // to single-precision gets the correct result.
5158 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5159 SINT, DAG.getConstant(2047, MVT::i64));
5160 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
5161 Round, DAG.getConstant(2047, MVT::i64));
5162 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
5163 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5164 Round, DAG.getConstant(-2048, MVT::i64));
5165
5166 // However, we cannot use that value unconditionally: if the magnitude
5167 // of the input value is small, the bit-twiddling we did above might
5168 // end up visibly changing the output. Fortunately, in that case, we
5169 // don't need to twiddle bits since the original input will convert
5170 // exactly to double-precision floating-point already. Therefore,
5171 // construct a conditional to use the original value if the top 11
5172 // bits are all sign-bit copies, and use the rounded value computed
5173 // above otherwise.
5174 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
5175 SINT, DAG.getConstant(53, MVT::i32));
5176 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
5177 Cond, DAG.getConstant(1, MVT::i64));
5178 Cond = DAG.getSetCC(dl, MVT::i32,
5179 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
5180
5181 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
5182 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00005183
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005184 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005185 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
5186
5187 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00005188 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005189 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005190 return FP;
5191 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005192
Owen Anderson9f944592009-08-11 20:47:22 +00005193 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005194 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005195 // Since we only generate this in 64-bit mode, we can take advantage of
5196 // 64-bit registers. In particular, sign extend the input value into the
5197 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
5198 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00005199 MachineFunction &MF = DAG.getMachineFunction();
5200 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005201 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005202
Hal Finkelbeb296b2013-03-31 10:12:51 +00005203 SDValue Ld;
Hal Finkelf6d45f22013-04-01 17:52:07 +00005204 if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) {
Hal Finkelbeb296b2013-03-31 10:12:51 +00005205 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
5206 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005207
Hal Finkelbeb296b2013-03-31 10:12:51 +00005208 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
5209 MachinePointerInfo::getFixedStack(FrameIdx),
5210 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00005211
Hal Finkelbeb296b2013-03-31 10:12:51 +00005212 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
5213 "Expected an i32 store");
5214 MachineMemOperand *MMO =
5215 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
5216 MachineMemOperand::MOLoad, 4, 4);
5217 SDValue Ops[] = { Store, FIdx };
Hal Finkelf6d45f22013-04-01 17:52:07 +00005218 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
5219 PPCISD::LFIWZX : PPCISD::LFIWAX,
5220 dl, DAG.getVTList(MVT::f64, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00005221 Ops, MVT::i32, MMO);
Hal Finkelbeb296b2013-03-31 10:12:51 +00005222 } else {
Hal Finkelf6d45f22013-04-01 17:52:07 +00005223 assert(PPCSubTarget.isPPC64() &&
5224 "i32->FP without LFIWAX supported only on PPC64");
5225
Hal Finkelbeb296b2013-03-31 10:12:51 +00005226 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
5227 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
5228
5229 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
5230 Op.getOperand(0));
5231
5232 // STD the extended value into the stack slot.
5233 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
5234 MachinePointerInfo::getFixedStack(FrameIdx),
5235 false, false, 0);
5236
5237 // Load the value as a double.
5238 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
5239 MachinePointerInfo::getFixedStack(FrameIdx),
5240 false, false, false, 0);
5241 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005242
Chris Lattner4211ca92006-04-14 06:01:58 +00005243 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005244 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
5245 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00005246 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005247 return FP;
5248}
5249
Dan Gohman21cea8a2010-04-17 15:26:15 +00005250SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
5251 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005252 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005253 /*
5254 The rounding mode is in bits 30:31 of FPSR, and has the following
5255 settings:
5256 00 Round to nearest
5257 01 Round to 0
5258 10 Round to +inf
5259 11 Round to -inf
5260
5261 FLT_ROUNDS, on the other hand, expects the following:
5262 -1 Undefined
5263 0 Round to 0
5264 1 Round to nearest
5265 2 Round to +inf
5266 3 Round to -inf
5267
5268 To perform the conversion, we do:
5269 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
5270 */
5271
5272 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005273 EVT VT = Op.getValueType();
5274 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005275
5276 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005277 EVT NodeTys[] = {
5278 MVT::f64, // return register
5279 MVT::Glue // unused in this context
5280 };
Craig Topper2d2aa0c2014-04-30 07:17:30 +00005281 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, None);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005282
5283 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00005284 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005285 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005286 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00005287 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005288
5289 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005290 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005291 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00005292 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005293 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005294
5295 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005296 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00005297 DAG.getNode(ISD::AND, dl, MVT::i32,
5298 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005299 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00005300 DAG.getNode(ISD::SRL, dl, MVT::i32,
5301 DAG.getNode(ISD::AND, dl, MVT::i32,
5302 DAG.getNode(ISD::XOR, dl, MVT::i32,
5303 CWD, DAG.getConstant(3, MVT::i32)),
5304 DAG.getConstant(3, MVT::i32)),
5305 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005306
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005307 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00005308 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005309
Duncan Sands13237ac2008-06-06 12:08:01 +00005310 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00005311 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005312}
5313
Dan Gohman21cea8a2010-04-17 15:26:15 +00005314SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005315 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005316 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005317 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00005318 assert(Op.getNumOperands() == 3 &&
5319 VT == Op.getOperand(1).getValueType() &&
5320 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005321
Chris Lattner601b8652006-09-20 03:47:40 +00005322 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005323 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005324 SDValue Lo = Op.getOperand(0);
5325 SDValue Hi = Op.getOperand(1);
5326 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005327 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005328
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005329 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005330 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005331 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5332 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5333 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5334 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005335 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005336 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5337 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5338 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005339 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005340 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005341}
5342
Dan Gohman21cea8a2010-04-17 15:26:15 +00005343SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005344 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005345 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00005346 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005347 assert(Op.getNumOperands() == 3 &&
5348 VT == Op.getOperand(1).getValueType() &&
5349 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005350
Dan Gohman8d2ead22008-03-07 20:36:53 +00005351 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005352 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005353 SDValue Lo = Op.getOperand(0);
5354 SDValue Hi = Op.getOperand(1);
5355 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005356 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005357
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005358 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005359 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005360 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5361 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5362 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5363 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005364 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005365 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5366 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5367 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005368 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005369 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005370}
5371
Dan Gohman21cea8a2010-04-17 15:26:15 +00005372SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005373 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005374 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005375 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005376 assert(Op.getNumOperands() == 3 &&
5377 VT == Op.getOperand(1).getValueType() &&
5378 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005379
Dan Gohman8d2ead22008-03-07 20:36:53 +00005380 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005381 SDValue Lo = Op.getOperand(0);
5382 SDValue Hi = Op.getOperand(1);
5383 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005384 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005385
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005386 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005387 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005388 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5389 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5390 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5391 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005392 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005393 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5394 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5395 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00005396 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005397 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005398 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005399}
5400
5401//===----------------------------------------------------------------------===//
5402// Vector related lowering.
5403//
5404
Chris Lattner2a099c02006-04-17 06:00:21 +00005405/// BuildSplatI - Build a canonical splati of Val with an element size of
5406/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005407static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005408 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00005409 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005410
Owen Anderson53aa7a92009-08-10 22:56:29 +00005411 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00005412 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00005413 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005414
Owen Anderson9f944592009-08-11 20:47:22 +00005415 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005416
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005417 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5418 if (Val == -1)
5419 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005420
Owen Anderson53aa7a92009-08-10 22:56:29 +00005421 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005422
Chris Lattner2a099c02006-04-17 06:00:21 +00005423 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00005424 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005425 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00005426 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Craig Topper48d114b2014-04-26 18:35:24 +00005427 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005428 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005429}
5430
Hal Finkelcf2e9082013-05-24 23:00:14 +00005431/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5432/// specified intrinsic ID.
5433static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005434 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00005435 EVT DestVT = MVT::Other) {
5436 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5437 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5438 DAG.getConstant(IID, MVT::i32), Op);
5439}
5440
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005441/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00005442/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005443static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005444 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005445 EVT DestVT = MVT::Other) {
5446 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005447 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005448 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005449}
5450
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005451/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5452/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005453static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005454 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005455 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00005456 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005457 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005458 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005459}
5460
5461
Chris Lattner264c9082006-04-17 17:55:10 +00005462/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5463/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005464static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005465 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00005466 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00005467 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5468 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005469
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005470 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00005471 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005472 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00005473 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005474 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00005475}
5476
Chris Lattner19e90552006-04-14 05:19:18 +00005477// If this is a case we can't handle, return null and let the default
5478// expansion code take care of it. If we CAN select this case, and if it
5479// selects to a single instruction, return Op. Otherwise, if we can codegen
5480// this case more efficiently than a constant pool load, lower it to the
5481// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005482SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5483 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005484 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005485 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
Craig Toppere73658d2014-04-28 04:05:08 +00005486 assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00005487
Bob Wilson85cefe82009-03-02 23:24:16 +00005488 // Check if this is a splat of a constant value.
5489 APInt APSplatBits, APSplatUndef;
5490 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005491 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00005492 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00005493 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00005494 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00005495
Bob Wilson530e0382009-03-03 19:26:27 +00005496 unsigned SplatBits = APSplatBits.getZExtValue();
5497 unsigned SplatUndef = APSplatUndef.getZExtValue();
5498 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005499
Bob Wilson530e0382009-03-03 19:26:27 +00005500 // First, handle single instruction cases.
5501
5502 // All zeros?
5503 if (SplatBits == 0) {
5504 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00005505 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5506 SDValue Z = DAG.getConstant(0, MVT::i32);
5507 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00005508 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00005509 }
Bob Wilson530e0382009-03-03 19:26:27 +00005510 return Op;
5511 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00005512
Bob Wilson530e0382009-03-03 19:26:27 +00005513 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5514 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5515 (32-SplatBitSize));
5516 if (SextVal >= -16 && SextVal <= 15)
5517 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005518
5519
Bob Wilson530e0382009-03-03 19:26:27 +00005520 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005521
Bob Wilson530e0382009-03-03 19:26:27 +00005522 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00005523 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5524 // If this value is in the range [17,31] and is odd, use:
5525 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5526 // If this value is in the range [-31,-17] and is odd, use:
5527 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5528 // Note the last two are three-instruction sequences.
5529 if (SextVal >= -32 && SextVal <= 31) {
5530 // To avoid having these optimizations undone by constant folding,
5531 // we convert to a pseudo that will be expanded later into one of
5532 // the above forms.
5533 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt51e79512013-02-20 15:50:31 +00005534 EVT VT = Op.getValueType();
5535 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5536 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5537 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilson530e0382009-03-03 19:26:27 +00005538 }
5539
5540 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5541 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5542 // for fneg/fabs.
5543 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5544 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00005545 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005546
5547 // Make the VSLW intrinsic, computing 0x8000_0000.
5548 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5549 OnesV, DAG, dl);
5550
5551 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00005552 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00005553 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005554 }
5555
5556 // Check to see if this is a wide variety of vsplti*, binop self cases.
5557 static const signed char SplatCsts[] = {
5558 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5559 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5560 };
5561
5562 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5563 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5564 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5565 int i = SplatCsts[idx];
5566
5567 // Figure out what shift amount will be used by altivec if shifted by i in
5568 // this splat size.
5569 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5570
5571 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00005572 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005573 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005574 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5575 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5576 Intrinsic::ppc_altivec_vslw
5577 };
5578 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005579 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005580 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005581
Bob Wilson530e0382009-03-03 19:26:27 +00005582 // vsplti + srl self.
5583 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005584 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005585 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5586 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5587 Intrinsic::ppc_altivec_vsrw
5588 };
5589 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005590 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005591 }
5592
Bob Wilson530e0382009-03-03 19:26:27 +00005593 // vsplti + sra self.
5594 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005595 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005596 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5597 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5598 Intrinsic::ppc_altivec_vsraw
5599 };
5600 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005601 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005602 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005603
Bob Wilson530e0382009-03-03 19:26:27 +00005604 // vsplti + rol self.
5605 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5606 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005607 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005608 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5609 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5610 Intrinsic::ppc_altivec_vrlw
5611 };
5612 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005613 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005614 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005615
Bob Wilson530e0382009-03-03 19:26:27 +00005616 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00005617 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005618 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005619 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00005620 }
Bob Wilson530e0382009-03-03 19:26:27 +00005621 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00005622 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005623 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005624 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00005625 }
Bob Wilson530e0382009-03-03 19:26:27 +00005626 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00005627 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005628 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005629 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5630 }
5631 }
5632
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005633 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00005634}
5635
Chris Lattner071ad012006-04-17 05:28:54 +00005636/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5637/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005638static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005639 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005640 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00005641 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00005642 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00005643 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005644
Chris Lattner071ad012006-04-17 05:28:54 +00005645 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00005646 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 +00005647 OP_VMRGHW,
5648 OP_VMRGLW,
5649 OP_VSPLTISW0,
5650 OP_VSPLTISW1,
5651 OP_VSPLTISW2,
5652 OP_VSPLTISW3,
5653 OP_VSLDOI4,
5654 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00005655 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00005656 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00005657
Chris Lattner071ad012006-04-17 05:28:54 +00005658 if (OpNum == OP_COPY) {
5659 if (LHSID == (1*9+2)*9+3) return LHS;
5660 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5661 return RHS;
5662 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005663
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005664 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005665 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5666 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005667
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005668 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00005669 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005670 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00005671 case OP_VMRGHW:
5672 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5673 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5674 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5675 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5676 break;
5677 case OP_VMRGLW:
5678 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5679 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5680 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5681 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5682 break;
5683 case OP_VSPLTISW0:
5684 for (unsigned i = 0; i != 16; ++i)
5685 ShufIdxs[i] = (i&3)+0;
5686 break;
5687 case OP_VSPLTISW1:
5688 for (unsigned i = 0; i != 16; ++i)
5689 ShufIdxs[i] = (i&3)+4;
5690 break;
5691 case OP_VSPLTISW2:
5692 for (unsigned i = 0; i != 16; ++i)
5693 ShufIdxs[i] = (i&3)+8;
5694 break;
5695 case OP_VSPLTISW3:
5696 for (unsigned i = 0; i != 16; ++i)
5697 ShufIdxs[i] = (i&3)+12;
5698 break;
5699 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005700 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005701 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005702 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005703 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005704 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005705 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00005706 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00005707 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5708 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005709 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00005710 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00005711}
5712
Chris Lattner19e90552006-04-14 05:19:18 +00005713/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5714/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5715/// return the code it can be lowered into. Worst case, it can always be
5716/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005717SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005718 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005719 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005720 SDValue V1 = Op.getOperand(0);
5721 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005722 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005723 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005724
Chris Lattner19e90552006-04-14 05:19:18 +00005725 // Cases that are handled by instructions that take permute immediates
5726 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5727 // selected by the instruction selector.
5728 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005729 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5730 PPC::isSplatShuffleMask(SVOp, 2) ||
5731 PPC::isSplatShuffleMask(SVOp, 4) ||
5732 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5733 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5734 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5735 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5736 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5737 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5738 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5739 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5740 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattner19e90552006-04-14 05:19:18 +00005741 return Op;
5742 }
5743 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005744
Chris Lattner19e90552006-04-14 05:19:18 +00005745 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5746 // and produce a fixed permutation. If any of these match, do not lower to
5747 // VPERM.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005748 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5749 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5750 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5751 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5752 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5753 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5754 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5755 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5756 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattner19e90552006-04-14 05:19:18 +00005757 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005758
Chris Lattner071ad012006-04-17 05:28:54 +00005759 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5760 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005761 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00005762
Chris Lattner071ad012006-04-17 05:28:54 +00005763 unsigned PFIndexes[4];
5764 bool isFourElementShuffle = true;
5765 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5766 unsigned EltNo = 8; // Start out undef.
5767 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005768 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00005769 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005770
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005771 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00005772 if ((ByteSource & 3) != j) {
5773 isFourElementShuffle = false;
5774 break;
5775 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005776
Chris Lattner071ad012006-04-17 05:28:54 +00005777 if (EltNo == 8) {
5778 EltNo = ByteSource/4;
5779 } else if (EltNo != ByteSource/4) {
5780 isFourElementShuffle = false;
5781 break;
5782 }
5783 }
5784 PFIndexes[i] = EltNo;
5785 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005786
5787 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00005788 // perfect shuffle vector to determine if it is cost effective to do this as
5789 // discrete instructions, or whether we should use a vperm.
5790 if (isFourElementShuffle) {
5791 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005792 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00005793 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005794
Chris Lattner071ad012006-04-17 05:28:54 +00005795 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5796 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005797
Chris Lattner071ad012006-04-17 05:28:54 +00005798 // Determining when to avoid vperm is tricky. Many things affect the cost
5799 // of vperm, particularly how many times the perm mask needs to be computed.
5800 // For example, if the perm mask can be hoisted out of a loop or is already
5801 // used (perhaps because there are multiple permutes with the same shuffle
5802 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5803 // the loop requires an extra register.
5804 //
5805 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00005806 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00005807 // available, if this block is within a loop, we should avoid using vperm
5808 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005809 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005810 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005811 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005812
Chris Lattner19e90552006-04-14 05:19:18 +00005813 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5814 // vector that will get spilled to the constant pool.
5815 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005816
Chris Lattner19e90552006-04-14 05:19:18 +00005817 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5818 // that it is in input element units, not in bytes. Convert now.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005819 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005820 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005821
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005822 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005823 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5824 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005825
Chris Lattner19e90552006-04-14 05:19:18 +00005826 for (unsigned j = 0; j != BytesPerElement; ++j)
5827 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson9f944592009-08-11 20:47:22 +00005828 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00005829 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005830
Owen Anderson9f944592009-08-11 20:47:22 +00005831 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Craig Topper48d114b2014-04-26 18:35:24 +00005832 ResultMask);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005833 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00005834}
5835
Chris Lattner9754d142006-04-18 17:59:36 +00005836/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5837/// altivec comparison. If it is, return true and fill in Opc/isDot with
5838/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005839static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner9754d142006-04-18 17:59:36 +00005840 bool &isDot) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005841 unsigned IntrinsicID =
5842 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00005843 CompareOpc = -1;
5844 isDot = false;
5845 switch (IntrinsicID) {
5846 default: return false;
5847 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00005848 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5849 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5850 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5851 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5852 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5853 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5854 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5855 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5856 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5857 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5858 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5859 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5860 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005861
Chris Lattner4211ca92006-04-14 06:01:58 +00005862 // Normal Comparisons.
5863 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5864 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5865 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5866 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5867 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5868 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5869 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5870 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5871 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5872 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5873 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5874 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5875 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5876 }
Chris Lattner9754d142006-04-18 17:59:36 +00005877 return true;
5878}
5879
5880/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5881/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005882SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005883 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00005884 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5885 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005886 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00005887 int CompareOpc;
5888 bool isDot;
5889 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005890 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005891
Chris Lattner9754d142006-04-18 17:59:36 +00005892 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00005893 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00005894 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00005895 Op.getOperand(1), Op.getOperand(2),
5896 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00005897 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00005898 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005899
Chris Lattner4211ca92006-04-14 06:01:58 +00005900 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005901 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005902 Op.getOperand(2), // LHS
5903 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00005904 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005905 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005906 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00005907 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005908
Chris Lattner4211ca92006-04-14 06:01:58 +00005909 // Now that we have the comparison, emit a copy from the CR to a GPR.
5910 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00005911 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00005912 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00005913 CompNode.getValue(1));
5914
Chris Lattner4211ca92006-04-14 06:01:58 +00005915 // Unpack the result based on how the target uses it.
5916 unsigned BitNo; // Bit # of CR6.
5917 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00005918 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00005919 default: // Can't happen, don't crash on invalid number though.
5920 case 0: // Return the value of the EQ bit of CR6.
5921 BitNo = 0; InvertBit = false;
5922 break;
5923 case 1: // Return the inverted value of the EQ bit of CR6.
5924 BitNo = 0; InvertBit = true;
5925 break;
5926 case 2: // Return the value of the LT bit of CR6.
5927 BitNo = 2; InvertBit = false;
5928 break;
5929 case 3: // Return the inverted value of the LT bit of CR6.
5930 BitNo = 2; InvertBit = true;
5931 break;
5932 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005933
Chris Lattner4211ca92006-04-14 06:01:58 +00005934 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00005935 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5936 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005937 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00005938 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5939 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00005940
Chris Lattner4211ca92006-04-14 06:01:58 +00005941 // If we are supposed to, toggle the bit.
5942 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00005943 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5944 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005945 return Flags;
5946}
5947
Hal Finkel5c0d1452014-03-30 13:22:59 +00005948SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
5949 SelectionDAG &DAG) const {
5950 SDLoc dl(Op);
5951 // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int
5952 // instructions), but for smaller types, we need to first extend up to v2i32
5953 // before doing going farther.
5954 if (Op.getValueType() == MVT::v2i64) {
5955 EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
5956 if (ExtVT != MVT::v2i32) {
5957 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0));
5958 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op,
5959 DAG.getValueType(EVT::getVectorVT(*DAG.getContext(),
5960 ExtVT.getVectorElementType(), 4)));
5961 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op);
5962 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op,
5963 DAG.getValueType(MVT::v2i32));
5964 }
5965
5966 return Op;
5967 }
5968
5969 return SDValue();
5970}
5971
Scott Michelcf0da6c2009-02-17 22:15:04 +00005972SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005973 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005974 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00005975 // Create a stack slot that is 16-byte aligned.
5976 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00005977 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00005978 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005979 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005980
Chris Lattner4211ca92006-04-14 06:01:58 +00005981 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00005982 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00005983 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005984 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005985 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00005986 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005987 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005988}
5989
Dan Gohman21cea8a2010-04-17 15:26:15 +00005990SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005991 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005992 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005993 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005994
Owen Anderson9f944592009-08-11 20:47:22 +00005995 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
5996 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005997
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005998 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005999 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006000
Chris Lattner7e4398742006-04-18 03:43:48 +00006001 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00006002 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
6003 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
6004 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006005
Chris Lattner7e4398742006-04-18 03:43:48 +00006006 // Low parts multiplied together, generating 32-bit results (we ignore the
6007 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006008 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00006009 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006010
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006011 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00006012 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00006013 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006014 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006015 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00006016 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
6017 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006018 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006019
Owen Anderson9f944592009-08-11 20:47:22 +00006020 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00006021
Chris Lattner96d50482006-04-18 04:28:57 +00006022 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006023 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00006024 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006025 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006026
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006027 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006028 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00006029 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00006030 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006031
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006032 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006033 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00006034 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00006035 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006036
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006037 // Merge the results together.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006038 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006039 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006040 Ops[i*2 ] = 2*i+1;
6041 Ops[i*2+1] = 2*i+1+16;
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006042 }
Owen Anderson9f944592009-08-11 20:47:22 +00006043 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00006044 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006045 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00006046 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006047}
6048
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006049/// LowerOperation - Provide custom lowering hooks for some operations.
6050///
Dan Gohman21cea8a2010-04-17 15:26:15 +00006051SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006052 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006053 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00006054 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00006055 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006056 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00006057 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00006058 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006059 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00006060 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
6061 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006062 case ISD::VASTART:
Dan Gohman31ae5862010-04-17 14:41:14 +00006063 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006064
6065 case ISD::VAARG:
Dan Gohman31ae5862010-04-17 14:41:14 +00006066 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00006067
Roman Divackyc3825df2013-07-25 21:36:47 +00006068 case ISD::VACOPY:
6069 return LowerVACOPY(Op, DAG, PPCSubTarget);
6070
Jim Laskeye4f4d042006-12-04 22:04:42 +00006071 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00006072 case ISD::DYNAMIC_STACKALLOC:
6073 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng51096af2008-04-19 01:30:48 +00006074
Hal Finkel756810f2013-03-21 21:37:52 +00006075 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
6076 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
6077
Hal Finkel940ab932014-02-28 00:27:01 +00006078 case ISD::LOAD: return LowerLOAD(Op, DAG);
6079 case ISD::STORE: return LowerSTORE(Op, DAG);
6080 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006081 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006082 case ISD::FP_TO_UINT:
6083 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006084 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00006085 case ISD::UINT_TO_FP:
6086 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00006087 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006088
Chris Lattner4211ca92006-04-14 06:01:58 +00006089 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00006090 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
6091 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
6092 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006093
Chris Lattner4211ca92006-04-14 06:01:58 +00006094 // Vector-related lowering.
6095 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6096 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6097 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6098 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Hal Finkel5c0d1452014-03-30 13:22:59 +00006099 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006100 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006101
Hal Finkel25c19922013-05-15 21:37:41 +00006102 // For counter-based loop handling.
6103 case ISD::INTRINSIC_W_CHAIN: return SDValue();
6104
Chris Lattnerf6a81562007-12-08 06:59:59 +00006105 // Frame & Return address.
6106 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00006107 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00006108 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006109}
6110
Duncan Sands6ed40142008-12-01 11:39:25 +00006111void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
6112 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006113 SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00006114 const TargetMachine &TM = getTargetMachine();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006115 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00006116 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00006117 default:
Craig Toppere55c5562012-02-07 02:50:20 +00006118 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkel25c19922013-05-15 21:37:41 +00006119 case ISD::INTRINSIC_W_CHAIN: {
6120 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
6121 Intrinsic::ppc_is_decremented_ctr_nonzero)
6122 break;
6123
6124 assert(N->getValueType(0) == MVT::i1 &&
6125 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00006126 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00006127 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
6128 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
6129 N->getOperand(1));
6130
6131 Results.push_back(NewInt);
6132 Results.push_back(NewInt.getValue(1));
6133 break;
6134 }
Roman Divacky4394e682011-06-28 15:30:42 +00006135 case ISD::VAARG: {
6136 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
6137 || TM.getSubtarget<PPCSubtarget>().isPPC64())
6138 return;
6139
6140 EVT VT = N->getValueType(0);
6141
6142 if (VT == MVT::i64) {
6143 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
6144
6145 Results.push_back(NewNode);
6146 Results.push_back(NewNode.getValue(1));
6147 }
6148 return;
6149 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006150 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00006151 assert(N->getValueType(0) == MVT::ppcf128);
6152 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006153 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006154 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006155 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00006156 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006157 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006158 DAG.getIntPtrConstant(1));
6159
Ulrich Weigand874fc622013-03-26 10:56:22 +00006160 // Add the two halves of the long double in round-to-zero mode.
6161 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00006162
6163 // We know the low half is about to be thrown away, so just use something
6164 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00006165 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00006166 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00006167 return;
Duncan Sands2a287912008-07-19 16:26:02 +00006168 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006169 case ISD::FP_TO_SINT:
Bill Schmidt41221692013-07-09 18:50:20 +00006170 // LowerFP_TO_INT() can only handle f32 and f64.
6171 if (N->getOperand(0).getValueType() == MVT::ppcf128)
6172 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006173 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00006174 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00006175 }
6176}
6177
6178
Chris Lattner4211ca92006-04-14 06:01:58 +00006179//===----------------------------------------------------------------------===//
6180// Other Lowering Code
6181//===----------------------------------------------------------------------===//
6182
Chris Lattner9b577f12005-08-26 21:23:58 +00006183MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00006184PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman747e55b2009-02-07 16:15:20 +00006185 bool is64bit, unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006186 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesend4eb0522008-08-25 22:34:37 +00006187 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6188
6189 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6190 MachineFunction *F = BB->getParent();
6191 MachineFunction::iterator It = BB;
6192 ++It;
6193
6194 unsigned dest = MI->getOperand(0).getReg();
6195 unsigned ptrA = MI->getOperand(1).getReg();
6196 unsigned ptrB = MI->getOperand(2).getReg();
6197 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006198 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00006199
6200 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6201 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6202 F->insert(It, loopMBB);
6203 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006204 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006205 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006206 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006207
6208 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006209 unsigned TmpReg = (!BinOpcode) ? incr :
6210 RegInfo.createVirtualRegister(
Dale Johannesenbc698292008-09-02 20:30:23 +00006211 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6212 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006213
6214 // thisMBB:
6215 // ...
6216 // fallthrough --> loopMBB
6217 BB->addSuccessor(loopMBB);
6218
6219 // loopMBB:
6220 // l[wd]arx dest, ptr
6221 // add r0, dest, incr
6222 // st[wd]cx. r0, ptr
6223 // bne- loopMBB
6224 // fallthrough --> exitMBB
6225 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006226 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00006227 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006228 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006229 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
6230 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesend4eb0522008-08-25 22:34:37 +00006231 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006232 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006233 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006234 BB->addSuccessor(loopMBB);
6235 BB->addSuccessor(exitMBB);
6236
6237 // exitMBB:
6238 // ...
6239 BB = exitMBB;
6240 return BB;
6241}
6242
6243MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00006244PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00006245 MachineBasicBlock *BB,
6246 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00006247 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006248 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesena32affb2008-08-28 17:53:09 +00006249 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6250 // In 64 bit mode we have to use 64 bits for addresses, even though the
6251 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
6252 // registers without caring whether they're 32 or 64, but here we're
6253 // doing actual arithmetic on the addresses.
6254 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00006255 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00006256
6257 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6258 MachineFunction *F = BB->getParent();
6259 MachineFunction::iterator It = BB;
6260 ++It;
6261
6262 unsigned dest = MI->getOperand(0).getReg();
6263 unsigned ptrA = MI->getOperand(1).getReg();
6264 unsigned ptrB = MI->getOperand(2).getReg();
6265 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006266 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00006267
6268 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6269 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6270 F->insert(It, loopMBB);
6271 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006272 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006273 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006274 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006275
6276 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006277 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006278 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6279 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00006280 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6281 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6282 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6283 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
6284 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6285 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6286 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6287 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6288 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
6289 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006290 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006291 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006292 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006293
6294 // thisMBB:
6295 // ...
6296 // fallthrough --> loopMBB
6297 BB->addSuccessor(loopMBB);
6298
6299 // The 4-byte load must be aligned, while a char or short may be
6300 // anywhere in the word. Hence all this nasty bookkeeping code.
6301 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6302 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006303 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00006304 // rlwinm ptr, ptr1, 0, 0, 29
6305 // slw incr2, incr, shift
6306 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6307 // slw mask, mask2, shift
6308 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00006309 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006310 // add tmp, tmpDest, incr2
6311 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00006312 // and tmp3, tmp, mask
6313 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00006314 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00006315 // bne- loopMBB
6316 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006317 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006318 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00006319 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006320 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006321 .addReg(ptrA).addReg(ptrB);
6322 } else {
6323 Ptr1Reg = ptrB;
6324 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006325 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006326 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006327 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006328 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6329 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006330 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006331 .addReg(Ptr1Reg).addImm(0).addImm(61);
6332 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006333 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006334 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006335 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006336 .addReg(incr).addReg(ShiftReg);
6337 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006338 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00006339 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006340 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6341 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00006342 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006343 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006344 .addReg(Mask2Reg).addReg(ShiftReg);
6345
6346 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006347 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006348 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006349 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006350 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006351 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006352 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006353 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006354 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006355 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006356 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006357 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00006358 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006359 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006360 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006361 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006362 BB->addSuccessor(loopMBB);
6363 BB->addSuccessor(exitMBB);
6364
6365 // exitMBB:
6366 // ...
6367 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006368 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6369 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00006370 return BB;
6371}
6372
Hal Finkel756810f2013-03-21 21:37:52 +00006373llvm::MachineBasicBlock*
6374PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6375 MachineBasicBlock *MBB) const {
6376 DebugLoc DL = MI->getDebugLoc();
6377 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6378
6379 MachineFunction *MF = MBB->getParent();
6380 MachineRegisterInfo &MRI = MF->getRegInfo();
6381
6382 const BasicBlock *BB = MBB->getBasicBlock();
6383 MachineFunction::iterator I = MBB;
6384 ++I;
6385
6386 // Memory Reference
6387 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6388 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6389
6390 unsigned DstReg = MI->getOperand(0).getReg();
6391 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6392 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6393 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6394 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6395
6396 MVT PVT = getPointerTy();
6397 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6398 "Invalid Pointer Size!");
6399 // For v = setjmp(buf), we generate
6400 //
6401 // thisMBB:
6402 // SjLjSetup mainMBB
6403 // bl mainMBB
6404 // v_restore = 1
6405 // b sinkMBB
6406 //
6407 // mainMBB:
6408 // buf[LabelOffset] = LR
6409 // v_main = 0
6410 //
6411 // sinkMBB:
6412 // v = phi(main, restore)
6413 //
6414
6415 MachineBasicBlock *thisMBB = MBB;
6416 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6417 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6418 MF->insert(I, mainMBB);
6419 MF->insert(I, sinkMBB);
6420
6421 MachineInstrBuilder MIB;
6422
6423 // Transfer the remainder of BB and its successor edges to sinkMBB.
6424 sinkMBB->splice(sinkMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006425 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Hal Finkel756810f2013-03-21 21:37:52 +00006426 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6427
6428 // Note that the structure of the jmp_buf used here is not compatible
6429 // with that used by libc, and is not designed to be. Specifically, it
6430 // stores only those 'reserved' registers that LLVM does not otherwise
6431 // understand how to spill. Also, by convention, by the time this
6432 // intrinsic is called, Clang has already stored the frame address in the
6433 // first slot of the buffer and stack address in the third. Following the
6434 // X86 target code, we'll store the jump address in the second slot. We also
6435 // need to save the TOC pointer (R2) to handle jumps between shared
6436 // libraries, and that will be stored in the fourth slot. The thread
6437 // identifier (R13) is not affected.
6438
6439 // thisMBB:
6440 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6441 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006442 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006443
6444 // Prepare IP either in reg.
6445 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6446 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6447 unsigned BufReg = MI->getOperand(1).getReg();
6448
6449 if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) {
6450 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6451 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006452 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006453 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006454 MIB.setMemRefs(MMOBegin, MMOEnd);
6455 }
6456
Hal Finkelf05d6c72013-07-17 23:50:51 +00006457 // Naked functions never have a base pointer, and so we use r1. For all
6458 // other functions, this decision must be delayed until during PEI.
6459 unsigned BaseReg;
6460 if (MF->getFunction()->getAttributes().hasAttribute(
6461 AttributeSet::FunctionIndex, Attribute::Naked))
6462 BaseReg = PPCSubTarget.isPPC64() ? PPC::X1 : PPC::R1;
6463 else
6464 BaseReg = PPCSubTarget.isPPC64() ? PPC::BP8 : PPC::BP;
6465
6466 MIB = BuildMI(*thisMBB, MI, DL,
6467 TII->get(PPCSubTarget.isPPC64() ? PPC::STD : PPC::STW))
6468 .addReg(BaseReg)
6469 .addImm(BPOffset)
6470 .addReg(BufReg);
6471 MIB.setMemRefs(MMOBegin, MMOEnd);
6472
Hal Finkel756810f2013-03-21 21:37:52 +00006473 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00006474 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling5e7656b2013-06-07 07:55:53 +00006475 const PPCRegisterInfo *TRI =
6476 static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo());
6477 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00006478
6479 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6480
6481 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6482 .addMBB(mainMBB);
6483 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6484
6485 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6486 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6487
6488 // mainMBB:
6489 // mainDstReg = 0
6490 MIB = BuildMI(mainMBB, DL,
6491 TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
6492
6493 // Store IP
6494 if (PPCSubTarget.isPPC64()) {
6495 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6496 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006497 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006498 .addReg(BufReg);
6499 } else {
6500 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6501 .addReg(LabelReg)
6502 .addImm(LabelOffset)
6503 .addReg(BufReg);
6504 }
6505
6506 MIB.setMemRefs(MMOBegin, MMOEnd);
6507
6508 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6509 mainMBB->addSuccessor(sinkMBB);
6510
6511 // sinkMBB:
6512 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6513 TII->get(PPC::PHI), DstReg)
6514 .addReg(mainDstReg).addMBB(mainMBB)
6515 .addReg(restoreDstReg).addMBB(thisMBB);
6516
6517 MI->eraseFromParent();
6518 return sinkMBB;
6519}
6520
6521MachineBasicBlock *
6522PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6523 MachineBasicBlock *MBB) const {
6524 DebugLoc DL = MI->getDebugLoc();
6525 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6526
6527 MachineFunction *MF = MBB->getParent();
6528 MachineRegisterInfo &MRI = MF->getRegInfo();
6529
6530 // Memory Reference
6531 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6532 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6533
6534 MVT PVT = getPointerTy();
6535 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6536 "Invalid Pointer Size!");
6537
6538 const TargetRegisterClass *RC =
6539 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6540 unsigned Tmp = MRI.createVirtualRegister(RC);
6541 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6542 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6543 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006544 unsigned BP = (PVT == MVT::i64) ? PPC::X30 : PPC::R30;
Hal Finkel756810f2013-03-21 21:37:52 +00006545
6546 MachineInstrBuilder MIB;
6547
6548 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6549 const int64_t SPOffset = 2 * PVT.getStoreSize();
6550 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006551 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006552
6553 unsigned BufReg = MI->getOperand(0).getReg();
6554
6555 // Reload FP (the jumped-to function may not have had a
6556 // frame pointer, and if so, then its r31 will be restored
6557 // as necessary).
6558 if (PVT == MVT::i64) {
6559 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6560 .addImm(0)
6561 .addReg(BufReg);
6562 } else {
6563 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6564 .addImm(0)
6565 .addReg(BufReg);
6566 }
6567 MIB.setMemRefs(MMOBegin, MMOEnd);
6568
6569 // Reload IP
6570 if (PVT == MVT::i64) {
6571 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006572 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006573 .addReg(BufReg);
6574 } else {
6575 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6576 .addImm(LabelOffset)
6577 .addReg(BufReg);
6578 }
6579 MIB.setMemRefs(MMOBegin, MMOEnd);
6580
6581 // Reload SP
6582 if (PVT == MVT::i64) {
6583 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006584 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006585 .addReg(BufReg);
6586 } else {
6587 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6588 .addImm(SPOffset)
6589 .addReg(BufReg);
6590 }
6591 MIB.setMemRefs(MMOBegin, MMOEnd);
6592
Hal Finkelf05d6c72013-07-17 23:50:51 +00006593 // Reload BP
6594 if (PVT == MVT::i64) {
6595 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
6596 .addImm(BPOffset)
6597 .addReg(BufReg);
6598 } else {
6599 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
6600 .addImm(BPOffset)
6601 .addReg(BufReg);
6602 }
6603 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00006604
6605 // Reload TOC
6606 if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) {
6607 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006608 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006609 .addReg(BufReg);
6610
6611 MIB.setMemRefs(MMOBegin, MMOEnd);
6612 }
6613
6614 // Jump
6615 BuildMI(*MBB, MI, DL,
6616 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6617 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6618
6619 MI->eraseFromParent();
6620 return MBB;
6621}
6622
Dale Johannesena32affb2008-08-28 17:53:09 +00006623MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00006624PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00006625 MachineBasicBlock *BB) const {
Hal Finkel756810f2013-03-21 21:37:52 +00006626 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6627 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6628 return emitEHSjLjSetJmp(MI, BB);
6629 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6630 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6631 return emitEHSjLjLongJmp(MI, BB);
6632 }
6633
Evan Cheng20350c42006-11-27 23:37:22 +00006634 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00006635
6636 // To "insert" these instructions we actually have to insert their
6637 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00006638 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00006639 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00006640 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00006641
Dan Gohman3b460302008-07-07 23:14:23 +00006642 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00006643
Hal Finkel460e94d2012-06-22 23:10:08 +00006644 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006645 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6646 MI->getOpcode() == PPC::SELECT_I4 ||
6647 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00006648 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00006649 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6650 MI->getOpcode() == PPC::SELECT_CC_I8)
6651 Cond.push_back(MI->getOperand(4));
6652 else
6653 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00006654 Cond.push_back(MI->getOperand(1));
6655
Hal Finkel460e94d2012-06-22 23:10:08 +00006656 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00006657 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6658 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
6659 Cond, MI->getOperand(2).getReg(),
6660 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00006661 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6662 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6663 MI->getOpcode() == PPC::SELECT_CC_F4 ||
6664 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006665 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
6666 MI->getOpcode() == PPC::SELECT_I4 ||
6667 MI->getOpcode() == PPC::SELECT_I8 ||
6668 MI->getOpcode() == PPC::SELECT_F4 ||
6669 MI->getOpcode() == PPC::SELECT_F8 ||
6670 MI->getOpcode() == PPC::SELECT_VRRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00006671 // The incoming instruction knows the destination vreg to set, the
6672 // condition code register to branch on, the true/false values to
6673 // select between, and a branch opcode to use.
6674
6675 // thisMBB:
6676 // ...
6677 // TrueVal = ...
6678 // cmpTY ccX, r1, r2
6679 // bCC copy1MBB
6680 // fallthrough --> copy0MBB
6681 MachineBasicBlock *thisMBB = BB;
6682 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6683 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006684 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006685 F->insert(It, copy0MBB);
6686 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006687
6688 // Transfer the remainder of BB and its successor edges to sinkMBB.
6689 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006690 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006691 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6692
Evan Cheng32e376f2008-07-12 02:23:19 +00006693 // Next, add the true and fallthrough blocks as its successors.
6694 BB->addSuccessor(copy0MBB);
6695 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006696
Hal Finkel940ab932014-02-28 00:27:01 +00006697 if (MI->getOpcode() == PPC::SELECT_I4 ||
6698 MI->getOpcode() == PPC::SELECT_I8 ||
6699 MI->getOpcode() == PPC::SELECT_F4 ||
6700 MI->getOpcode() == PPC::SELECT_F8 ||
6701 MI->getOpcode() == PPC::SELECT_VRRC) {
6702 BuildMI(BB, dl, TII->get(PPC::BC))
6703 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6704 } else {
6705 unsigned SelectPred = MI->getOperand(4).getImm();
6706 BuildMI(BB, dl, TII->get(PPC::BCC))
6707 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6708 }
Dan Gohman34396292010-07-06 20:24:04 +00006709
Evan Cheng32e376f2008-07-12 02:23:19 +00006710 // copy0MBB:
6711 // %FalseValue = ...
6712 // # fallthrough to sinkMBB
6713 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006714
Evan Cheng32e376f2008-07-12 02:23:19 +00006715 // Update machine-CFG edges
6716 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006717
Evan Cheng32e376f2008-07-12 02:23:19 +00006718 // sinkMBB:
6719 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6720 // ...
6721 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00006722 BuildMI(*BB, BB->begin(), dl,
6723 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00006724 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
6725 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6726 }
Dale Johannesena32affb2008-08-28 17:53:09 +00006727 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
6728 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
6729 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
6730 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006731 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
6732 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
6733 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
6734 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006735
6736 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
6737 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
6738 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
6739 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006740 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
6741 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
6742 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
6743 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006744
6745 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
6746 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
6747 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
6748 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006749 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
6750 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
6751 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
6752 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006753
6754 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
6755 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
6756 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
6757 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006758 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
6759 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
6760 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
6761 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006762
6763 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006764 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006765 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006766 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006767 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006768 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006769 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006770 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006771
6772 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6773 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6774 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6775 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006776 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6777 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6778 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6779 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006780
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006781 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6782 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6783 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6784 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6785 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6786 BB = EmitAtomicBinary(MI, BB, false, 0);
6787 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6788 BB = EmitAtomicBinary(MI, BB, true, 0);
6789
Evan Cheng32e376f2008-07-12 02:23:19 +00006790 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6791 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6792 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6793
6794 unsigned dest = MI->getOperand(0).getReg();
6795 unsigned ptrA = MI->getOperand(1).getReg();
6796 unsigned ptrB = MI->getOperand(2).getReg();
6797 unsigned oldval = MI->getOperand(3).getReg();
6798 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006799 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006800
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006801 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6802 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6803 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006804 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006805 F->insert(It, loop1MBB);
6806 F->insert(It, loop2MBB);
6807 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006808 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006809 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006810 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006811 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006812
6813 // thisMBB:
6814 // ...
6815 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006816 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006817
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006818 // loop1MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006819 // l[wd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006820 // cmp[wd] dest, oldval
6821 // bne- midMBB
6822 // loop2MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006823 // st[wd]cx. newval, ptr
6824 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006825 // b exitBB
6826 // midMBB:
6827 // st[wd]cx. dest, ptr
6828 // exitBB:
6829 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006830 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00006831 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006832 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00006833 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006834 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006835 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6836 BB->addSuccessor(loop2MBB);
6837 BB->addSuccessor(midMBB);
6838
6839 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006840 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng32e376f2008-07-12 02:23:19 +00006841 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006842 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006843 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006844 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006845 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006846 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006847
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006848 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006849 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006850 .addReg(dest).addReg(ptrA).addReg(ptrB);
6851 BB->addSuccessor(exitMBB);
6852
Evan Cheng32e376f2008-07-12 02:23:19 +00006853 // exitMBB:
6854 // ...
6855 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00006856 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6857 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6858 // We must use 64-bit registers for addresses when targeting 64-bit,
6859 // since we're actually doing arithmetic on them. Other registers
6860 // can be 32-bit.
6861 bool is64bit = PPCSubTarget.isPPC64();
6862 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6863
6864 unsigned dest = MI->getOperand(0).getReg();
6865 unsigned ptrA = MI->getOperand(1).getReg();
6866 unsigned ptrB = MI->getOperand(2).getReg();
6867 unsigned oldval = MI->getOperand(3).getReg();
6868 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006869 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00006870
6871 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6872 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6873 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6874 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6875 F->insert(It, loop1MBB);
6876 F->insert(It, loop2MBB);
6877 F->insert(It, midMBB);
6878 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006879 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006880 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006881 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006882
6883 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006884 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006885 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6886 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00006887 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6888 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6889 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6890 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6891 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6892 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6893 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6894 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6895 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6896 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6897 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6898 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6899 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6900 unsigned Ptr1Reg;
6901 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00006902 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00006903 // thisMBB:
6904 // ...
6905 // fallthrough --> loopMBB
6906 BB->addSuccessor(loop1MBB);
6907
6908 // The 4-byte load must be aligned, while a char or short may be
6909 // anywhere in the word. Hence all this nasty bookkeeping code.
6910 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6911 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006912 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00006913 // rlwinm ptr, ptr1, 0, 0, 29
6914 // slw newval2, newval, shift
6915 // slw oldval2, oldval,shift
6916 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6917 // slw mask, mask2, shift
6918 // and newval3, newval2, mask
6919 // and oldval3, oldval2, mask
6920 // loop1MBB:
6921 // lwarx tmpDest, ptr
6922 // and tmp, tmpDest, mask
6923 // cmpw tmp, oldval3
6924 // bne- midMBB
6925 // loop2MBB:
6926 // andc tmp2, tmpDest, mask
6927 // or tmp4, tmp2, newval3
6928 // stwcx. tmp4, ptr
6929 // bne- loop1MBB
6930 // b exitBB
6931 // midMBB:
6932 // stwcx. tmpDest, ptr
6933 // exitBB:
6934 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006935 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00006936 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006937 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006938 .addReg(ptrA).addReg(ptrB);
6939 } else {
6940 Ptr1Reg = ptrB;
6941 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006942 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006943 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006944 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006945 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6946 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006947 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006948 .addReg(Ptr1Reg).addImm(0).addImm(61);
6949 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006950 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006951 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006952 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006953 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006954 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006955 .addReg(oldval).addReg(ShiftReg);
6956 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006957 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00006958 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006959 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6960 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6961 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00006962 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006963 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006964 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006965 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006966 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006967 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006968 .addReg(OldVal2Reg).addReg(MaskReg);
6969
6970 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006971 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006972 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006973 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6974 .addReg(TmpDestReg).addReg(MaskReg);
6975 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00006976 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006977 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006978 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6979 BB->addSuccessor(loop2MBB);
6980 BB->addSuccessor(midMBB);
6981
6982 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006983 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6984 .addReg(TmpDestReg).addReg(MaskReg);
6985 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6986 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6987 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006988 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006989 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006990 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006991 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006992 BB->addSuccessor(loop1MBB);
6993 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006994
Dale Johannesen340d2642008-08-30 00:08:53 +00006995 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006996 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006997 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00006998 BB->addSuccessor(exitMBB);
6999
7000 // exitMBB:
7001 // ...
7002 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00007003 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
7004 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00007005 } else if (MI->getOpcode() == PPC::FADDrtz) {
7006 // This pseudo performs an FADD with rounding mode temporarily forced
7007 // to round-to-zero. We emit this via custom inserter since the FPSCR
7008 // is not modeled at the SelectionDAG level.
7009 unsigned Dest = MI->getOperand(0).getReg();
7010 unsigned Src1 = MI->getOperand(1).getReg();
7011 unsigned Src2 = MI->getOperand(2).getReg();
7012 DebugLoc dl = MI->getDebugLoc();
7013
7014 MachineRegisterInfo &RegInfo = F->getRegInfo();
7015 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
7016
7017 // Save FPSCR value.
7018 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
7019
7020 // Set rounding mode to round-to-zero.
7021 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
7022 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
7023
7024 // Perform addition.
7025 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
7026
7027 // Restore FPSCR value.
7028 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00007029 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
7030 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
7031 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
7032 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
7033 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
7034 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
7035 PPC::ANDIo8 : PPC::ANDIo;
7036 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
7037 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
7038
7039 MachineRegisterInfo &RegInfo = F->getRegInfo();
7040 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
7041 &PPC::GPRCRegClass :
7042 &PPC::G8RCRegClass);
7043
7044 DebugLoc dl = MI->getDebugLoc();
7045 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
7046 .addReg(MI->getOperand(1).getReg()).addImm(1);
7047 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
7048 MI->getOperand(0).getReg())
7049 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Dale Johannesen340d2642008-08-30 00:08:53 +00007050 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007051 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00007052 }
Chris Lattner9b577f12005-08-26 21:23:58 +00007053
Dan Gohman34396292010-07-06 20:24:04 +00007054 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00007055 return BB;
7056}
7057
Chris Lattner4211ca92006-04-14 06:01:58 +00007058//===----------------------------------------------------------------------===//
7059// Target Optimization Hooks
7060//===----------------------------------------------------------------------===//
7061
Hal Finkelb0c810f2013-04-03 17:44:56 +00007062SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
7063 DAGCombinerInfo &DCI) const {
Hal Finkel2e103312013-04-03 04:01:11 +00007064 if (DCI.isAfterLegalizeVectorOps())
7065 return SDValue();
7066
Hal Finkelb0c810f2013-04-03 17:44:56 +00007067 EVT VT = Op.getValueType();
7068
7069 if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) ||
7070 (VT == MVT::f64 && PPCSubTarget.hasFRE()) ||
Hal Finkel27774d92014-03-13 07:58:58 +00007071 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec()) ||
7072 (VT == MVT::v2f64 && PPCSubTarget.hasVSX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00007073
7074 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
7075 // For the reciprocal, we need to find the zero of the function:
7076 // F(X) = A X - 1 [which has a zero at X = 1/A]
7077 // =>
7078 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
7079 // does not require additional intermediate precision]
7080
7081 // Convergence is quadratic, so we essentially double the number of digits
7082 // correct after every iteration. The minimum architected relative
7083 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
7084 // 23 digits and double has 52 digits.
7085 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00007086 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00007087 ++Iterations;
7088
7089 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007090 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007091
7092 SDValue FPOne =
Hal Finkelb0c810f2013-04-03 17:44:56 +00007093 DAG.getConstantFP(1.0, VT.getScalarType());
7094 if (VT.isVector()) {
7095 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00007096 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00007097 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel2e103312013-04-03 04:01:11 +00007098 FPOne, FPOne, FPOne, FPOne);
7099 }
7100
Hal Finkelb0c810f2013-04-03 17:44:56 +00007101 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007102 DCI.AddToWorklist(Est.getNode());
7103
7104 // Newton iterations: Est = Est + Est (1 - Arg * Est)
7105 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007106 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00007107 DCI.AddToWorklist(NewEst.getNode());
7108
Hal Finkelb0c810f2013-04-03 17:44:56 +00007109 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007110 DCI.AddToWorklist(NewEst.getNode());
7111
Hal Finkelb0c810f2013-04-03 17:44:56 +00007112 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007113 DCI.AddToWorklist(NewEst.getNode());
7114
Hal Finkelb0c810f2013-04-03 17:44:56 +00007115 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007116 DCI.AddToWorklist(Est.getNode());
7117 }
7118
7119 return Est;
7120 }
7121
7122 return SDValue();
7123}
7124
Hal Finkelb0c810f2013-04-03 17:44:56 +00007125SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel2e103312013-04-03 04:01:11 +00007126 DAGCombinerInfo &DCI) const {
7127 if (DCI.isAfterLegalizeVectorOps())
7128 return SDValue();
7129
Hal Finkelb0c810f2013-04-03 17:44:56 +00007130 EVT VT = Op.getValueType();
7131
7132 if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) ||
7133 (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) ||
Hal Finkel27774d92014-03-13 07:58:58 +00007134 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec()) ||
7135 (VT == MVT::v2f64 && PPCSubTarget.hasVSX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00007136
7137 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
7138 // For the reciprocal sqrt, we need to find the zero of the function:
7139 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
7140 // =>
7141 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
7142 // As a result, we precompute A/2 prior to the iteration loop.
7143
7144 // Convergence is quadratic, so we essentially double the number of digits
7145 // correct after every iteration. The minimum architected relative
7146 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
7147 // 23 digits and double has 52 digits.
7148 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00007149 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00007150 ++Iterations;
7151
7152 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007153 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007154
Hal Finkelb0c810f2013-04-03 17:44:56 +00007155 SDValue FPThreeHalves =
7156 DAG.getConstantFP(1.5, VT.getScalarType());
7157 if (VT.isVector()) {
7158 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00007159 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00007160 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
7161 FPThreeHalves, FPThreeHalves,
7162 FPThreeHalves, FPThreeHalves);
Hal Finkel2e103312013-04-03 04:01:11 +00007163 }
7164
Hal Finkelb0c810f2013-04-03 17:44:56 +00007165 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007166 DCI.AddToWorklist(Est.getNode());
7167
7168 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
7169 // this entire sequence requires only one FP constant.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007170 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007171 DCI.AddToWorklist(HalfArg.getNode());
7172
Hal Finkelb0c810f2013-04-03 17:44:56 +00007173 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007174 DCI.AddToWorklist(HalfArg.getNode());
7175
7176 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
7177 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007178 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00007179 DCI.AddToWorklist(NewEst.getNode());
7180
Hal Finkelb0c810f2013-04-03 17:44:56 +00007181 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007182 DCI.AddToWorklist(NewEst.getNode());
7183
Hal Finkelb0c810f2013-04-03 17:44:56 +00007184 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007185 DCI.AddToWorklist(NewEst.getNode());
7186
Hal Finkelb0c810f2013-04-03 17:44:56 +00007187 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007188 DCI.AddToWorklist(Est.getNode());
7189 }
7190
7191 return Est;
7192 }
7193
7194 return SDValue();
7195}
7196
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007197// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
7198// not enforce equality of the chain operands.
7199static bool isConsecutiveLS(LSBaseSDNode *LS, LSBaseSDNode *Base,
7200 unsigned Bytes, int Dist,
7201 SelectionDAG &DAG) {
7202 EVT VT = LS->getMemoryVT();
7203 if (VT.getSizeInBits() / 8 != Bytes)
7204 return false;
7205
7206 SDValue Loc = LS->getBasePtr();
7207 SDValue BaseLoc = Base->getBasePtr();
7208 if (Loc.getOpcode() == ISD::FrameIndex) {
7209 if (BaseLoc.getOpcode() != ISD::FrameIndex)
7210 return false;
7211 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7212 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
7213 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
7214 int FS = MFI->getObjectSize(FI);
7215 int BFS = MFI->getObjectSize(BFI);
7216 if (FS != BFS || FS != (int)Bytes) return false;
7217 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
7218 }
7219
7220 // Handle X+C
7221 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
7222 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
7223 return true;
7224
7225 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Craig Topper062a2ba2014-04-25 05:30:21 +00007226 const GlobalValue *GV1 = nullptr;
7227 const GlobalValue *GV2 = nullptr;
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007228 int64_t Offset1 = 0;
7229 int64_t Offset2 = 0;
7230 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
7231 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
7232 if (isGA1 && isGA2 && GV1 == GV2)
7233 return Offset1 == (Offset2 + Dist*Bytes);
7234 return false;
7235}
7236
Hal Finkel7d8a6912013-05-26 18:08:30 +00007237// Return true is there is a nearyby consecutive load to the one provided
7238// (regardless of alignment). We search up and down the chain, looking though
7239// token factors and other loads (but nothing else). As a result, a true
7240// results indicates that it is safe to create a new consecutive load adjacent
7241// to the load provided.
7242static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
7243 SDValue Chain = LD->getChain();
7244 EVT VT = LD->getMemoryVT();
7245
7246 SmallSet<SDNode *, 16> LoadRoots;
7247 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
7248 SmallSet<SDNode *, 16> Visited;
7249
7250 // First, search up the chain, branching to follow all token-factor operands.
7251 // If we find a consecutive load, then we're done, otherwise, record all
7252 // nodes just above the top-level loads and token factors.
7253 while (!Queue.empty()) {
7254 SDNode *ChainNext = Queue.pop_back_val();
7255 if (!Visited.insert(ChainNext))
7256 continue;
7257
7258 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007259 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007260 return true;
7261
7262 if (!Visited.count(ChainLD->getChain().getNode()))
7263 Queue.push_back(ChainLD->getChain().getNode());
7264 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
7265 for (SDNode::op_iterator O = ChainNext->op_begin(),
7266 OE = ChainNext->op_end(); O != OE; ++O)
7267 if (!Visited.count(O->getNode()))
7268 Queue.push_back(O->getNode());
7269 } else
7270 LoadRoots.insert(ChainNext);
7271 }
7272
7273 // Second, search down the chain, starting from the top-level nodes recorded
7274 // in the first phase. These top-level nodes are the nodes just above all
7275 // loads and token factors. Starting with their uses, recursively look though
7276 // all loads (just the chain uses) and token factors to find a consecutive
7277 // load.
7278 Visited.clear();
7279 Queue.clear();
7280
7281 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
7282 IE = LoadRoots.end(); I != IE; ++I) {
7283 Queue.push_back(*I);
7284
7285 while (!Queue.empty()) {
7286 SDNode *LoadRoot = Queue.pop_back_val();
7287 if (!Visited.insert(LoadRoot))
7288 continue;
7289
7290 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007291 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007292 return true;
7293
7294 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
7295 UE = LoadRoot->use_end(); UI != UE; ++UI)
7296 if (((isa<LoadSDNode>(*UI) &&
7297 cast<LoadSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
7298 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
7299 Queue.push_back(*UI);
7300 }
7301 }
7302
7303 return false;
7304}
7305
Hal Finkel940ab932014-02-28 00:27:01 +00007306SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
7307 DAGCombinerInfo &DCI) const {
7308 SelectionDAG &DAG = DCI.DAG;
7309 SDLoc dl(N);
7310
7311 assert(PPCSubTarget.useCRBits() &&
7312 "Expecting to be tracking CR bits");
7313 // If we're tracking CR bits, we need to be careful that we don't have:
7314 // trunc(binary-ops(zext(x), zext(y)))
7315 // or
7316 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
7317 // such that we're unnecessarily moving things into GPRs when it would be
7318 // better to keep them in CR bits.
7319
7320 // Note that trunc here can be an actual i1 trunc, or can be the effective
7321 // truncation that comes from a setcc or select_cc.
7322 if (N->getOpcode() == ISD::TRUNCATE &&
7323 N->getValueType(0) != MVT::i1)
7324 return SDValue();
7325
7326 if (N->getOperand(0).getValueType() != MVT::i32 &&
7327 N->getOperand(0).getValueType() != MVT::i64)
7328 return SDValue();
7329
7330 if (N->getOpcode() == ISD::SETCC ||
7331 N->getOpcode() == ISD::SELECT_CC) {
7332 // If we're looking at a comparison, then we need to make sure that the
7333 // high bits (all except for the first) don't matter the result.
7334 ISD::CondCode CC =
7335 cast<CondCodeSDNode>(N->getOperand(
7336 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
7337 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
7338
7339 if (ISD::isSignedIntSetCC(CC)) {
7340 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
7341 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
7342 return SDValue();
7343 } else if (ISD::isUnsignedIntSetCC(CC)) {
7344 if (!DAG.MaskedValueIsZero(N->getOperand(0),
7345 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
7346 !DAG.MaskedValueIsZero(N->getOperand(1),
7347 APInt::getHighBitsSet(OpBits, OpBits-1)))
7348 return SDValue();
7349 } else {
7350 // This is neither a signed nor an unsigned comparison, just make sure
7351 // that the high bits are equal.
7352 APInt Op1Zero, Op1One;
7353 APInt Op2Zero, Op2One;
Jay Foada0653a32014-05-14 21:14:37 +00007354 DAG.computeKnownBits(N->getOperand(0), Op1Zero, Op1One);
7355 DAG.computeKnownBits(N->getOperand(1), Op2Zero, Op2One);
Hal Finkel940ab932014-02-28 00:27:01 +00007356
7357 // We don't really care about what is known about the first bit (if
7358 // anything), so clear it in all masks prior to comparing them.
7359 Op1Zero.clearBit(0); Op1One.clearBit(0);
7360 Op2Zero.clearBit(0); Op2One.clearBit(0);
7361
7362 if (Op1Zero != Op2Zero || Op1One != Op2One)
7363 return SDValue();
7364 }
7365 }
7366
7367 // We now know that the higher-order bits are irrelevant, we just need to
7368 // make sure that all of the intermediate operations are bit operations, and
7369 // all inputs are extensions.
7370 if (N->getOperand(0).getOpcode() != ISD::AND &&
7371 N->getOperand(0).getOpcode() != ISD::OR &&
7372 N->getOperand(0).getOpcode() != ISD::XOR &&
7373 N->getOperand(0).getOpcode() != ISD::SELECT &&
7374 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
7375 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
7376 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
7377 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
7378 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
7379 return SDValue();
7380
7381 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
7382 N->getOperand(1).getOpcode() != ISD::AND &&
7383 N->getOperand(1).getOpcode() != ISD::OR &&
7384 N->getOperand(1).getOpcode() != ISD::XOR &&
7385 N->getOperand(1).getOpcode() != ISD::SELECT &&
7386 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
7387 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
7388 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
7389 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
7390 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
7391 return SDValue();
7392
7393 SmallVector<SDValue, 4> Inputs;
7394 SmallVector<SDValue, 8> BinOps, PromOps;
7395 SmallPtrSet<SDNode *, 16> Visited;
7396
7397 for (unsigned i = 0; i < 2; ++i) {
7398 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7399 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7400 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7401 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7402 isa<ConstantSDNode>(N->getOperand(i)))
7403 Inputs.push_back(N->getOperand(i));
7404 else
7405 BinOps.push_back(N->getOperand(i));
7406
7407 if (N->getOpcode() == ISD::TRUNCATE)
7408 break;
7409 }
7410
7411 // Visit all inputs, collect all binary operations (and, or, xor and
7412 // select) that are all fed by extensions.
7413 while (!BinOps.empty()) {
7414 SDValue BinOp = BinOps.back();
7415 BinOps.pop_back();
7416
7417 if (!Visited.insert(BinOp.getNode()))
7418 continue;
7419
7420 PromOps.push_back(BinOp);
7421
7422 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7423 // The condition of the select is not promoted.
7424 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7425 continue;
7426 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7427 continue;
7428
7429 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7430 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7431 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7432 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7433 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7434 Inputs.push_back(BinOp.getOperand(i));
7435 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7436 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7437 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7438 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7439 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
7440 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7441 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7442 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7443 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
7444 BinOps.push_back(BinOp.getOperand(i));
7445 } else {
7446 // We have an input that is not an extension or another binary
7447 // operation; we'll abort this transformation.
7448 return SDValue();
7449 }
7450 }
7451 }
7452
7453 // Make sure that this is a self-contained cluster of operations (which
7454 // is not quite the same thing as saying that everything has only one
7455 // use).
7456 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7457 if (isa<ConstantSDNode>(Inputs[i]))
7458 continue;
7459
7460 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7461 UE = Inputs[i].getNode()->use_end();
7462 UI != UE; ++UI) {
7463 SDNode *User = *UI;
7464 if (User != N && !Visited.count(User))
7465 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007466
7467 // Make sure that we're not going to promote the non-output-value
7468 // operand(s) or SELECT or SELECT_CC.
7469 // FIXME: Although we could sometimes handle this, and it does occur in
7470 // practice that one of the condition inputs to the select is also one of
7471 // the outputs, we currently can't deal with this.
7472 if (User->getOpcode() == ISD::SELECT) {
7473 if (User->getOperand(0) == Inputs[i])
7474 return SDValue();
7475 } else if (User->getOpcode() == ISD::SELECT_CC) {
7476 if (User->getOperand(0) == Inputs[i] ||
7477 User->getOperand(1) == Inputs[i])
7478 return SDValue();
7479 }
Hal Finkel940ab932014-02-28 00:27:01 +00007480 }
7481 }
7482
7483 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7484 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7485 UE = PromOps[i].getNode()->use_end();
7486 UI != UE; ++UI) {
7487 SDNode *User = *UI;
7488 if (User != N && !Visited.count(User))
7489 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007490
7491 // Make sure that we're not going to promote the non-output-value
7492 // operand(s) or SELECT or SELECT_CC.
7493 // FIXME: Although we could sometimes handle this, and it does occur in
7494 // practice that one of the condition inputs to the select is also one of
7495 // the outputs, we currently can't deal with this.
7496 if (User->getOpcode() == ISD::SELECT) {
7497 if (User->getOperand(0) == PromOps[i])
7498 return SDValue();
7499 } else if (User->getOpcode() == ISD::SELECT_CC) {
7500 if (User->getOperand(0) == PromOps[i] ||
7501 User->getOperand(1) == PromOps[i])
7502 return SDValue();
7503 }
Hal Finkel940ab932014-02-28 00:27:01 +00007504 }
7505 }
7506
7507 // Replace all inputs with the extension operand.
7508 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7509 // Constants may have users outside the cluster of to-be-promoted nodes,
7510 // and so we need to replace those as we do the promotions.
7511 if (isa<ConstantSDNode>(Inputs[i]))
7512 continue;
7513 else
7514 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
7515 }
7516
7517 // Replace all operations (these are all the same, but have a different
7518 // (i1) return type). DAG.getNode will validate that the types of
7519 // a binary operator match, so go through the list in reverse so that
7520 // we've likely promoted both operands first. Any intermediate truncations or
7521 // extensions disappear.
7522 while (!PromOps.empty()) {
7523 SDValue PromOp = PromOps.back();
7524 PromOps.pop_back();
7525
7526 if (PromOp.getOpcode() == ISD::TRUNCATE ||
7527 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
7528 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
7529 PromOp.getOpcode() == ISD::ANY_EXTEND) {
7530 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
7531 PromOp.getOperand(0).getValueType() != MVT::i1) {
7532 // The operand is not yet ready (see comment below).
7533 PromOps.insert(PromOps.begin(), PromOp);
7534 continue;
7535 }
7536
7537 SDValue RepValue = PromOp.getOperand(0);
7538 if (isa<ConstantSDNode>(RepValue))
7539 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
7540
7541 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
7542 continue;
7543 }
7544
7545 unsigned C;
7546 switch (PromOp.getOpcode()) {
7547 default: C = 0; break;
7548 case ISD::SELECT: C = 1; break;
7549 case ISD::SELECT_CC: C = 2; break;
7550 }
7551
7552 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7553 PromOp.getOperand(C).getValueType() != MVT::i1) ||
7554 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7555 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
7556 // The to-be-promoted operands of this node have not yet been
7557 // promoted (this should be rare because we're going through the
7558 // list backward, but if one of the operands has several users in
7559 // this cluster of to-be-promoted nodes, it is possible).
7560 PromOps.insert(PromOps.begin(), PromOp);
7561 continue;
7562 }
7563
7564 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7565 PromOp.getNode()->op_end());
7566
7567 // If there are any constant inputs, make sure they're replaced now.
7568 for (unsigned i = 0; i < 2; ++i)
7569 if (isa<ConstantSDNode>(Ops[C+i]))
7570 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
7571
7572 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00007573 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00007574 }
7575
7576 // Now we're left with the initial truncation itself.
7577 if (N->getOpcode() == ISD::TRUNCATE)
7578 return N->getOperand(0);
7579
7580 // Otherwise, this is a comparison. The operands to be compared have just
7581 // changed type (to i1), but everything else is the same.
7582 return SDValue(N, 0);
7583}
7584
7585SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
7586 DAGCombinerInfo &DCI) const {
7587 SelectionDAG &DAG = DCI.DAG;
7588 SDLoc dl(N);
7589
Hal Finkel940ab932014-02-28 00:27:01 +00007590 // If we're tracking CR bits, we need to be careful that we don't have:
7591 // zext(binary-ops(trunc(x), trunc(y)))
7592 // or
7593 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
7594 // such that we're unnecessarily moving things into CR bits that can more
7595 // efficiently stay in GPRs. Note that if we're not certain that the high
7596 // bits are set as required by the final extension, we still may need to do
7597 // some masking to get the proper behavior.
7598
Hal Finkel46043ed2014-03-01 21:36:57 +00007599 // This same functionality is important on PPC64 when dealing with
7600 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
7601 // the return values of functions. Because it is so similar, it is handled
7602 // here as well.
7603
Hal Finkel940ab932014-02-28 00:27:01 +00007604 if (N->getValueType(0) != MVT::i32 &&
7605 N->getValueType(0) != MVT::i64)
7606 return SDValue();
7607
Hal Finkel46043ed2014-03-01 21:36:57 +00007608 if (!((N->getOperand(0).getValueType() == MVT::i1 &&
7609 PPCSubTarget.useCRBits()) ||
7610 (N->getOperand(0).getValueType() == MVT::i32 &&
7611 PPCSubTarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +00007612 return SDValue();
7613
7614 if (N->getOperand(0).getOpcode() != ISD::AND &&
7615 N->getOperand(0).getOpcode() != ISD::OR &&
7616 N->getOperand(0).getOpcode() != ISD::XOR &&
7617 N->getOperand(0).getOpcode() != ISD::SELECT &&
7618 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
7619 return SDValue();
7620
7621 SmallVector<SDValue, 4> Inputs;
7622 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
7623 SmallPtrSet<SDNode *, 16> Visited;
7624
7625 // Visit all inputs, collect all binary operations (and, or, xor and
7626 // select) that are all fed by truncations.
7627 while (!BinOps.empty()) {
7628 SDValue BinOp = BinOps.back();
7629 BinOps.pop_back();
7630
7631 if (!Visited.insert(BinOp.getNode()))
7632 continue;
7633
7634 PromOps.push_back(BinOp);
7635
7636 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7637 // The condition of the select is not promoted.
7638 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7639 continue;
7640 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7641 continue;
7642
7643 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7644 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7645 Inputs.push_back(BinOp.getOperand(i));
7646 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7647 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7648 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7649 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7650 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
7651 BinOps.push_back(BinOp.getOperand(i));
7652 } else {
7653 // We have an input that is not a truncation or another binary
7654 // operation; we'll abort this transformation.
7655 return SDValue();
7656 }
7657 }
7658 }
7659
7660 // Make sure that this is a self-contained cluster of operations (which
7661 // is not quite the same thing as saying that everything has only one
7662 // use).
7663 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7664 if (isa<ConstantSDNode>(Inputs[i]))
7665 continue;
7666
7667 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7668 UE = Inputs[i].getNode()->use_end();
7669 UI != UE; ++UI) {
7670 SDNode *User = *UI;
7671 if (User != N && !Visited.count(User))
7672 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007673
7674 // Make sure that we're not going to promote the non-output-value
7675 // operand(s) or SELECT or SELECT_CC.
7676 // FIXME: Although we could sometimes handle this, and it does occur in
7677 // practice that one of the condition inputs to the select is also one of
7678 // the outputs, we currently can't deal with this.
7679 if (User->getOpcode() == ISD::SELECT) {
7680 if (User->getOperand(0) == Inputs[i])
7681 return SDValue();
7682 } else if (User->getOpcode() == ISD::SELECT_CC) {
7683 if (User->getOperand(0) == Inputs[i] ||
7684 User->getOperand(1) == Inputs[i])
7685 return SDValue();
7686 }
Hal Finkel940ab932014-02-28 00:27:01 +00007687 }
7688 }
7689
7690 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7691 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7692 UE = PromOps[i].getNode()->use_end();
7693 UI != UE; ++UI) {
7694 SDNode *User = *UI;
7695 if (User != N && !Visited.count(User))
7696 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007697
7698 // Make sure that we're not going to promote the non-output-value
7699 // operand(s) or SELECT or SELECT_CC.
7700 // FIXME: Although we could sometimes handle this, and it does occur in
7701 // practice that one of the condition inputs to the select is also one of
7702 // the outputs, we currently can't deal with this.
7703 if (User->getOpcode() == ISD::SELECT) {
7704 if (User->getOperand(0) == PromOps[i])
7705 return SDValue();
7706 } else if (User->getOpcode() == ISD::SELECT_CC) {
7707 if (User->getOperand(0) == PromOps[i] ||
7708 User->getOperand(1) == PromOps[i])
7709 return SDValue();
7710 }
Hal Finkel940ab932014-02-28 00:27:01 +00007711 }
7712 }
7713
Hal Finkel46043ed2014-03-01 21:36:57 +00007714 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +00007715 bool ReallyNeedsExt = false;
7716 if (N->getOpcode() != ISD::ANY_EXTEND) {
7717 // If all of the inputs are not already sign/zero extended, then
7718 // we'll still need to do that at the end.
7719 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7720 if (isa<ConstantSDNode>(Inputs[i]))
7721 continue;
7722
7723 unsigned OpBits =
7724 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +00007725 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
7726
Hal Finkel940ab932014-02-28 00:27:01 +00007727 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
7728 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00007729 APInt::getHighBitsSet(OpBits,
7730 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +00007731 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +00007732 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
7733 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +00007734 ReallyNeedsExt = true;
7735 break;
7736 }
7737 }
7738 }
7739
7740 // Replace all inputs, either with the truncation operand, or a
7741 // truncation or extension to the final output type.
7742 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7743 // Constant inputs need to be replaced with the to-be-promoted nodes that
7744 // use them because they might have users outside of the cluster of
7745 // promoted nodes.
7746 if (isa<ConstantSDNode>(Inputs[i]))
7747 continue;
7748
7749 SDValue InSrc = Inputs[i].getOperand(0);
7750 if (Inputs[i].getValueType() == N->getValueType(0))
7751 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
7752 else if (N->getOpcode() == ISD::SIGN_EXTEND)
7753 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7754 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
7755 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7756 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7757 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
7758 else
7759 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7760 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
7761 }
7762
7763 // Replace all operations (these are all the same, but have a different
7764 // (promoted) return type). DAG.getNode will validate that the types of
7765 // a binary operator match, so go through the list in reverse so that
7766 // we've likely promoted both operands first.
7767 while (!PromOps.empty()) {
7768 SDValue PromOp = PromOps.back();
7769 PromOps.pop_back();
7770
7771 unsigned C;
7772 switch (PromOp.getOpcode()) {
7773 default: C = 0; break;
7774 case ISD::SELECT: C = 1; break;
7775 case ISD::SELECT_CC: C = 2; break;
7776 }
7777
7778 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7779 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
7780 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7781 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
7782 // The to-be-promoted operands of this node have not yet been
7783 // promoted (this should be rare because we're going through the
7784 // list backward, but if one of the operands has several users in
7785 // this cluster of to-be-promoted nodes, it is possible).
7786 PromOps.insert(PromOps.begin(), PromOp);
7787 continue;
7788 }
7789
7790 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7791 PromOp.getNode()->op_end());
7792
7793 // If this node has constant inputs, then they'll need to be promoted here.
7794 for (unsigned i = 0; i < 2; ++i) {
7795 if (!isa<ConstantSDNode>(Ops[C+i]))
7796 continue;
7797 if (Ops[C+i].getValueType() == N->getValueType(0))
7798 continue;
7799
7800 if (N->getOpcode() == ISD::SIGN_EXTEND)
7801 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7802 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7803 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7804 else
7805 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7806 }
7807
7808 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00007809 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00007810 }
7811
7812 // Now we're left with the initial extension itself.
7813 if (!ReallyNeedsExt)
7814 return N->getOperand(0);
7815
Hal Finkel46043ed2014-03-01 21:36:57 +00007816 // To zero extend, just mask off everything except for the first bit (in the
7817 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +00007818 if (N->getOpcode() == ISD::ZERO_EXTEND)
7819 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00007820 DAG.getConstant(APInt::getLowBitsSet(
7821 N->getValueSizeInBits(0), PromBits),
7822 N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +00007823
7824 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
7825 "Invalid extension type");
7826 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0));
7827 SDValue ShiftCst =
Hal Finkel46043ed2014-03-01 21:36:57 +00007828 DAG.getConstant(N->getValueSizeInBits(0)-PromBits, ShiftAmountTy);
Hal Finkel940ab932014-02-28 00:27:01 +00007829 return DAG.getNode(ISD::SRA, dl, N->getValueType(0),
7830 DAG.getNode(ISD::SHL, dl, N->getValueType(0),
7831 N->getOperand(0), ShiftCst), ShiftCst);
7832}
7833
Duncan Sandsdc2dac12008-11-24 14:53:14 +00007834SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
7835 DAGCombinerInfo &DCI) const {
Dan Gohman57c732b2010-04-21 01:34:56 +00007836 const TargetMachine &TM = getTargetMachine();
Chris Lattnerf4184352006-03-01 04:57:39 +00007837 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007838 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00007839 switch (N->getOpcode()) {
7840 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00007841 case PPCISD::SHL:
7842 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007843 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007844 return N->getOperand(0);
7845 }
7846 break;
7847 case PPCISD::SRL:
7848 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007849 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007850 return N->getOperand(0);
7851 }
7852 break;
7853 case PPCISD::SRA:
7854 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007855 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007856 C->isAllOnesValue()) // -1 >>s V -> -1.
7857 return N->getOperand(0);
7858 }
7859 break;
Hal Finkel940ab932014-02-28 00:27:01 +00007860 case ISD::SIGN_EXTEND:
7861 case ISD::ZERO_EXTEND:
7862 case ISD::ANY_EXTEND:
7863 return DAGCombineExtBoolTrunc(N, DCI);
7864 case ISD::TRUNCATE:
7865 case ISD::SETCC:
7866 case ISD::SELECT_CC:
7867 return DAGCombineTruncBoolExt(N, DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007868 case ISD::FDIV: {
7869 assert(TM.Options.UnsafeFPMath &&
7870 "Reciprocal estimates require UnsafeFPMath");
Scott Michelcf0da6c2009-02-17 22:15:04 +00007871
Hal Finkel2e103312013-04-03 04:01:11 +00007872 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007873 SDValue RV =
7874 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007875 if (RV.getNode()) {
Hal Finkel2e103312013-04-03 04:01:11 +00007876 DCI.AddToWorklist(RV.getNode());
7877 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7878 N->getOperand(0), RV);
7879 }
Hal Finkelf96c18e2013-04-04 22:44:12 +00007880 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
7881 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7882 SDValue RV =
7883 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7884 DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007885 if (RV.getNode()) {
Hal Finkelf96c18e2013-04-04 22:44:12 +00007886 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007887 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007888 N->getValueType(0), RV);
7889 DCI.AddToWorklist(RV.getNode());
7890 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7891 N->getOperand(0), RV);
7892 }
7893 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
7894 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7895 SDValue RV =
7896 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7897 DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007898 if (RV.getNode()) {
Hal Finkelf96c18e2013-04-04 22:44:12 +00007899 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007900 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007901 N->getValueType(0), RV,
7902 N->getOperand(1).getOperand(1));
7903 DCI.AddToWorklist(RV.getNode());
7904 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7905 N->getOperand(0), RV);
7906 }
Hal Finkel2e103312013-04-03 04:01:11 +00007907 }
7908
Hal Finkelb0c810f2013-04-03 17:44:56 +00007909 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007910 if (RV.getNode()) {
Hal Finkel2e103312013-04-03 04:01:11 +00007911 DCI.AddToWorklist(RV.getNode());
7912 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7913 N->getOperand(0), RV);
7914 }
7915
7916 }
7917 break;
7918 case ISD::FSQRT: {
7919 assert(TM.Options.UnsafeFPMath &&
7920 "Reciprocal estimates require UnsafeFPMath");
7921
7922 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
7923 // reciprocal sqrt.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007924 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007925 if (RV.getNode()) {
Hal Finkel2e103312013-04-03 04:01:11 +00007926 DCI.AddToWorklist(RV.getNode());
Hal Finkelb0c810f2013-04-03 17:44:56 +00007927 RV = DAGCombineFastRecip(RV, DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007928 if (RV.getNode()) {
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007929 // Unfortunately, RV is now NaN if the input was exactly 0. Select out
7930 // this case and force the answer to 0.
7931
7932 EVT VT = RV.getValueType();
7933
7934 SDValue Zero = DAG.getConstantFP(0.0, VT.getScalarType());
7935 if (VT.isVector()) {
7936 assert(VT.getVectorNumElements() == 4 && "Unknown vector type");
7937 Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Zero, Zero, Zero, Zero);
7938 }
7939
7940 SDValue ZeroCmp =
7941 DAG.getSetCC(dl, getSetCCResultType(*DAG.getContext(), VT),
7942 N->getOperand(0), Zero, ISD::SETEQ);
7943 DCI.AddToWorklist(ZeroCmp.getNode());
7944 DCI.AddToWorklist(RV.getNode());
7945
7946 RV = DAG.getNode(VT.isVector() ? ISD::VSELECT : ISD::SELECT, dl, VT,
7947 ZeroCmp, Zero, RV);
Hal Finkel2e103312013-04-03 04:01:11 +00007948 return RV;
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007949 }
Hal Finkel2e103312013-04-03 04:01:11 +00007950 }
7951
7952 }
7953 break;
Chris Lattnerf4184352006-03-01 04:57:39 +00007954 case ISD::SINT_TO_FP:
Chris Lattnera35f3062006-06-16 17:34:12 +00007955 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007956 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
7957 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
7958 // We allow the src/dst to be either f32/f64, but the intermediate
7959 // type must be i64.
Owen Anderson9f944592009-08-11 20:47:22 +00007960 if (N->getOperand(0).getValueType() == MVT::i64 &&
7961 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007962 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007963 if (Val.getValueType() == MVT::f32) {
7964 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007965 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007966 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007967
Owen Anderson9f944592009-08-11 20:47:22 +00007968 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007969 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007970 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007971 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007972 if (N->getValueType(0) == MVT::f32) {
7973 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner72733e52008-01-17 07:00:52 +00007974 DAG.getIntPtrConstant(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007975 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007976 }
7977 return Val;
Owen Anderson9f944592009-08-11 20:47:22 +00007978 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007979 // If the intermediate type is i32, we can avoid the load/store here
7980 // too.
Chris Lattnerf4184352006-03-01 04:57:39 +00007981 }
Chris Lattnerf4184352006-03-01 04:57:39 +00007982 }
7983 }
7984 break;
Chris Lattner27f53452006-03-01 05:50:56 +00007985 case ISD::STORE:
7986 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
7987 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnerf5b46f72008-01-18 16:54:56 +00007988 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00007989 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00007990 N->getOperand(1).getValueType() == MVT::i32 &&
7991 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007992 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007993 if (Val.getValueType() == MVT::f32) {
7994 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007995 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007996 }
Owen Anderson9f944592009-08-11 20:47:22 +00007997 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007998 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007999
Hal Finkel60c75102013-04-01 15:37:53 +00008000 SDValue Ops[] = {
8001 N->getOperand(0), Val, N->getOperand(2),
8002 DAG.getValueType(N->getOperand(1).getValueType())
8003 };
8004
8005 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00008006 DAG.getVTList(MVT::Other), Ops,
Hal Finkel60c75102013-04-01 15:37:53 +00008007 cast<StoreSDNode>(N)->getMemoryVT(),
8008 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00008009 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008010 return Val;
8011 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008012
Chris Lattnera7976d32006-07-10 20:56:58 +00008013 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00008014 if (cast<StoreSDNode>(N)->isUnindexed() &&
8015 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00008016 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00008017 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00008018 N->getOperand(1).getValueType() == MVT::i16 ||
8019 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008020 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008021 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008022 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008023 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00008024 if (BSwapOp.getValueType() == MVT::i16)
8025 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00008026
Dan Gohman48b185d2009-09-25 20:36:54 +00008027 SDValue Ops[] = {
8028 N->getOperand(0), BSwapOp, N->getOperand(2),
8029 DAG.getValueType(N->getOperand(1).getValueType())
8030 };
8031 return
8032 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00008033 Ops, cast<StoreSDNode>(N)->getMemoryVT(),
Dan Gohman48b185d2009-09-25 20:36:54 +00008034 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008035 }
8036 break;
Hal Finkelcf2e9082013-05-24 23:00:14 +00008037 case ISD::LOAD: {
8038 LoadSDNode *LD = cast<LoadSDNode>(N);
8039 EVT VT = LD->getValueType(0);
8040 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
8041 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
8042 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
8043 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
Hal Finkel40c34782013-09-15 22:09:58 +00008044 (VT == MVT::v16i8 || VT == MVT::v8i16 ||
8045 VT == MVT::v4i32 || VT == MVT::v4f32) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +00008046 LD->getAlignment() < ABIAlignment) {
8047 // This is a type-legal unaligned Altivec load.
8048 SDValue Chain = LD->getChain();
8049 SDValue Ptr = LD->getBasePtr();
8050
8051 // This implements the loading of unaligned vectors as described in
8052 // the venerable Apple Velocity Engine overview. Specifically:
8053 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
8054 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
8055 //
8056 // The general idea is to expand a sequence of one or more unaligned
8057 // loads into a alignment-based permutation-control instruction (lvsl),
8058 // a series of regular vector loads (which always truncate their
8059 // input address to an aligned address), and a series of permutations.
8060 // The results of these permutations are the requested loaded values.
8061 // The trick is that the last "extra" load is not taken from the address
8062 // you might suspect (sizeof(vector) bytes after the last requested
8063 // load), but rather sizeof(vector) - 1 bytes after the last
8064 // requested vector. The point of this is to avoid a page fault if the
Alp Tokercb402912014-01-24 17:20:08 +00008065 // base address happened to be aligned. This works because if the base
Hal Finkelcf2e9082013-05-24 23:00:14 +00008066 // address is aligned, then adding less than a full vector length will
8067 // cause the last vector in the sequence to be (re)loaded. Otherwise,
8068 // the next vector will be fetched as you might suspect was necessary.
8069
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008070 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00008071 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008072 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
8073 // optimization later.
Hal Finkelcf2e9082013-05-24 23:00:14 +00008074 SDValue PermCntl = BuildIntrinsicOp(Intrinsic::ppc_altivec_lvsl, Ptr,
8075 DAG, dl, MVT::v16i8);
8076
8077 // Refine the alignment of the original load (a "new" load created here
8078 // which was identical to the first except for the alignment would be
8079 // merged with the existing node regardless).
8080 MachineFunction &MF = DAG.getMachineFunction();
8081 MachineMemOperand *MMO =
8082 MF.getMachineMemOperand(LD->getPointerInfo(),
8083 LD->getMemOperand()->getFlags(),
8084 LD->getMemoryVT().getStoreSize(),
8085 ABIAlignment);
8086 LD->refineAlignment(MMO);
8087 SDValue BaseLoad = SDValue(LD, 0);
8088
8089 // Note that the value of IncOffset (which is provided to the next
8090 // load's pointer info offset value, and thus used to calculate the
8091 // alignment), and the value of IncValue (which is actually used to
8092 // increment the pointer value) are different! This is because we
8093 // require the next load to appear to be aligned, even though it
8094 // is actually offset from the base pointer by a lesser amount.
8095 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +00008096 int IncValue = IncOffset;
8097
8098 // Walk (both up and down) the chain looking for another load at the real
8099 // (aligned) offset (the alignment of the other load does not matter in
8100 // this case). If found, then do not use the offset reduction trick, as
8101 // that will prevent the loads from being later combined (as they would
8102 // otherwise be duplicates).
8103 if (!findConsecutiveLoad(LD, DAG))
8104 --IncValue;
8105
Hal Finkelcf2e9082013-05-24 23:00:14 +00008106 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
8107 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
8108
Hal Finkelcf2e9082013-05-24 23:00:14 +00008109 SDValue ExtraLoad =
8110 DAG.getLoad(VT, dl, Chain, Ptr,
8111 LD->getPointerInfo().getWithOffset(IncOffset),
8112 LD->isVolatile(), LD->isNonTemporal(),
8113 LD->isInvariant(), ABIAlignment);
8114
8115 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8116 BaseLoad.getValue(1), ExtraLoad.getValue(1));
8117
8118 if (BaseLoad.getValueType() != MVT::v4i32)
8119 BaseLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, BaseLoad);
8120
8121 if (ExtraLoad.getValueType() != MVT::v4i32)
8122 ExtraLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ExtraLoad);
8123
8124 SDValue Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
8125 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
8126
8127 if (VT != MVT::v4i32)
8128 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
8129
8130 // Now we need to be really careful about how we update the users of the
8131 // original load. We cannot just call DCI.CombineTo (or
8132 // DAG.ReplaceAllUsesWith for that matter), because the load still has
8133 // uses created here (the permutation for example) that need to stay.
8134 SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
8135 while (UI != UE) {
8136 SDUse &Use = UI.getUse();
8137 SDNode *User = *UI;
8138 // Note: BaseLoad is checked here because it might not be N, but a
8139 // bitcast of N.
8140 if (User == Perm.getNode() || User == BaseLoad.getNode() ||
8141 User == TF.getNode() || Use.getResNo() > 1) {
8142 ++UI;
8143 continue;
8144 }
8145
8146 SDValue To = Use.getResNo() ? TF : Perm;
8147 ++UI;
8148
8149 SmallVector<SDValue, 8> Ops;
8150 for (SDNode::op_iterator O = User->op_begin(),
8151 OE = User->op_end(); O != OE; ++O) {
8152 if (*O == Use)
8153 Ops.push_back(To);
8154 else
8155 Ops.push_back(*O);
8156 }
8157
Craig Topper8c0b4d02014-04-28 05:57:50 +00008158 DAG.UpdateNodeOperands(User, Ops);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008159 }
8160
8161 return SDValue(N, 0);
8162 }
8163 }
8164 break;
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008165 case ISD::INTRINSIC_WO_CHAIN:
8166 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() ==
8167 Intrinsic::ppc_altivec_lvsl &&
8168 N->getOperand(1)->getOpcode() == ISD::ADD) {
8169 SDValue Add = N->getOperand(1);
8170
8171 if (DAG.MaskedValueIsZero(Add->getOperand(1),
8172 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
8173 Add.getValueType().getScalarType().getSizeInBits()))) {
8174 SDNode *BasePtr = Add->getOperand(0).getNode();
8175 for (SDNode::use_iterator UI = BasePtr->use_begin(),
8176 UE = BasePtr->use_end(); UI != UE; ++UI) {
8177 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8178 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
8179 Intrinsic::ppc_altivec_lvsl) {
8180 // We've found another LVSL, and this address if an aligned
8181 // multiple of that one. The results will be the same, so use the
8182 // one we've just found instead.
8183
8184 return SDValue(*UI, 0);
8185 }
8186 }
8187 }
8188 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +00008189
8190 break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008191 case ISD::BSWAP:
8192 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008193 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +00008194 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008195 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
8196 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008197 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008198 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008199 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00008200 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +00008201 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008202 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +00008203 LD->getChain(), // Chain
8204 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008205 DAG.getValueType(N->getValueType(0)) // VT
8206 };
Dan Gohman48b185d2009-09-25 20:36:54 +00008207 SDValue BSLoad =
8208 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +00008209 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
8210 MVT::i64 : MVT::i32, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00008211 Ops, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008212
Scott Michelcf0da6c2009-02-17 22:15:04 +00008213 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008214 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +00008215 if (N->getValueType(0) == MVT::i16)
8216 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008217
Chris Lattnera7976d32006-07-10 20:56:58 +00008218 // First, combine the bswap away. This makes the value produced by the
8219 // load dead.
8220 DCI.CombineTo(N, ResVal);
8221
8222 // Next, combine the load away, we give it a bogus result value but a real
8223 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008224 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00008225
Chris Lattnera7976d32006-07-10 20:56:58 +00008226 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008227 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008228 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008229
Chris Lattner27f53452006-03-01 05:50:56 +00008230 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008231 case PPCISD::VCMP: {
8232 // If a VCMPo node already exists with exactly the same operands as this
8233 // node, use its result instead of this node (VCMPo computes both a CR6 and
8234 // a normal output).
8235 //
8236 if (!N->getOperand(0).hasOneUse() &&
8237 !N->getOperand(1).hasOneUse() &&
8238 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00008239
Chris Lattnerd4058a52006-03-31 06:02:07 +00008240 // Scan all of the users of the LHS, looking for VCMPo's that match.
Craig Topper062a2ba2014-04-25 05:30:21 +00008241 SDNode *VCMPoNode = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008242
Gabor Greiff304a7a2008-08-28 21:40:38 +00008243 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +00008244 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
8245 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008246 if (UI->getOpcode() == PPCISD::VCMPo &&
8247 UI->getOperand(1) == N->getOperand(1) &&
8248 UI->getOperand(2) == N->getOperand(2) &&
8249 UI->getOperand(0) == N->getOperand(0)) {
8250 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008251 break;
8252 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008253
Chris Lattner518834c2006-04-18 18:28:22 +00008254 // If there is no VCMPo node, or if the flag value has a single use, don't
8255 // transform this.
8256 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
8257 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008258
8259 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +00008260 // chain, this transformation is more complex. Note that multiple things
8261 // could use the value result, which we should ignore.
Craig Topper062a2ba2014-04-25 05:30:21 +00008262 SDNode *FlagUser = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008263 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Craig Topper062a2ba2014-04-25 05:30:21 +00008264 FlagUser == nullptr; ++UI) {
Chris Lattner518834c2006-04-18 18:28:22 +00008265 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008266 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +00008267 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008268 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +00008269 FlagUser = User;
8270 break;
8271 }
8272 }
8273 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008274
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008275 // If the user is a MFOCRF instruction, we know this is safe.
8276 // Otherwise we give up for right now.
8277 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008278 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +00008279 }
8280 break;
8281 }
Hal Finkel940ab932014-02-28 00:27:01 +00008282 case ISD::BRCOND: {
8283 SDValue Cond = N->getOperand(1);
8284 SDValue Target = N->getOperand(2);
8285
8286 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8287 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
8288 Intrinsic::ppc_is_decremented_ctr_nonzero) {
8289
8290 // We now need to make the intrinsic dead (it cannot be instruction
8291 // selected).
8292 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
8293 assert(Cond.getNode()->hasOneUse() &&
8294 "Counter decrement has more than one use");
8295
8296 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
8297 N->getOperand(0), Target);
8298 }
8299 }
8300 break;
Chris Lattner9754d142006-04-18 17:59:36 +00008301 case ISD::BR_CC: {
8302 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008303 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +00008304 // lowering is done pre-legalize, because the legalizer lowers the predicate
8305 // compare down to code that is difficult to reassemble.
8306 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008307 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +00008308
8309 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
8310 // value. If so, pass-through the AND to get to the intrinsic.
8311 if (LHS.getOpcode() == ISD::AND &&
8312 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8313 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
8314 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8315 isa<ConstantSDNode>(LHS.getOperand(1)) &&
8316 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
8317 isZero())
8318 LHS = LHS.getOperand(0);
8319
8320 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8321 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
8322 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8323 isa<ConstantSDNode>(RHS)) {
8324 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
8325 "Counter decrement comparison is not EQ or NE");
8326
8327 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
8328 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
8329 (CC == ISD::SETNE && !Val);
8330
8331 // We now need to make the intrinsic dead (it cannot be instruction
8332 // selected).
8333 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
8334 assert(LHS.getNode()->hasOneUse() &&
8335 "Counter decrement has more than one use");
8336
8337 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
8338 N->getOperand(0), N->getOperand(4));
8339 }
8340
Chris Lattner9754d142006-04-18 17:59:36 +00008341 int CompareOpc;
8342 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008343
Chris Lattner9754d142006-04-18 17:59:36 +00008344 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8345 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
8346 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
8347 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00008348
Chris Lattner9754d142006-04-18 17:59:36 +00008349 // If this is a comparison against something other than 0/1, then we know
8350 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008351 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00008352 if (Val != 0 && Val != 1) {
8353 if (CC == ISD::SETEQ) // Cond never true, remove branch.
8354 return N->getOperand(0);
8355 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +00008356 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +00008357 N->getOperand(0), N->getOperand(4));
8358 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008359
Chris Lattner9754d142006-04-18 17:59:36 +00008360 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008361
Chris Lattner9754d142006-04-18 17:59:36 +00008362 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008363 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008364 LHS.getOperand(2), // LHS of compare
8365 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +00008366 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008367 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00008368 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00008369 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008370
Chris Lattner9754d142006-04-18 17:59:36 +00008371 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008372 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +00008373 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +00008374 default: // Can't happen, don't crash on invalid number though.
8375 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008376 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +00008377 break;
8378 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008379 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +00008380 break;
8381 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008382 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +00008383 break;
8384 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008385 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +00008386 break;
8387 }
8388
Owen Anderson9f944592009-08-11 20:47:22 +00008389 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
8390 DAG.getConstant(CompOpc, MVT::i32),
8391 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +00008392 N->getOperand(4), CompNode.getValue(1));
8393 }
8394 break;
8395 }
Chris Lattnerf4184352006-03-01 04:57:39 +00008396 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008397
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008398 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +00008399}
8400
Chris Lattner4211ca92006-04-14 06:01:58 +00008401//===----------------------------------------------------------------------===//
8402// Inline Assembly Support
8403//===----------------------------------------------------------------------===//
8404
Jay Foada0653a32014-05-14 21:14:37 +00008405void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
8406 APInt &KnownZero,
8407 APInt &KnownOne,
8408 const SelectionDAG &DAG,
8409 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00008410 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +00008411 switch (Op.getOpcode()) {
8412 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008413 case PPCISD::LBRX: {
8414 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +00008415 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +00008416 KnownZero = 0xFFFF0000;
8417 break;
8418 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008419 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00008420 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +00008421 default: break;
8422 case Intrinsic::ppc_altivec_vcmpbfp_p:
8423 case Intrinsic::ppc_altivec_vcmpeqfp_p:
8424 case Intrinsic::ppc_altivec_vcmpequb_p:
8425 case Intrinsic::ppc_altivec_vcmpequh_p:
8426 case Intrinsic::ppc_altivec_vcmpequw_p:
8427 case Intrinsic::ppc_altivec_vcmpgefp_p:
8428 case Intrinsic::ppc_altivec_vcmpgtfp_p:
8429 case Intrinsic::ppc_altivec_vcmpgtsb_p:
8430 case Intrinsic::ppc_altivec_vcmpgtsh_p:
8431 case Intrinsic::ppc_altivec_vcmpgtsw_p:
8432 case Intrinsic::ppc_altivec_vcmpgtub_p:
8433 case Intrinsic::ppc_altivec_vcmpgtuh_p:
8434 case Intrinsic::ppc_altivec_vcmpgtuw_p:
8435 KnownZero = ~1U; // All bits but the low one are known to be zero.
8436 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008437 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008438 }
8439 }
8440}
8441
8442
Chris Lattnerd6855142007-03-25 02:14:49 +00008443/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +00008444/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008445PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00008446PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
8447 if (Constraint.size() == 1) {
8448 switch (Constraint[0]) {
8449 default: break;
8450 case 'b':
8451 case 'r':
8452 case 'f':
8453 case 'v':
8454 case 'y':
8455 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +00008456 case 'Z':
8457 // FIXME: While Z does indicate a memory constraint, it specifically
8458 // indicates an r+r address (used in conjunction with the 'y' modifier
8459 // in the replacement string). Currently, we're forcing the base
8460 // register to be r0 in the asm printer (which is interpreted as zero)
8461 // and forming the complete address in the second register. This is
8462 // suboptimal.
8463 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +00008464 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008465 } else if (Constraint == "wc") { // individual CR bits.
8466 return C_RegisterClass;
Hal Finkel27774d92014-03-13 07:58:58 +00008467 } else if (Constraint == "wa" || Constraint == "wd" ||
8468 Constraint == "wf" || Constraint == "ws") {
8469 return C_RegisterClass; // VSX registers.
Chris Lattnerd6855142007-03-25 02:14:49 +00008470 }
8471 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +00008472}
8473
John Thompsone8360b72010-10-29 17:29:13 +00008474/// Examine constraint type and operand type and determine a weight value.
8475/// This object must already have been set up with the operand type
8476/// and the current alternative constraint selected.
8477TargetLowering::ConstraintWeight
8478PPCTargetLowering::getSingleConstraintMatchWeight(
8479 AsmOperandInfo &info, const char *constraint) const {
8480 ConstraintWeight weight = CW_Invalid;
8481 Value *CallOperandVal = info.CallOperandVal;
8482 // If we don't have a value, we can't do a match,
8483 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00008484 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00008485 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00008486 Type *type = CallOperandVal->getType();
Hal Finkel6aca2372014-03-02 18:23:39 +00008487
John Thompsone8360b72010-10-29 17:29:13 +00008488 // Look at the constraint type.
Hal Finkel6aca2372014-03-02 18:23:39 +00008489 if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
8490 return CW_Register; // an individual CR bit.
Hal Finkel27774d92014-03-13 07:58:58 +00008491 else if ((StringRef(constraint) == "wa" ||
8492 StringRef(constraint) == "wd" ||
8493 StringRef(constraint) == "wf") &&
8494 type->isVectorTy())
8495 return CW_Register;
8496 else if (StringRef(constraint) == "ws" && type->isDoubleTy())
8497 return CW_Register;
Hal Finkel6aca2372014-03-02 18:23:39 +00008498
John Thompsone8360b72010-10-29 17:29:13 +00008499 switch (*constraint) {
8500 default:
8501 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8502 break;
8503 case 'b':
8504 if (type->isIntegerTy())
8505 weight = CW_Register;
8506 break;
8507 case 'f':
8508 if (type->isFloatTy())
8509 weight = CW_Register;
8510 break;
8511 case 'd':
8512 if (type->isDoubleTy())
8513 weight = CW_Register;
8514 break;
8515 case 'v':
8516 if (type->isVectorTy())
8517 weight = CW_Register;
8518 break;
8519 case 'y':
8520 weight = CW_Register;
8521 break;
Hal Finkel4f24c622012-11-05 18:18:42 +00008522 case 'Z':
8523 weight = CW_Memory;
8524 break;
John Thompsone8360b72010-10-29 17:29:13 +00008525 }
8526 return weight;
8527}
8528
Scott Michelcf0da6c2009-02-17 22:15:04 +00008529std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner584a11a2006-11-02 01:44:04 +00008530PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00008531 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +00008532 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +00008533 // GCC RS6000 Constraint Letters
8534 switch (Constraint[0]) {
8535 case 'b': // R1-R31
Hal Finkel638a9fa2013-03-19 18:51:05 +00008536 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
8537 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
8538 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008539 case 'r': // R0-R31
Owen Anderson9f944592009-08-11 20:47:22 +00008540 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +00008541 return std::make_pair(0U, &PPC::G8RCRegClass);
8542 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008543 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008544 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +00008545 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008546 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +00008547 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008548 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008549 case 'v':
Craig Topperabadc662012-04-20 06:31:50 +00008550 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008551 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +00008552 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008553 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008554 } else if (Constraint == "wc") { // an individual CR bit.
8555 return std::make_pair(0U, &PPC::CRBITRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +00008556 } else if (Constraint == "wa" || Constraint == "wd" ||
Hal Finkel19be5062014-03-29 05:29:01 +00008557 Constraint == "wf") {
Hal Finkel27774d92014-03-13 07:58:58 +00008558 return std::make_pair(0U, &PPC::VSRCRegClass);
Hal Finkel19be5062014-03-29 05:29:01 +00008559 } else if (Constraint == "ws") {
8560 return std::make_pair(0U, &PPC::VSFRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008561 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008562
Hal Finkelb176acb2013-08-03 12:25:10 +00008563 std::pair<unsigned, const TargetRegisterClass*> R =
8564 TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8565
8566 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
8567 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
8568 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
8569 // register.
8570 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
8571 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
8572 if (R.first && VT == MVT::i64 && PPCSubTarget.isPPC64() &&
8573 PPC::GPRCRegClass.contains(R.first)) {
8574 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
8575 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +00008576 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +00008577 &PPC::G8RCRegClass);
8578 }
8579
8580 return R;
Chris Lattner01513612006-01-31 19:20:21 +00008581}
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008582
Chris Lattner584a11a2006-11-02 01:44:04 +00008583
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008584/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +00008585/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +00008586void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00008587 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008588 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00008589 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00008590 SDValue Result;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008591
Eric Christopherde9399b2011-06-02 23:16:42 +00008592 // Only support length 1 constraints.
8593 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008594
Eric Christopherde9399b2011-06-02 23:16:42 +00008595 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008596 switch (Letter) {
8597 default: break;
8598 case 'I':
8599 case 'J':
8600 case 'K':
8601 case 'L':
8602 case 'M':
8603 case 'N':
8604 case 'O':
8605 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +00008606 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008607 if (!CST) return; // Must be an immediate to match.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008608 unsigned Value = CST->getZExtValue();
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008609 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008610 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008611 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008612 if ((short)Value == (int)Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008613 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008614 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008615 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
8616 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008617 if ((short)Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008618 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008619 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008620 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008621 if ((Value >> 16) == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008622 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008623 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008624 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008625 if (Value > 31)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008626 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008627 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008628 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008629 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008630 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008631 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008632 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008633 if (Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008634 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008635 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008636 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008637 if ((short)-Value == (int)-Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008638 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008639 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008640 }
8641 break;
8642 }
8643 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008644
Gabor Greiff304a7a2008-08-28 21:40:38 +00008645 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008646 Ops.push_back(Result);
8647 return;
8648 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008649
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008650 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +00008651 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008652}
Evan Cheng2dd2c652006-03-13 23:20:37 +00008653
Chris Lattner1eb94d92007-03-30 23:15:24 +00008654// isLegalAddressingMode - Return true if the addressing mode represented
8655// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008656bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00008657 Type *Ty) const {
Chris Lattner1eb94d92007-03-30 23:15:24 +00008658 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelcf0da6c2009-02-17 22:15:04 +00008659
Chris Lattner1eb94d92007-03-30 23:15:24 +00008660 // PPC allows a sign-extended 16-bit immediate field.
8661 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
8662 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008663
Chris Lattner1eb94d92007-03-30 23:15:24 +00008664 // No global is ever allowed as a base.
8665 if (AM.BaseGV)
8666 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008667
8668 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +00008669 switch (AM.Scale) {
8670 case 0: // "r+i" or just "i", depending on HasBaseReg.
8671 break;
8672 case 1:
8673 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
8674 return false;
8675 // Otherwise we have r+r or r+i.
8676 break;
8677 case 2:
8678 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
8679 return false;
8680 // Allow 2*r as r+r.
8681 break;
Chris Lattner19ccd622007-04-09 22:10:05 +00008682 default:
8683 // No other scales are supported.
8684 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +00008685 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008686
Chris Lattner1eb94d92007-03-30 23:15:24 +00008687 return true;
8688}
8689
Dan Gohman21cea8a2010-04-17 15:26:15 +00008690SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
8691 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00008692 MachineFunction &MF = DAG.getMachineFunction();
8693 MachineFrameInfo *MFI = MF.getFrameInfo();
8694 MFI->setReturnAddressIsTaken(true);
8695
Bill Wendling908bf812014-01-06 00:43:20 +00008696 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008697 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008698
Andrew Trickef9de2a2013-05-25 02:42:55 +00008699 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008700 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +00008701
Dale Johannesen81bfca72010-05-03 22:59:34 +00008702 // Make sure the function does not optimize away the store of the RA to
8703 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +00008704 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008705 FuncInfo->setLRStoreRequired();
8706 bool isPPC64 = PPCSubTarget.isPPC64();
8707 bool isDarwinABI = PPCSubTarget.isDarwinABI();
8708
8709 if (Depth > 0) {
8710 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8711 SDValue Offset =
Wesley Peck527da1b2010-11-23 03:31:01 +00008712
Anton Korobeynikov2f931282011-01-10 12:39:04 +00008713 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen81bfca72010-05-03 22:59:34 +00008714 isPPC64? MVT::i64 : MVT::i32);
8715 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
8716 DAG.getNode(ISD::ADD, dl, getPointerTy(),
8717 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008718 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008719 }
Chris Lattnerf6a81562007-12-08 06:59:59 +00008720
Chris Lattnerf6a81562007-12-08 06:59:59 +00008721 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008722 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008723 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008724 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +00008725}
8726
Dan Gohman21cea8a2010-04-17 15:26:15 +00008727SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
8728 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008729 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008730 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00008731
Owen Anderson53aa7a92009-08-10 22:56:29 +00008732 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00008733 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008734
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008735 MachineFunction &MF = DAG.getMachineFunction();
8736 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008737 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +00008738
8739 // Naked functions never have a frame pointer, and so we use r1. For all
8740 // other functions, this decision must be delayed until during PEI.
8741 unsigned FrameReg;
8742 if (MF.getFunction()->getAttributes().hasAttribute(
8743 AttributeSet::FunctionIndex, Attribute::Naked))
8744 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
8745 else
8746 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
8747
Dale Johannesen81bfca72010-05-03 22:59:34 +00008748 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
8749 PtrVT);
8750 while (Depth--)
8751 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008752 FrameAddr, MachinePointerInfo(), false, false,
8753 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008754 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008755}
Dan Gohmanc14e5222008-10-21 03:41:46 +00008756
Hal Finkel0d8db462014-05-11 19:29:11 +00008757// FIXME? Maybe this could be a TableGen attribute on some registers and
8758// this table could be generated automatically from RegInfo.
8759unsigned PPCTargetLowering::getRegisterByName(const char* RegName,
8760 EVT VT) const {
8761 bool isPPC64 = PPCSubTarget.isPPC64();
8762 bool isDarwinABI = PPCSubTarget.isDarwinABI();
8763
8764 if ((isPPC64 && VT != MVT::i64 && VT != MVT::i32) ||
8765 (!isPPC64 && VT != MVT::i32))
8766 report_fatal_error("Invalid register global variable type");
8767
8768 bool is64Bit = isPPC64 && VT == MVT::i64;
8769 unsigned Reg = StringSwitch<unsigned>(RegName)
8770 .Case("r1", is64Bit ? PPC::X1 : PPC::R1)
8771 .Case("r2", isDarwinABI ? 0 : (is64Bit ? PPC::X2 : PPC::R2))
8772 .Case("r13", (!isPPC64 && isDarwinABI) ? 0 :
8773 (is64Bit ? PPC::X13 : PPC::R13))
8774 .Default(0);
8775
8776 if (Reg)
8777 return Reg;
8778 report_fatal_error("Invalid register name global variable");
8779}
8780
Dan Gohmanc14e5222008-10-21 03:41:46 +00008781bool
8782PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8783 // The PowerPC target isn't yet aware of offsets.
8784 return false;
8785}
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008786
Evan Chengd9929f02010-04-01 20:10:42 +00008787/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +00008788/// and store operations as a result of memset, memcpy, and memmove
8789/// lowering. If DstAlign is zero that means it's safe to destination
8790/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
8791/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +00008792/// probably because the source does not need to be loaded. If 'IsMemset' is
8793/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
8794/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
8795/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +00008796/// It returns EVT::Other if the type should be determined using generic
8797/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +00008798EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
8799 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00008800 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00008801 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +00008802 MachineFunction &MF) const {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008803 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +00008804 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008805 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00008806 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008807 }
8808}
Hal Finkel88ed4e32012-04-01 19:23:08 +00008809
Hal Finkel34974ed2014-04-12 21:52:38 +00008810/// \brief Returns true if it is beneficial to convert a load of a constant
8811/// to just the constant itself.
8812bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
8813 Type *Ty) const {
8814 assert(Ty->isIntegerTy());
8815
8816 unsigned BitSize = Ty->getPrimitiveSizeInBits();
8817 if (BitSize == 0 || BitSize > 64)
8818 return false;
8819 return true;
8820}
8821
8822bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
8823 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
8824 return false;
8825 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
8826 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
8827 return NumBits1 == 64 && NumBits2 == 32;
8828}
8829
8830bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
8831 if (!VT1.isInteger() || !VT2.isInteger())
8832 return false;
8833 unsigned NumBits1 = VT1.getSizeInBits();
8834 unsigned NumBits2 = VT2.getSizeInBits();
8835 return NumBits1 == 64 && NumBits2 == 32;
8836}
8837
8838bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
8839 return isInt<16>(Imm) || isUInt<16>(Imm);
8840}
8841
8842bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8843 return isInt<16>(Imm) || isUInt<16>(Imm);
8844}
8845
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008846bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +00008847 unsigned,
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008848 bool *Fast) const {
8849 if (DisablePPCUnaligned)
8850 return false;
8851
8852 // PowerPC supports unaligned memory access for simple non-vector types.
8853 // Although accessing unaligned addresses is not as efficient as accessing
8854 // aligned addresses, it is generally more efficient than manual expansion,
8855 // and generally only traps for software emulation when crossing page
8856 // boundaries.
8857
8858 if (!VT.isSimple())
8859 return false;
8860
Hal Finkel6e28e6a2014-03-26 19:39:09 +00008861 if (VT.getSimpleVT().isVector()) {
8862 if (PPCSubTarget.hasVSX()) {
8863 if (VT != MVT::v2f64 && VT != MVT::v2i64)
8864 return false;
8865 } else {
8866 return false;
8867 }
8868 }
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008869
8870 if (VT == MVT::ppcf128)
8871 return false;
8872
8873 if (Fast)
8874 *Fast = true;
8875
8876 return true;
8877}
8878
Stephen Lin73de7bf2013-07-09 18:16:56 +00008879bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
8880 VT = VT.getScalarType();
8881
Hal Finkel0a479ae2012-06-22 00:49:52 +00008882 if (!VT.isSimple())
8883 return false;
8884
8885 switch (VT.getSimpleVT().SimpleTy) {
8886 case MVT::f32:
8887 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +00008888 return true;
8889 default:
8890 break;
8891 }
8892
8893 return false;
8894}
8895
Hal Finkelb4240ca2014-03-31 17:48:16 +00008896bool
8897PPCTargetLowering::shouldExpandBuildVectorWithShuffles(
8898 EVT VT , unsigned DefinedValues) const {
8899 if (VT == MVT::v2i64)
8900 return false;
8901
8902 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
8903}
8904
Hal Finkel88ed4e32012-04-01 19:23:08 +00008905Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel21442b22013-09-11 23:05:25 +00008906 if (DisableILPPref || PPCSubTarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008907 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +00008908
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008909 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +00008910}
8911
Bill Schmidt0cf702f2013-07-30 00:50:39 +00008912// Create a fast isel object.
8913FastISel *
8914PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
8915 const TargetLibraryInfo *LibInfo) const {
8916 return PPC::createFastISel(FuncInfo, LibInfo);
8917}