blob: a0b3e467d209544b22ceb953078308022967b942 [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);
463 setOperationAction(ISD::CTPOP, VT, Expand);
464 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000465 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000466 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000467 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000468 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000469 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
470
471 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
472 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
473 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
474 setTruncStoreAction(VT, InnerVT, Expand);
475 }
476 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
477 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
478 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000479 }
480
Chris Lattner95c7adc2006-04-04 17:25:31 +0000481 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
482 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000483 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000484
Owen Anderson9f944592009-08-11 20:47:22 +0000485 setOperationAction(ISD::AND , MVT::v4i32, Legal);
486 setOperationAction(ISD::OR , MVT::v4i32, Legal);
487 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
488 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Hal Finkel940ab932014-02-28 00:27:01 +0000489 setOperationAction(ISD::SELECT, MVT::v4i32,
490 Subtarget->useCRBits() ? Legal : Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000491 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000492 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
493 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
494 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
495 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000496 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
497 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
498 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
499 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000500
Craig Topperabadc662012-04-20 06:31:50 +0000501 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
502 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
503 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
504 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000505
Owen Anderson9f944592009-08-11 20:47:22 +0000506 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000507 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000508
Hal Finkel27774d92014-03-13 07:58:58 +0000509 if (TM.Options.UnsafeFPMath || Subtarget->hasVSX()) {
Hal Finkel2e103312013-04-03 04:01:11 +0000510 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
511 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
512 }
513
Owen Anderson9f944592009-08-11 20:47:22 +0000514 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
515 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
516 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000517
Owen Anderson9f944592009-08-11 20:47:22 +0000518 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
519 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000520
Owen Anderson9f944592009-08-11 20:47:22 +0000521 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
522 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
523 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
524 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000525
526 // Altivec does not contain unordered floating-point compare instructions
527 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
528 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
529 setCondCodeAction(ISD::SETUGT, MVT::v4f32, Expand);
530 setCondCodeAction(ISD::SETUGE, MVT::v4f32, Expand);
531 setCondCodeAction(ISD::SETULT, MVT::v4f32, Expand);
532 setCondCodeAction(ISD::SETULE, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000533
534 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
535 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000536
537 if (Subtarget->hasVSX()) {
538 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
Hal Finkel82569b62014-03-27 22:22:48 +0000539 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
Hal Finkel27774d92014-03-13 07:58:58 +0000540
541 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
542 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
543 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
544 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
545 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
546
547 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
548
549 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
550 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
551
552 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
553 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
554
Hal Finkel732f0f72014-03-26 12:49:28 +0000555 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
556 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal);
557 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
558 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
559 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
560
Hal Finkel27774d92014-03-13 07:58:58 +0000561 // Share the Altivec comparison restrictions.
562 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand);
563 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand);
564 setCondCodeAction(ISD::SETUGT, MVT::v2f64, Expand);
565 setCondCodeAction(ISD::SETUGE, MVT::v2f64, Expand);
566 setCondCodeAction(ISD::SETULT, MVT::v2f64, Expand);
567 setCondCodeAction(ISD::SETULE, MVT::v2f64, Expand);
568
569 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand);
570 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand);
571
Hal Finkel9281c9a2014-03-26 18:26:30 +0000572 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
573 setOperationAction(ISD::STORE, MVT::v2f64, Legal);
574
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000575 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal);
576
Hal Finkel19be5062014-03-29 05:29:01 +0000577 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000578
579 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass);
580 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass);
Hal Finkela6c8b512014-03-26 16:12:58 +0000581
582 // VSX v2i64 only supports non-arithmetic operations.
583 setOperationAction(ISD::ADD, MVT::v2i64, Expand);
584 setOperationAction(ISD::SUB, MVT::v2i64, Expand);
585
Hal Finkelad801b72014-03-27 21:26:33 +0000586 setOperationAction(ISD::SHL, MVT::v2i64, Expand);
587 setOperationAction(ISD::SRA, MVT::v2i64, Expand);
588 setOperationAction(ISD::SRL, MVT::v2i64, Expand);
589
Hal Finkel777c9dd2014-03-29 16:04:40 +0000590 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
591
Hal Finkel9281c9a2014-03-26 18:26:30 +0000592 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
593 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64);
594 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
595 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64);
596
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000597 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal);
598
Hal Finkel7279f4b2014-03-26 19:13:54 +0000599 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
600 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
601 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
602 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
603
Hal Finkel5c0d1452014-03-30 13:22:59 +0000604 // Vector operation legalization checks the result type of
605 // SIGN_EXTEND_INREG, overall legalization checks the inner type.
606 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal);
607 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal);
608 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
609 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
610
Hal Finkela6c8b512014-03-26 16:12:58 +0000611 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000612 }
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000613 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000614
Hal Finkel70381a72012-08-04 14:10:46 +0000615 if (Subtarget->has64BitSupport()) {
Hal Finkel322e41a2012-04-01 20:08:17 +0000616 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel70381a72012-08-04 14:10:46 +0000617 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
618 }
Hal Finkel322e41a2012-04-01 20:08:17 +0000619
Eli Friedman7dfa7912011-08-29 18:23:02 +0000620 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
621 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkel1b5ff082012-12-25 17:22:53 +0000622 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
623 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000624
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000625 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidta76bf5a2013-04-23 18:49:44 +0000626 // Altivec instructions set fields to all zeros or all ones.
627 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000628
Evan Cheng39e90022012-07-02 22:39:56 +0000629 if (isPPC64) {
Chris Lattner454436d2006-10-18 01:20:43 +0000630 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000631 setExceptionPointerRegister(PPC::X3);
632 setExceptionSelectorRegister(PPC::X4);
633 } else {
Chris Lattner454436d2006-10-18 01:20:43 +0000634 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000635 setExceptionPointerRegister(PPC::R3);
636 setExceptionSelectorRegister(PPC::R4);
637 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000638
Chris Lattnerf4184352006-03-01 04:57:39 +0000639 // We have target-specific dag combine patterns for the following nodes:
640 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000641 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000642 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000643 setTargetDAGCombine(ISD::BR_CC);
Hal Finkel940ab932014-02-28 00:27:01 +0000644 if (Subtarget->useCRBits())
645 setTargetDAGCombine(ISD::BRCOND);
Chris Lattnera7976d32006-07-10 20:56:58 +0000646 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000647 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000648
Hal Finkel46043ed2014-03-01 21:36:57 +0000649 setTargetDAGCombine(ISD::SIGN_EXTEND);
650 setTargetDAGCombine(ISD::ZERO_EXTEND);
651 setTargetDAGCombine(ISD::ANY_EXTEND);
652
Hal Finkel940ab932014-02-28 00:27:01 +0000653 if (Subtarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000654 setTargetDAGCombine(ISD::TRUNCATE);
655 setTargetDAGCombine(ISD::SETCC);
656 setTargetDAGCombine(ISD::SELECT_CC);
657 }
658
Hal Finkel2e103312013-04-03 04:01:11 +0000659 // Use reciprocal estimates.
660 if (TM.Options.UnsafeFPMath) {
661 setTargetDAGCombine(ISD::FDIV);
662 setTargetDAGCombine(ISD::FSQRT);
663 }
664
Dale Johannesen10432e52007-10-19 00:59:18 +0000665 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng39e90022012-07-02 22:39:56 +0000666 if (Subtarget->isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000667 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000668 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
669 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000670 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
671 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000672 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
673 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
674 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
675 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
676 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000677 }
678
Hal Finkel940ab932014-02-28 00:27:01 +0000679 // With 32 condition bits, we don't need to sink (and duplicate) compares
680 // aggressively in CodeGenPrep.
681 if (Subtarget->useCRBits())
682 setHasMultipleConditionRegisters();
683
Hal Finkel65298572011-10-17 18:53:03 +0000684 setMinFunctionAlignment(2);
685 if (PPCSubTarget.isDarwin())
686 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000687
Evan Cheng39e90022012-07-02 22:39:56 +0000688 if (isPPC64 && Subtarget->isJITCodeModel())
689 // Temporary workaround for the inability of PPC64 JIT to handle jump
690 // tables.
691 setSupportJumpTables(false);
692
Eli Friedman30a49e92011-08-03 21:06:02 +0000693 setInsertFencesForAtomic(true);
694
Hal Finkel21442b22013-09-11 23:05:25 +0000695 if (Subtarget->enableMachineScheduler())
696 setSchedulingPreference(Sched::Source);
697 else
698 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000699
Chris Lattnerf22556d2005-08-16 17:14:42 +0000700 computeRegisterProperties();
Hal Finkel742b5352012-08-28 16:12:39 +0000701
702 // The Freescale cores does better with aggressive inlining of memcpy and
703 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
704 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
705 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000706 MaxStoresPerMemset = 32;
707 MaxStoresPerMemsetOptSize = 16;
708 MaxStoresPerMemcpy = 32;
709 MaxStoresPerMemcpyOptSize = 8;
710 MaxStoresPerMemmove = 32;
711 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel742b5352012-08-28 16:12:39 +0000712
713 setPrefFunctionAlignment(4);
Hal Finkel742b5352012-08-28 16:12:39 +0000714 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000715}
716
Hal Finkel262a2242013-09-12 23:20:06 +0000717/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
718/// the desired ByVal argument alignment.
719static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
720 unsigned MaxMaxAlign) {
721 if (MaxAlign == MaxMaxAlign)
722 return;
723 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
724 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
725 MaxAlign = 32;
726 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
727 MaxAlign = 16;
728 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
729 unsigned EltAlign = 0;
730 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
731 if (EltAlign > MaxAlign)
732 MaxAlign = EltAlign;
733 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
734 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
735 unsigned EltAlign = 0;
736 getMaxByValAlign(STy->getElementType(i), EltAlign, MaxMaxAlign);
737 if (EltAlign > MaxAlign)
738 MaxAlign = EltAlign;
739 if (MaxAlign == MaxMaxAlign)
740 break;
741 }
742 }
743}
744
Dale Johannesencbde4c22008-02-28 22:31:51 +0000745/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
746/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000747unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000748 // Darwin passes everything on 4 byte boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000749 if (PPCSubTarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000750 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000751
752 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000753 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000754 unsigned Align = PPCSubTarget.isPPC64() ? 8 : 4;
755 if (PPCSubTarget.hasAltivec() || PPCSubTarget.hasQPX())
756 getMaxByValAlign(Ty, Align, PPCSubTarget.hasQPX() ? 32 : 16);
757 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000758}
759
Chris Lattner347ed8a2006-01-09 23:52:17 +0000760const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
761 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000762 default: return nullptr;
Evan Cheng32e376f2008-07-12 02:23:19 +0000763 case PPCISD::FSEL: return "PPCISD::FSEL";
764 case PPCISD::FCFID: return "PPCISD::FCFID";
765 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
766 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000767 case PPCISD::FRE: return "PPCISD::FRE";
768 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000769 case PPCISD::STFIWX: return "PPCISD::STFIWX";
770 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
771 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
772 case PPCISD::VPERM: return "PPCISD::VPERM";
773 case PPCISD::Hi: return "PPCISD::Hi";
774 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000775 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller79fef932009-12-18 13:00:15 +0000776 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
777 case PPCISD::LOAD: return "PPCISD::LOAD";
778 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000779 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
780 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
781 case PPCISD::SRL: return "PPCISD::SRL";
782 case PPCISD::SRA: return "PPCISD::SRA";
783 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000784 case PPCISD::CALL: return "PPCISD::CALL";
785 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000786 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000787 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng32e376f2008-07-12 02:23:19 +0000788 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel756810f2013-03-21 21:37:52 +0000789 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
790 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000791 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng32e376f2008-07-12 02:23:19 +0000792 case PPCISD::VCMP: return "PPCISD::VCMP";
793 case PPCISD::VCMPo: return "PPCISD::VCMPo";
794 case PPCISD::LBRX: return "PPCISD::LBRX";
795 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng32e376f2008-07-12 02:23:19 +0000796 case PPCISD::LARX: return "PPCISD::LARX";
797 case PPCISD::STCX: return "PPCISD::STCX";
798 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +0000799 case PPCISD::BDNZ: return "PPCISD::BDNZ";
800 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000801 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000802 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000803 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +0000804 case PPCISD::CR6SET: return "PPCISD::CR6SET";
805 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34627e32012-11-27 17:35:46 +0000806 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
807 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
808 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Roman Divacky32143e22013-12-20 18:08:54 +0000809 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000810 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
811 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000812 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000813 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
814 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
815 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000816 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
817 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
818 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
819 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
820 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +0000821 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +0000822 case PPCISD::SC: return "PPCISD::SC";
Chris Lattner347ed8a2006-01-09 23:52:17 +0000823 }
824}
825
Matt Arsenault758659232013-05-18 00:21:46 +0000826EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000827 if (!VT.isVector())
Hal Finkel940ab932014-02-28 00:27:01 +0000828 return PPCSubTarget.useCRBits() ? MVT::i1 : MVT::i32;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000829 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000830}
831
Chris Lattner4211ca92006-04-14 06:01:58 +0000832//===----------------------------------------------------------------------===//
833// Node matching predicates, for use by the tblgen matching code.
834//===----------------------------------------------------------------------===//
835
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000836/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000837static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000838 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000839 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000840 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000841 // Maybe this has already been legalized into the constant pool?
842 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000843 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000844 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000845 }
846 return false;
847}
848
Chris Lattnere8b83b42006-04-06 17:23:16 +0000849/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
850/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000851static bool isConstantOrUndef(int Op, int Val) {
852 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000853}
854
855/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
856/// VPKUHUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000857bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000858 if (!isUnary) {
859 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000860 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000861 return false;
862 } else {
863 for (unsigned i = 0; i != 8; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000864 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
865 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000866 return false;
867 }
Chris Lattner1d338192006-04-06 18:26:28 +0000868 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000869}
870
871/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
872/// VPKUWUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000873bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000874 if (!isUnary) {
875 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000876 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
877 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000878 return false;
879 } else {
880 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000881 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
882 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
883 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
884 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000885 return false;
886 }
Chris Lattner1d338192006-04-06 18:26:28 +0000887 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000888}
889
Chris Lattnerf38e0332006-04-06 22:02:42 +0000890/// isVMerge - Common function, used to match vmrg* shuffles.
891///
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000892static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +0000893 unsigned LHSStart, unsigned RHSStart) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000894 if (N->getValueType(0) != MVT::v16i8)
895 return false;
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000896 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
897 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000898
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000899 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
900 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000901 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000902 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000903 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000904 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000905 return false;
906 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000907 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +0000908}
909
910/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
911/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000912bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000913 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000914 if (!isUnary)
915 return isVMerge(N, UnitSize, 8, 24);
916 return isVMerge(N, UnitSize, 8, 8);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000917}
918
919/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
920/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000921bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000922 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000923 if (!isUnary)
924 return isVMerge(N, UnitSize, 0, 16);
925 return isVMerge(N, UnitSize, 0, 0);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000926}
927
928
Chris Lattner1d338192006-04-06 18:26:28 +0000929/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
930/// amount, otherwise return -1.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000931int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000932 if (N->getValueType(0) != MVT::v16i8)
Hal Finkela775e512014-04-08 19:00:27 +0000933 return -1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000934
935 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +0000936
Chris Lattner1d338192006-04-06 18:26:28 +0000937 // Find the first non-undef value in the shuffle mask.
938 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000939 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +0000940 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000941
Chris Lattner1d338192006-04-06 18:26:28 +0000942 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000943
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000944 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +0000945 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000946 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +0000947 if (ShiftAmt < i) return -1;
948 ShiftAmt -= i;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000949
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000950 if (!isUnary) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000951 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000952 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000953 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000954 return -1;
955 } else {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000956 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000957 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000958 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000959 return -1;
960 }
Chris Lattner1d338192006-04-06 18:26:28 +0000961 return ShiftAmt;
962}
Chris Lattnerffc47562006-03-20 06:33:01 +0000963
964/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
965/// specifies a splat of a single element that is suitable for input to
966/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000967bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +0000968 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +0000969 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000970
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000971 // This is a splat operation if each element of the permute is the same, and
972 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000973 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +0000974
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000975 // FIXME: Handle UNDEF elements too!
976 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +0000977 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000978
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000979 // Check that the indices are consecutive, in the case of a multi-byte element
980 // splatted with a v16i8 mask.
981 for (unsigned i = 1; i != EltSize; ++i)
982 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000983 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000984
Chris Lattner95c7adc2006-04-04 17:25:31 +0000985 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000986 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +0000987 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000988 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000989 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000990 }
Chris Lattner95c7adc2006-04-04 17:25:31 +0000991 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +0000992}
993
Evan Cheng581d2792007-07-30 07:51:22 +0000994/// isAllNegativeZeroVector - Returns true if all elements of build_vector
995/// are -0.0.
996bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000997 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
998
999 APInt APVal, APUndef;
1000 unsigned BitSize;
1001 bool HasAnyUndefs;
Wesley Peck527da1b2010-11-23 03:31:01 +00001002
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00001003 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001004 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001005 return CFP->getValueAPF().isNegZero();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001006
Evan Cheng581d2792007-07-30 07:51:22 +00001007 return false;
1008}
1009
Chris Lattnerffc47562006-03-20 06:33:01 +00001010/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
1011/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner95c7adc2006-04-04 17:25:31 +00001012unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001013 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1014 assert(isSplatShuffleMask(SVOp, EltSize));
1015 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +00001016}
1017
Chris Lattner74cf9ff2006-04-12 17:37:20 +00001018/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001019/// by using a vspltis[bhw] instruction of the specified element size, return
1020/// the constant being splatted. The ByteSize field indicates the number of
1021/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001022SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001023 SDValue OpVal(nullptr, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001024
1025 // If ByteSize of the splat is bigger than the element size of the
1026 // build_vector, then we have a case where we are checking for a splat where
1027 // multiple elements of the buildvector are folded together into a single
1028 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
1029 unsigned EltSize = 16/N->getNumOperands();
1030 if (EltSize < ByteSize) {
1031 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001032 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001033 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001034
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001035 // See if all of the elements in the buildvector agree across.
1036 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1037 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1038 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001039 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001040
Scott Michelcf0da6c2009-02-17 22:15:04 +00001041
Craig Topper062a2ba2014-04-25 05:30:21 +00001042 if (!UniquedVals[i&(Multiple-1)].getNode())
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001043 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
1044 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001045 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001046 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001047
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001048 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
1049 // either constant or undef values that are identical for each chunk. See
1050 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001051
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001052 // Check to see if all of the leading entries are either 0 or -1. If
1053 // neither, then this won't fit into the immediate field.
1054 bool LeadingZero = true;
1055 bool LeadingOnes = true;
1056 for (unsigned i = 0; i != Multiple-1; ++i) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001057 if (!UniquedVals[i].getNode()) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001058
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001059 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
1060 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
1061 }
1062 // Finally, check the least significant entry.
1063 if (LeadingZero) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001064 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001065 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00001066 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001067 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +00001068 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001069 }
1070 if (LeadingOnes) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001071 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001072 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +00001073 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001074 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +00001075 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001076 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001077
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001078 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001079 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001080
Chris Lattner2771e2c2006-03-25 06:12:06 +00001081 // Check to see if this buildvec has a single non-undef value in its elements.
1082 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1083 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Craig Topper062a2ba2014-04-25 05:30:21 +00001084 if (!OpVal.getNode())
Chris Lattner2771e2c2006-03-25 06:12:06 +00001085 OpVal = N->getOperand(i);
1086 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001087 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001088 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001089
Craig Topper062a2ba2014-04-25 05:30:21 +00001090 if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001091
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001092 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001093 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001094 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001095 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001096 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001097 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001098 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001099 }
1100
1101 // If the splat value is larger than the element value, then we can never do
1102 // this splat. The only case that we could fit the replicated bits into our
1103 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001104 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001105
Chris Lattner2771e2c2006-03-25 06:12:06 +00001106 // If the element value is larger than the splat value, cut it in half and
1107 // check to see if the two halves are equal. Continue doing this until we
1108 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
1109 while (ValSizeInBytes > ByteSize) {
1110 ValSizeInBytes >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001111
Chris Lattner2771e2c2006-03-25 06:12:06 +00001112 // If the top half equals the bottom half, we're still ok.
Chris Lattner39cc7172006-04-05 17:39:25 +00001113 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
1114 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001115 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001116 }
1117
1118 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001119 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001120
Evan Chengb1ddc982006-03-26 09:52:32 +00001121 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001122 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001123
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001124 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001125 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +00001126 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001127 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001128}
1129
Chris Lattner4211ca92006-04-14 06:01:58 +00001130//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001131// Addressing Mode Selection
1132//===----------------------------------------------------------------------===//
1133
1134/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1135/// or 64-bit immediate, and if the value can be accurately represented as a
1136/// sign extension from a 16-bit value. If so, this returns true and the
1137/// immediate.
1138static bool isIntS16Immediate(SDNode *N, short &Imm) {
1139 if (N->getOpcode() != ISD::Constant)
1140 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001141
Dan Gohmaneffb8942008-09-12 16:56:44 +00001142 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001143 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001144 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001145 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001146 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001147}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001148static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001149 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001150}
1151
1152
1153/// SelectAddressRegReg - Given the specified addressed, check to see if it
1154/// can be represented as an indexed [r+r] operation. Returns false if it
1155/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001156bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1157 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001158 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001159 short imm = 0;
1160 if (N.getOpcode() == ISD::ADD) {
1161 if (isIntS16Immediate(N.getOperand(1), imm))
1162 return false; // r+i
1163 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1164 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001165
Chris Lattnera801fced2006-11-08 02:15:41 +00001166 Base = N.getOperand(0);
1167 Index = N.getOperand(1);
1168 return true;
1169 } else if (N.getOpcode() == ISD::OR) {
1170 if (isIntS16Immediate(N.getOperand(1), imm))
1171 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001172
Chris Lattnera801fced2006-11-08 02:15:41 +00001173 // If this is an or of disjoint bitfields, we can codegen this as an add
1174 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1175 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001176 APInt LHSKnownZero, LHSKnownOne;
1177 APInt RHSKnownZero, RHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001178 DAG.computeKnownBits(N.getOperand(0),
1179 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001180
Dan Gohmanf19609a2008-02-27 01:23:58 +00001181 if (LHSKnownZero.getBoolValue()) {
Jay Foada0653a32014-05-14 21:14:37 +00001182 DAG.computeKnownBits(N.getOperand(1),
1183 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001184 // If all of the bits are known zero on the LHS or RHS, the add won't
1185 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001186 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001187 Base = N.getOperand(0);
1188 Index = N.getOperand(1);
1189 return true;
1190 }
1191 }
1192 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001193
Chris Lattnera801fced2006-11-08 02:15:41 +00001194 return false;
1195}
1196
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001197// If we happen to be doing an i64 load or store into a stack slot that has
1198// less than a 4-byte alignment, then the frame-index elimination may need to
1199// use an indexed load or store instruction (because the offset may not be a
1200// multiple of 4). The extra register needed to hold the offset comes from the
1201// register scavenger, and it is possible that the scavenger will need to use
1202// an emergency spill slot. As a result, we need to make sure that a spill slot
1203// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1204// stack slot.
1205static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1206 // FIXME: This does not handle the LWA case.
1207 if (VT != MVT::i64)
1208 return;
1209
Hal Finkel7ab3db52013-07-10 15:29:01 +00001210 // NOTE: We'll exclude negative FIs here, which come from argument
1211 // lowering, because there are no known test cases triggering this problem
1212 // using packed structures (or similar). We can remove this exclusion if
1213 // we find such a test case. The reason why this is so test-case driven is
1214 // because this entire 'fixup' is only to prevent crashes (from the
1215 // register scavenger) on not-really-valid inputs. For example, if we have:
1216 // %a = alloca i1
1217 // %b = bitcast i1* %a to i64*
1218 // store i64* a, i64 b
1219 // then the store should really be marked as 'align 1', but is not. If it
1220 // were marked as 'align 1' then the indexed form would have been
1221 // instruction-selected initially, and the problem this 'fixup' is preventing
1222 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001223 if (FrameIdx < 0)
1224 return;
1225
1226 MachineFunction &MF = DAG.getMachineFunction();
1227 MachineFrameInfo *MFI = MF.getFrameInfo();
1228
1229 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1230 if (Align >= 4)
1231 return;
1232
1233 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1234 FuncInfo->setHasNonRISpills();
1235}
1236
Chris Lattnera801fced2006-11-08 02:15:41 +00001237/// Returns true if the address N can be represented by a base register plus
1238/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001239/// represented as reg+reg. If Aligned is true, only accept displacements
1240/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001241bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001242 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001243 SelectionDAG &DAG,
1244 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001245 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001246 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001247 // If this can be more profitably realized as r+r, fail.
1248 if (SelectAddressRegReg(N, Disp, Base, DAG))
1249 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001250
Chris Lattnera801fced2006-11-08 02:15:41 +00001251 if (N.getOpcode() == ISD::ADD) {
1252 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001253 if (isIntS16Immediate(N.getOperand(1), imm) &&
1254 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001255 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001256 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1257 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001258 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001259 } else {
1260 Base = N.getOperand(0);
1261 }
1262 return true; // [r+i]
1263 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1264 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001265 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001266 && "Cannot handle constant offsets yet!");
1267 Disp = N.getOperand(1).getOperand(0); // The global address.
1268 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001269 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001270 Disp.getOpcode() == ISD::TargetConstantPool ||
1271 Disp.getOpcode() == ISD::TargetJumpTable);
1272 Base = N.getOperand(0);
1273 return true; // [&g+r]
1274 }
1275 } else if (N.getOpcode() == ISD::OR) {
1276 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001277 if (isIntS16Immediate(N.getOperand(1), imm) &&
1278 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001279 // If this is an or of disjoint bitfields, we can codegen this as an add
1280 // (for better address arithmetic) if the LHS and RHS of the OR are
1281 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001282 APInt LHSKnownZero, LHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001283 DAG.computeKnownBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001284
Dan Gohmanf19609a2008-02-27 01:23:58 +00001285 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001286 // If all of the bits are known zero on the LHS or RHS, the add won't
1287 // carry.
1288 Base = N.getOperand(0);
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001289 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001290 return true;
1291 }
1292 }
1293 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1294 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001295
Chris Lattnera801fced2006-11-08 02:15:41 +00001296 // If this address fits entirely in a 16-bit sext immediate field, codegen
1297 // this as "d, 0"
1298 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001299 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001300 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkelf70c41e2013-03-21 23:45:03 +00001301 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1302 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001303 return true;
1304 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001305
1306 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001307 if ((CN->getValueType(0) == MVT::i32 ||
1308 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1309 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001310 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001311
Chris Lattnera801fced2006-11-08 02:15:41 +00001312 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001313 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001314
Owen Anderson9f944592009-08-11 20:47:22 +00001315 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1316 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001317 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001318 return true;
1319 }
1320 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001321
Chris Lattnera801fced2006-11-08 02:15:41 +00001322 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001323 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001324 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001325 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1326 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001327 Base = N;
1328 return true; // [r+0]
1329}
1330
1331/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1332/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001333bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1334 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001335 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001336 // Check to see if we can easily represent this as an [r+r] address. This
1337 // will fail if it thinks that the address is more profitably represented as
1338 // reg+imm, e.g. where imm = 0.
1339 if (SelectAddressRegReg(N, Base, Index, DAG))
1340 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001341
Chris Lattnera801fced2006-11-08 02:15:41 +00001342 // If the operand is an addition, always emit this as [r+r], since this is
1343 // better (for code size, and execution, as the memop does the add for free)
1344 // than emitting an explicit add.
1345 if (N.getOpcode() == ISD::ADD) {
1346 Base = N.getOperand(0);
1347 Index = N.getOperand(1);
1348 return true;
1349 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001350
Chris Lattnera801fced2006-11-08 02:15:41 +00001351 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkelf70c41e2013-03-21 23:45:03 +00001352 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1353 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001354 Index = N;
1355 return true;
1356}
1357
Chris Lattnera801fced2006-11-08 02:15:41 +00001358/// getPreIndexedAddressParts - returns true by value, base pointer and
1359/// offset pointer and addressing mode by reference if the node's address
1360/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001361bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1362 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001363 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001364 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001365 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001366
Ulrich Weigande90b0222013-03-22 14:58:48 +00001367 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001368 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001369 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001370 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001371 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1372 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001373 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001374 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001375 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001376 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001377 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001378 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001379 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001380 } else
1381 return false;
1382
Chris Lattner68371252006-11-14 01:38:31 +00001383 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands13237ac2008-06-06 12:08:01 +00001384 if (VT.isVector())
Chris Lattner68371252006-11-14 01:38:31 +00001385 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001386
Ulrich Weigande90b0222013-03-22 14:58:48 +00001387 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1388
1389 // Common code will reject creating a pre-inc form if the base pointer
1390 // is a frame index, or if N is a store and the base pointer is either
1391 // the same as or a predecessor of the value being stored. Check for
1392 // those situations here, and try with swapped Base/Offset instead.
1393 bool Swap = false;
1394
1395 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1396 Swap = true;
1397 else if (!isLoad) {
1398 SDValue Val = cast<StoreSDNode>(N)->getValue();
1399 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1400 Swap = true;
1401 }
1402
1403 if (Swap)
1404 std::swap(Base, Offset);
1405
Hal Finkelca542be2012-06-20 15:43:03 +00001406 AM = ISD::PRE_INC;
1407 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001408 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001409
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001410 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001411 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001412 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001413 return false;
1414 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001415 // LDU/STU need an address with at least 4-byte alignment.
1416 if (Alignment < 4)
1417 return false;
1418
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001419 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001420 return false;
1421 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001422
Chris Lattnerb314b152006-11-11 00:08:42 +00001423 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001424 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1425 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001426 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001427 LD->getExtensionType() == ISD::SEXTLOAD &&
1428 isa<ConstantSDNode>(Offset))
1429 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001430 }
1431
Chris Lattnerce645542006-11-10 02:08:47 +00001432 AM = ISD::PRE_INC;
1433 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001434}
1435
1436//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001437// LowerOperation implementation
1438//===----------------------------------------------------------------------===//
1439
Chris Lattneredb9d842010-11-15 02:46:57 +00001440/// GetLabelAccessInfo - Return true if we should reference labels using a
1441/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1442static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Craig Topper062a2ba2014-04-25 05:30:21 +00001443 unsigned &LoOpFlags,
1444 const GlobalValue *GV = nullptr) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001445 HiOpFlags = PPCII::MO_HA;
1446 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001447
Chris Lattneredb9d842010-11-15 02:46:57 +00001448 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1449 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peck527da1b2010-11-23 03:31:01 +00001450 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattneredb9d842010-11-15 02:46:57 +00001451 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattnerdd6df842010-11-15 03:13:19 +00001452 if (isPIC) {
1453 HiOpFlags |= PPCII::MO_PIC_FLAG;
1454 LoOpFlags |= PPCII::MO_PIC_FLAG;
1455 }
1456
1457 // If this is a reference to a global value that requires a non-lazy-ptr, make
1458 // sure that instruction lowering adds it.
1459 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1460 HiOpFlags |= PPCII::MO_NLP_FLAG;
1461 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001462
Chris Lattnerdd6df842010-11-15 03:13:19 +00001463 if (GV->hasHiddenVisibility()) {
1464 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1465 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1466 }
1467 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001468
Chris Lattneredb9d842010-11-15 02:46:57 +00001469 return isPIC;
1470}
1471
1472static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1473 SelectionDAG &DAG) {
1474 EVT PtrVT = HiPart.getValueType();
1475 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001476 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001477
1478 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1479 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001480
Chris Lattneredb9d842010-11-15 02:46:57 +00001481 // With PIC, the first instruction is actually "GR+hi(&G)".
1482 if (isPIC)
1483 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1484 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001485
Chris Lattneredb9d842010-11-15 02:46:57 +00001486 // Generate non-pic code that has direct accesses to the constant pool.
1487 // The address of the global is just (hi(&g)+lo(&g)).
1488 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1489}
1490
Scott Michelcf0da6c2009-02-17 22:15:04 +00001491SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001492 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001493 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001494 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001495 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001496
Roman Divackyace47072012-08-24 16:26:02 +00001497 // 64-bit SVR4 ABI code is always position-independent.
1498 // The actual address of the GlobalValue is stored in the TOC.
1499 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1500 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001501 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001502 DAG.getRegister(PPC::X2, MVT::i64));
1503 }
1504
Chris Lattneredb9d842010-11-15 02:46:57 +00001505 unsigned MOHiFlag, MOLoFlag;
1506 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1507 SDValue CPIHi =
1508 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1509 SDValue CPILo =
1510 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1511 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001512}
1513
Dan Gohman21cea8a2010-04-17 15:26:15 +00001514SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001515 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001516 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001517
Roman Divackyace47072012-08-24 16:26:02 +00001518 // 64-bit SVR4 ABI code is always position-independent.
1519 // The actual address of the GlobalValue is stored in the TOC.
1520 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1521 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001522 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001523 DAG.getRegister(PPC::X2, MVT::i64));
1524 }
1525
Chris Lattneredb9d842010-11-15 02:46:57 +00001526 unsigned MOHiFlag, MOLoFlag;
1527 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1528 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1529 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1530 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001531}
1532
Dan Gohman21cea8a2010-04-17 15:26:15 +00001533SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1534 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001535 EVT PtrVT = Op.getValueType();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001536
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001537 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peck527da1b2010-11-23 03:31:01 +00001538
Chris Lattneredb9d842010-11-15 02:46:57 +00001539 unsigned MOHiFlag, MOLoFlag;
1540 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001541 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1542 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001543 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1544}
1545
Roman Divackye3f15c982012-06-04 17:36:38 +00001546SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1547 SelectionDAG &DAG) const {
1548
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001549 // FIXME: TLS addresses currently use medium model code sequences,
1550 // which is the most useful form. Eventually support for small and
1551 // large models could be added if users need it, at the cost of
1552 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001553 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001554 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001555 const GlobalValue *GV = GA->getGlobal();
1556 EVT PtrVT = getPointerTy();
1557 bool is64bit = PPCSubTarget.isPPC64();
1558
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001559 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001560
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001561 if (Model == TLSModel::LocalExec) {
1562 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001563 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001564 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001565 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001566 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1567 is64bit ? MVT::i64 : MVT::i32);
1568 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1569 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1570 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001571
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001572 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001573 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001574 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1575 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00001576 SDValue GOTPtr;
1577 if (is64bit) {
1578 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1579 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1580 PtrVT, GOTReg, TGA);
1581 } else
1582 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001583 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00001584 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001585 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001586 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001587
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001588 if (Model == TLSModel::GeneralDynamic) {
1589 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1590 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1591 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1592 GOTReg, TGA);
1593 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1594 GOTEntryHi, TGA);
1595
1596 // We need a chain node, and don't have one handy. The underlying
1597 // call has no side effects, so using the function entry node
1598 // suffices.
1599 SDValue Chain = DAG.getEntryNode();
1600 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1601 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1602 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1603 PtrVT, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001604 // The return value from GET_TLS_ADDR really is in X3 already, but
1605 // some hacks are needed here to tie everything together. The extra
1606 // copies dissolve during subsequent transforms.
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001607 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1608 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1609 }
1610
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001611 if (Model == TLSModel::LocalDynamic) {
1612 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1613 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1614 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1615 GOTReg, TGA);
1616 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1617 GOTEntryHi, TGA);
1618
1619 // We need a chain node, and don't have one handy. The underlying
1620 // call has no side effects, so using the function entry node
1621 // suffices.
1622 SDValue Chain = DAG.getEntryNode();
1623 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1624 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1625 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1626 PtrVT, ParmReg, TGA);
1627 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1628 // some hacks are needed here to tie everything together. The extra
1629 // copies dissolve during subsequent transforms.
1630 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1631 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt9ed4dbc2012-12-13 20:57:10 +00001632 Chain, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001633 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1634 }
1635
1636 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00001637}
1638
Chris Lattneredb9d842010-11-15 02:46:57 +00001639SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1640 SelectionDAG &DAG) const {
1641 EVT PtrVT = Op.getValueType();
1642 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001643 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00001644 const GlobalValue *GV = GSDN->getGlobal();
1645
Chris Lattneredb9d842010-11-15 02:46:57 +00001646 // 64-bit SVR4 ABI code is always position-independent.
1647 // The actual address of the GlobalValue is stored in the TOC.
1648 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1649 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1650 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1651 DAG.getRegister(PPC::X2, MVT::i64));
1652 }
1653
Chris Lattnerdd6df842010-11-15 03:13:19 +00001654 unsigned MOHiFlag, MOLoFlag;
1655 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00001656
Chris Lattnerdd6df842010-11-15 03:13:19 +00001657 SDValue GAHi =
1658 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1659 SDValue GALo =
1660 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00001661
Chris Lattnerdd6df842010-11-15 03:13:19 +00001662 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00001663
Chris Lattnerdd6df842010-11-15 03:13:19 +00001664 // If the global reference is actually to a non-lazy-pointer, we have to do an
1665 // extra load to get the address of the global.
1666 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1667 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001668 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00001669 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00001670}
1671
Dan Gohman21cea8a2010-04-17 15:26:15 +00001672SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00001673 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001674 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001675
Hal Finkel777c9dd2014-03-29 16:04:40 +00001676 if (Op.getValueType() == MVT::v2i64) {
1677 // When the operands themselves are v2i64 values, we need to do something
1678 // special because VSX has no underlying comparison operations for these.
1679 if (Op.getOperand(0).getValueType() == MVT::v2i64) {
1680 // Equality can be handled by casting to the legal type for Altivec
1681 // comparisons, everything else needs to be expanded.
1682 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
1683 return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
1684 DAG.getSetCC(dl, MVT::v4i32,
1685 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)),
1686 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)),
1687 CC));
1688 }
1689
1690 return SDValue();
1691 }
1692
1693 // We handle most of these in the usual way.
1694 return Op;
1695 }
1696
Chris Lattner4211ca92006-04-14 06:01:58 +00001697 // If we're comparing for equality to zero, expose the fact that this is
1698 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1699 // fold the new nodes.
1700 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1701 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001702 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001703 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001704 if (VT.bitsLT(MVT::i32)) {
1705 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001706 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001707 }
Duncan Sands13237ac2008-06-06 12:08:01 +00001708 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001709 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1710 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00001711 DAG.getConstant(Log2b, MVT::i32));
1712 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00001713 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001714 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00001715 // optimized. FIXME: revisit this when we can custom lower all setcc
1716 // optimizations.
1717 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001718 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001719 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001720
Chris Lattner4211ca92006-04-14 06:01:58 +00001721 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00001722 // by xor'ing the rhs with the lhs, which is faster than setting a
1723 // condition register, reading it back out, and masking the correct bit. The
1724 // normal approach here uses sub to do this instead of xor. Using xor exposes
1725 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001726 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00001727 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001728 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001729 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00001730 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001731 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00001732 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001733 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001734}
1735
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001736SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001737 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00001738 SDNode *Node = Op.getNode();
1739 EVT VT = Node->getValueType(0);
1740 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1741 SDValue InChain = Node->getOperand(0);
1742 SDValue VAListPtr = Node->getOperand(1);
1743 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001744 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001745
Roman Divacky4394e682011-06-28 15:30:42 +00001746 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1747
1748 // gpr_index
1749 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1750 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1751 false, false, 0);
1752 InChain = GprIndex.getValue(1);
1753
1754 if (VT == MVT::i64) {
1755 // Check if GprIndex is even
1756 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1757 DAG.getConstant(1, MVT::i32));
1758 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1759 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1760 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1761 DAG.getConstant(1, MVT::i32));
1762 // Align GprIndex to be even if it isn't
1763 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1764 GprIndex);
1765 }
1766
1767 // fpr index is 1 byte after gpr
1768 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1769 DAG.getConstant(1, MVT::i32));
1770
1771 // fpr
1772 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1773 FprPtr, MachinePointerInfo(SV), MVT::i8,
1774 false, false, 0);
1775 InChain = FprIndex.getValue(1);
1776
1777 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1778 DAG.getConstant(8, MVT::i32));
1779
1780 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1781 DAG.getConstant(4, MVT::i32));
1782
1783 // areas
1784 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001785 MachinePointerInfo(), false, false,
1786 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001787 InChain = OverflowArea.getValue(1);
1788
1789 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001790 MachinePointerInfo(), false, false,
1791 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001792 InChain = RegSaveArea.getValue(1);
1793
1794 // select overflow_area if index > 8
1795 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1796 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1797
Roman Divacky4394e682011-06-28 15:30:42 +00001798 // adjustment constant gpr_index * 4/8
1799 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1800 VT.isInteger() ? GprIndex : FprIndex,
1801 DAG.getConstant(VT.isInteger() ? 4 : 8,
1802 MVT::i32));
1803
1804 // OurReg = RegSaveArea + RegConstant
1805 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1806 RegConstant);
1807
1808 // Floating types are 32 bytes into RegSaveArea
1809 if (VT.isFloatingPoint())
1810 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1811 DAG.getConstant(32, MVT::i32));
1812
1813 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1814 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1815 VT.isInteger() ? GprIndex : FprIndex,
1816 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1817 MVT::i32));
1818
1819 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1820 VT.isInteger() ? VAListPtr : FprPtr,
1821 MachinePointerInfo(SV),
1822 MVT::i8, false, false, 0);
1823
1824 // determine if we should load from reg_save_area or overflow_area
1825 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1826
1827 // increase overflow_area by 4/8 if gpr/fpr > 8
1828 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1829 DAG.getConstant(VT.isInteger() ? 4 : 8,
1830 MVT::i32));
1831
1832 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1833 OverflowAreaPlusN);
1834
1835 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1836 OverflowAreaPtr,
1837 MachinePointerInfo(),
1838 MVT::i32, false, false, 0);
1839
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00001840 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001841 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001842}
1843
Roman Divackyc3825df2013-07-25 21:36:47 +00001844SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
1845 const PPCSubtarget &Subtarget) const {
1846 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
1847
1848 // We have to copy the entire va_list struct:
1849 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
1850 return DAG.getMemcpy(Op.getOperand(0), Op,
1851 Op.getOperand(1), Op.getOperand(2),
1852 DAG.getConstant(12, MVT::i32), 8, false, true,
1853 MachinePointerInfo(), MachinePointerInfo());
1854}
1855
Duncan Sandsa0984362011-09-06 13:37:06 +00001856SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1857 SelectionDAG &DAG) const {
1858 return Op.getOperand(0);
1859}
1860
1861SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1862 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00001863 SDValue Chain = Op.getOperand(0);
1864 SDValue Trmp = Op.getOperand(1); // trampoline
1865 SDValue FPtr = Op.getOperand(2); // nested function
1866 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00001867 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00001868
Owen Anderson53aa7a92009-08-10 22:56:29 +00001869 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00001870 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00001871 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001872 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00001873 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00001874
Scott Michelcf0da6c2009-02-17 22:15:04 +00001875 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00001876 TargetLowering::ArgListEntry Entry;
1877
1878 Entry.Ty = IntPtrTy;
1879 Entry.Node = Trmp; Args.push_back(Entry);
1880
1881 // TrampSize == (isPPC64 ? 48 : 40);
1882 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00001883 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00001884 Args.push_back(Entry);
1885
1886 Entry.Node = FPtr; Args.push_back(Entry);
1887 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001888
Bill Wendling95e1af22008-09-17 00:30:57 +00001889 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001890 TargetLowering::CallLoweringInfo CLI(DAG);
1891 CLI.setDebugLoc(dl).setChain(Chain)
1892 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
1893 DAG.getExternalSymbol("__trampoline_setup", PtrVT), &Args, 0);
Bill Wendling95e1af22008-09-17 00:30:57 +00001894
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001895 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Duncan Sandsa0984362011-09-06 13:37:06 +00001896 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00001897}
1898
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001899SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001900 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001901 MachineFunction &MF = DAG.getMachineFunction();
1902 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1903
Andrew Trickef9de2a2013-05-25 02:42:55 +00001904 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001905
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001906 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001907 // vastart just stores the address of the VarArgsFrameIndex slot into the
1908 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001909 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00001910 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001911 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00001912 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1913 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00001914 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001915 }
1916
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001917 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001918 // We suppose the given va_list is already allocated.
1919 //
1920 // typedef struct {
1921 // char gpr; /* index into the array of 8 GPRs
1922 // * stored in the register save area
1923 // * gpr=0 corresponds to r3,
1924 // * gpr=1 to r4, etc.
1925 // */
1926 // char fpr; /* index into the array of 8 FPRs
1927 // * stored in the register save area
1928 // * fpr=0 corresponds to f1,
1929 // * fpr=1 to f2, etc.
1930 // */
1931 // char *overflow_arg_area;
1932 // /* location on stack that holds
1933 // * the next overflow argument
1934 // */
1935 // char *reg_save_area;
1936 // /* where r3:r10 and f1:f8 (if saved)
1937 // * are stored
1938 // */
1939 // } va_list[1];
1940
1941
Dan Gohman31ae5862010-04-17 14:41:14 +00001942 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1943 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001944
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001945
Owen Anderson53aa7a92009-08-10 22:56:29 +00001946 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001947
Dan Gohman31ae5862010-04-17 14:41:14 +00001948 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1949 PtrVT);
1950 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1951 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001952
Duncan Sands13237ac2008-06-06 12:08:01 +00001953 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001954 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001955
Duncan Sands13237ac2008-06-06 12:08:01 +00001956 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001957 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001958
1959 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001960 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001961
Dan Gohman2d489b52008-02-06 22:27:42 +00001962 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001963
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001964 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001965 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00001966 Op.getOperand(1),
1967 MachinePointerInfo(SV),
1968 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001969 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001970 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001971 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001972
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001973 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001974 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00001975 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1976 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00001977 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001978 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001979 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001980
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001981 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001982 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00001983 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1984 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001985 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001986 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001987 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001988
1989 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00001990 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1991 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001992 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001993
Chris Lattner4211ca92006-04-14 06:01:58 +00001994}
1995
Chris Lattner4f2e4e02007-03-06 00:59:59 +00001996#include "PPCGenCallingConv.inc"
1997
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001998// Function whose sole purpose is to kill compiler warnings
1999// stemming from unused functions included from PPCGenCallingConv.inc.
2000CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00002001 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002002}
2003
Bill Schmidt230b4512013-06-12 16:39:22 +00002004bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
2005 CCValAssign::LocInfo &LocInfo,
2006 ISD::ArgFlagsTy &ArgFlags,
2007 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002008 return true;
2009}
2010
Bill Schmidt230b4512013-06-12 16:39:22 +00002011bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
2012 MVT &LocVT,
2013 CCValAssign::LocInfo &LocInfo,
2014 ISD::ArgFlagsTy &ArgFlags,
2015 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002016 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002017 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2018 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2019 };
2020 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002021
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002022 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
2023
2024 // Skip one register if the first unallocated register has an even register
2025 // number and there are still argument registers available which have not been
2026 // allocated yet. RegNum is actually an index into ArgRegs, which means we
2027 // need to skip a register if RegNum is odd.
2028 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
2029 State.AllocateReg(ArgRegs[RegNum]);
2030 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002031
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002032 // Always return false here, as this function only makes sure that the first
2033 // unallocated register has an odd register number and does not actually
2034 // allocate a register for the current argument.
2035 return false;
2036}
2037
Bill Schmidt230b4512013-06-12 16:39:22 +00002038bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
2039 MVT &LocVT,
2040 CCValAssign::LocInfo &LocInfo,
2041 ISD::ArgFlagsTy &ArgFlags,
2042 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002043 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002044 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2045 PPC::F8
2046 };
2047
2048 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002049
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002050 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
2051
2052 // If there is only one Floating-point register left we need to put both f64
2053 // values of a split ppc_fp128 value on the stack.
2054 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
2055 State.AllocateReg(ArgRegs[RegNum]);
2056 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002057
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002058 // Always return false here, as this function only makes sure that the two f64
2059 // values a ppc_fp128 value is split into are both passed in registers or both
2060 // passed on the stack and does not actually allocate a register for the
2061 // current argument.
2062 return false;
2063}
2064
Chris Lattner43df5b32007-02-25 05:34:32 +00002065/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002066/// on Darwin.
Craig Topper840beec2014-04-04 05:16:06 +00002067static const MCPhysReg *GetFPR() {
2068 static const MCPhysReg FPR[] = {
Chris Lattner43df5b32007-02-25 05:34:32 +00002069 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002070 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner43df5b32007-02-25 05:34:32 +00002071 };
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002072
Chris Lattner43df5b32007-02-25 05:34:32 +00002073 return FPR;
2074}
2075
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002076/// CalculateStackSlotSize - Calculates the size reserved for this argument on
2077/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002078static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002079 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00002080 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002081 if (Flags.isByVal())
2082 ArgSize = Flags.getByValSize();
2083 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2084
2085 return ArgSize;
2086}
2087
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002088SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002089PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002090 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002091 const SmallVectorImpl<ISD::InputArg>
2092 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002093 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002094 SmallVectorImpl<SDValue> &InVals)
2095 const {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002096 if (PPCSubTarget.isSVR4ABI()) {
2097 if (PPCSubTarget.isPPC64())
2098 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
2099 dl, DAG, InVals);
2100 else
2101 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2102 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002103 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002104 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2105 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002106 }
2107}
2108
2109SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002110PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002111 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002112 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002113 const SmallVectorImpl<ISD::InputArg>
2114 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002115 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002116 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002117
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002118 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002119 // +-----------------------------------+
2120 // +--> | Back chain |
2121 // | +-----------------------------------+
2122 // | | Floating-point register save area |
2123 // | +-----------------------------------+
2124 // | | General register save area |
2125 // | +-----------------------------------+
2126 // | | CR save word |
2127 // | +-----------------------------------+
2128 // | | VRSAVE save word |
2129 // | +-----------------------------------+
2130 // | | Alignment padding |
2131 // | +-----------------------------------+
2132 // | | Vector register save area |
2133 // | +-----------------------------------+
2134 // | | Local variable space |
2135 // | +-----------------------------------+
2136 // | | Parameter list area |
2137 // | +-----------------------------------+
2138 // | | LR save word |
2139 // | +-----------------------------------+
2140 // SP--> +--- | Back chain |
2141 // +-----------------------------------+
2142 //
2143 // Specifications:
2144 // System V Application Binary Interface PowerPC Processor Supplement
2145 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002146
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002147 MachineFunction &MF = DAG.getMachineFunction();
2148 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002149 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002150
Owen Anderson53aa7a92009-08-10 22:56:29 +00002151 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002152 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002153 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2154 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002155 unsigned PtrByteSize = 4;
2156
2157 // Assign locations to all of the incoming arguments.
2158 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002159 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002160 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002161
2162 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002163 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002164
Bill Schmidtef17c142013-02-06 17:33:58 +00002165 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002166
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002167 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2168 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002169
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002170 // Arguments stored in registers.
2171 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002172 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002173 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002174
Owen Anderson9f944592009-08-11 20:47:22 +00002175 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002176 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002177 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002178 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002179 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002180 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002181 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002182 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00002183 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002184 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002185 case MVT::f64:
Hal Finkel19be5062014-03-29 05:29:01 +00002186 if (PPCSubTarget.hasVSX())
2187 RC = &PPC::VSFRCRegClass;
2188 else
2189 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002190 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002191 case MVT::v16i8:
2192 case MVT::v8i16:
2193 case MVT::v4i32:
2194 case MVT::v4f32:
Hal Finkel7811c612014-03-28 19:58:11 +00002195 RC = &PPC::VRRCRegClass;
2196 break;
Hal Finkel27774d92014-03-13 07:58:58 +00002197 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002198 case MVT::v2i64:
Hal Finkel7811c612014-03-28 19:58:11 +00002199 RC = &PPC::VSHRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002200 break;
2201 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002202
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002203 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002204 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002205 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2206 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2207
2208 if (ValVT == MVT::i1)
2209 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002210
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002211 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002212 } else {
2213 // Argument stored in memory.
2214 assert(VA.isMemLoc());
2215
Hal Finkel940ab932014-02-28 00:27:01 +00002216 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002217 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002218 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002219
2220 // Create load nodes to retrieve arguments from the stack.
2221 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002222 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2223 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002224 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002225 }
2226 }
2227
2228 // Assign locations to all of the incoming aggregate by value arguments.
2229 // Aggregates passed by value are stored in the local variable space of the
2230 // caller's stack frame, right above the parameter list area.
2231 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002232 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002233 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002234
2235 // Reserve stack space for the allocations in CCInfo.
2236 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2237
Bill Schmidtef17c142013-02-06 17:33:58 +00002238 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002239
2240 // Area that is at least reserved in the caller of this function.
2241 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00002242
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002243 // Set the size that is at least reserved in caller of this function. Tail
2244 // call optimized function's reserved stack space needs to be aligned so that
2245 // taking the difference between two stack areas will result in an aligned
2246 // stack.
2247 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2248
2249 MinReservedArea =
2250 std::max(MinReservedArea,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002251 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peck527da1b2010-11-23 03:31:01 +00002252
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002253 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002254 getStackAlignment();
2255 unsigned AlignMask = TargetAlign-1;
2256 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peck527da1b2010-11-23 03:31:01 +00002257
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002258 FI->setMinReservedArea(MinReservedArea);
2259
2260 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002261
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002262 // If the function takes variable number of arguments, make a frame index for
2263 // the start of the first vararg value... for expansion of llvm.va_start.
2264 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +00002265 static const MCPhysReg GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002266 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2267 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2268 };
2269 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2270
Craig Topper840beec2014-04-04 05:16:06 +00002271 static const MCPhysReg FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002272 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2273 PPC::F8
2274 };
2275 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2276
Dan Gohman31ae5862010-04-17 14:41:14 +00002277 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2278 NumGPArgRegs));
2279 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2280 NumFPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002281
2282 // Make room for NumGPArgRegs and NumFPArgRegs.
2283 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson9f944592009-08-11 20:47:22 +00002284 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002285
Dan Gohman31ae5862010-04-17 14:41:14 +00002286 FuncInfo->setVarArgsStackOffset(
2287 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002288 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002289
Dan Gohman31ae5862010-04-17 14:41:14 +00002290 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2291 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002292
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002293 // The fixed integer arguments of a variadic function are stored to the
2294 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2295 // the result of va_next.
2296 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2297 // Get an existing live-in vreg, or add a new one.
2298 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2299 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002300 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002301
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002302 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002303 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2304 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002305 MemOps.push_back(Store);
2306 // Increment the address by four for the next argument to store
2307 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2308 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2309 }
2310
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002311 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2312 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002313 // The double arguments are stored to the VarArgsFrameIndex
2314 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002315 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2316 // Get an existing live-in vreg, or add a new one.
2317 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2318 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002319 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002320
Owen Anderson9f944592009-08-11 20:47:22 +00002321 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002322 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2323 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002324 MemOps.push_back(Store);
2325 // Increment the address by eight for the next argument to store
Owen Anderson9f944592009-08-11 20:47:22 +00002326 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002327 PtrVT);
2328 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2329 }
2330 }
2331
2332 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002333 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002334
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002335 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002336}
2337
Bill Schmidt57d6de52012-10-23 15:51:16 +00002338// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2339// value to MVT::i64 and then truncate to the correct register size.
2340SDValue
2341PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2342 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002343 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002344 if (Flags.isSExt())
2345 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2346 DAG.getValueType(ObjectVT));
2347 else if (Flags.isZExt())
2348 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2349 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002350
Hal Finkel940ab932014-02-28 00:27:01 +00002351 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002352}
2353
2354// Set the size that is at least reserved in caller of this function. Tail
2355// call optimized functions' reserved stack space needs to be aligned so that
2356// taking the difference between two stack areas will result in an aligned
2357// stack.
2358void
2359PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2360 unsigned nAltivecParamsAtEnd,
2361 unsigned MinReservedArea,
2362 bool isPPC64) const {
2363 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2364 // Add the Altivec parameters at the end, if needed.
2365 if (nAltivecParamsAtEnd) {
2366 MinReservedArea = ((MinReservedArea+15)/16)*16;
2367 MinReservedArea += 16*nAltivecParamsAtEnd;
2368 }
2369 MinReservedArea =
2370 std::max(MinReservedArea,
2371 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2372 unsigned TargetAlign
2373 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2374 getStackAlignment();
2375 unsigned AlignMask = TargetAlign-1;
2376 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2377 FI->setMinReservedArea(MinReservedArea);
2378}
2379
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002380SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002381PPCTargetLowering::LowerFormalArguments_64SVR4(
2382 SDValue Chain,
2383 CallingConv::ID CallConv, bool isVarArg,
2384 const SmallVectorImpl<ISD::InputArg>
2385 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002386 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002387 SmallVectorImpl<SDValue> &InVals) const {
2388 // TODO: add description of PPC stack frame format, or at least some docs.
2389 //
2390 MachineFunction &MF = DAG.getMachineFunction();
2391 MachineFrameInfo *MFI = MF.getFrameInfo();
2392 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2393
2394 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2395 // Potential tail calls could cause overwriting of argument stack slots.
2396 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2397 (CallConv == CallingConv::Fast));
2398 unsigned PtrByteSize = 8;
2399
2400 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2401 // Area that is at least reserved in caller of this function.
2402 unsigned MinReservedArea = ArgOffset;
2403
Craig Topper840beec2014-04-04 05:16:06 +00002404 static const MCPhysReg GPR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002405 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2406 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2407 };
2408
Craig Topper840beec2014-04-04 05:16:06 +00002409 static const MCPhysReg *FPR = GetFPR();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002410
Craig Topper840beec2014-04-04 05:16:06 +00002411 static const MCPhysReg VR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002412 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2413 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2414 };
Craig Topper840beec2014-04-04 05:16:06 +00002415 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00002416 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
2417 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
2418 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002419
2420 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2421 const unsigned Num_FPR_Regs = 13;
2422 const unsigned Num_VR_Regs = array_lengthof(VR);
2423
2424 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2425
2426 // Add DAG nodes to load the arguments or copy them out of registers. On
2427 // entry to a function on PPC, the arguments start after the linkage area,
2428 // although the first ones are often in registers.
2429
2430 SmallVector<SDValue, 8> MemOps;
2431 unsigned nAltivecParamsAtEnd = 0;
2432 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002433 unsigned CurArgIdx = 0;
2434 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002435 SDValue ArgVal;
2436 bool needsLoad = false;
2437 EVT ObjectVT = Ins[ArgNo].VT;
Hal Finkel940ab932014-02-28 00:27:01 +00002438 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002439 unsigned ArgSize = ObjSize;
2440 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt6631e942013-02-20 17:31:41 +00002441 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2442 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002443
2444 unsigned CurArgOffset = ArgOffset;
2445
2446 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2447 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
Hal Finkel27774d92014-03-13 07:58:58 +00002448 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8 ||
Hal Finkela6c8b512014-03-26 16:12:58 +00002449 ObjectVT==MVT::v2f64 || ObjectVT==MVT::v2i64) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002450 if (isVarArg) {
2451 MinReservedArea = ((MinReservedArea+15)/16)*16;
2452 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2453 Flags,
2454 PtrByteSize);
2455 } else
2456 nAltivecParamsAtEnd++;
2457 } else
2458 // Calculate min reserved area.
2459 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2460 Flags,
2461 PtrByteSize);
2462
2463 // FIXME the codegen can be much improved in some cases.
2464 // We do not have to keep everything in memory.
2465 if (Flags.isByVal()) {
2466 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2467 ObjSize = Flags.getByValSize();
2468 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002469 // Empty aggregate parameters do not take up registers. Examples:
2470 // struct { } a;
2471 // union { } b;
2472 // int c[0];
2473 // etc. However, we have to provide a place-holder in InVals, so
2474 // pretend we have an 8-byte item at the current address for that
2475 // purpose.
2476 if (!ObjSize) {
2477 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2478 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2479 InVals.push_back(FIN);
2480 continue;
2481 }
Hal Finkel262a2242013-09-12 23:20:06 +00002482
2483 unsigned BVAlign = Flags.getByValAlign();
2484 if (BVAlign > 8) {
2485 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
2486 CurArgOffset = ArgOffset;
2487 }
2488
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002489 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt48081ca2012-10-16 13:30:53 +00002490 if (ObjSize < PtrByteSize)
2491 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002492 // The value of the object is its address.
2493 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2494 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2495 InVals.push_back(FIN);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002496
2497 if (ObjSize < 8) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002498 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002499 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002500 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002501 SDValue Store;
2502
2503 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2504 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2505 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2506 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002507 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002508 ObjType, false, false, 0);
2509 } else {
2510 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2511 // store the whole register as-is to the parameter save area
2512 // slot. The address of the parameter was already calculated
2513 // above (InVals.push_back(FIN)) to be the right-justified
2514 // offset within the slot. For this store, we need a new
2515 // frame index that points at the beginning of the slot.
2516 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2517 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2518 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002519 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002520 false, false, 0);
2521 }
2522
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002523 MemOps.push_back(Store);
2524 ++GPR_idx;
2525 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002526 // Whether we copied from a register or not, advance the offset
2527 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002528 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002529 continue;
2530 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002531
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002532 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2533 // Store whatever pieces of the object are in registers
2534 // to memory. ArgOffset will be the address of the beginning
2535 // of the object.
2536 if (GPR_idx != Num_GPR_Regs) {
2537 unsigned VReg;
2538 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2539 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2540 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2541 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002542 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002543 MachinePointerInfo(FuncArg, j),
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002544 false, false, 0);
2545 MemOps.push_back(Store);
2546 ++GPR_idx;
2547 ArgOffset += PtrByteSize;
2548 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00002549 ArgOffset += ArgSize - j;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002550 break;
2551 }
2552 }
2553 continue;
2554 }
2555
2556 switch (ObjectVT.getSimpleVT().SimpleTy) {
2557 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00002558 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002559 case MVT::i32:
2560 case MVT::i64:
2561 if (GPR_idx != Num_GPR_Regs) {
2562 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2563 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2564
Hal Finkel940ab932014-02-28 00:27:01 +00002565 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002566 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2567 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002568 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002569
2570 ++GPR_idx;
2571 } else {
2572 needsLoad = true;
2573 ArgSize = PtrByteSize;
2574 }
2575 ArgOffset += 8;
2576 break;
2577
2578 case MVT::f32:
2579 case MVT::f64:
2580 // Every 8 bytes of argument space consumes one of the GPRs available for
2581 // argument passing.
2582 if (GPR_idx != Num_GPR_Regs) {
2583 ++GPR_idx;
2584 }
2585 if (FPR_idx != Num_FPR_Regs) {
2586 unsigned VReg;
2587
2588 if (ObjectVT == MVT::f32)
2589 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2590 else
Hal Finkel19be5062014-03-29 05:29:01 +00002591 VReg = MF.addLiveIn(FPR[FPR_idx], PPCSubTarget.hasVSX() ?
2592 &PPC::VSFRCRegClass :
2593 &PPC::F8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002594
2595 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2596 ++FPR_idx;
2597 } else {
2598 needsLoad = true;
Bill Schmidt22162472012-10-11 15:38:20 +00002599 ArgSize = PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002600 }
2601
2602 ArgOffset += 8;
2603 break;
2604 case MVT::v4f32:
2605 case MVT::v4i32:
2606 case MVT::v8i16:
2607 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00002608 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002609 case MVT::v2i64:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002610 // Note that vector arguments in registers don't reserve stack space,
2611 // except in varargs functions.
2612 if (VR_idx != Num_VR_Regs) {
Hal Finkel7811c612014-03-28 19:58:11 +00002613 unsigned VReg = (ObjectVT == MVT::v2f64 || ObjectVT == MVT::v2i64) ?
2614 MF.addLiveIn(VSRH[VR_idx], &PPC::VSHRCRegClass) :
2615 MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002616 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2617 if (isVarArg) {
2618 while ((ArgOffset % 16) != 0) {
2619 ArgOffset += PtrByteSize;
2620 if (GPR_idx != Num_GPR_Regs)
2621 GPR_idx++;
2622 }
2623 ArgOffset += 16;
2624 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2625 }
2626 ++VR_idx;
2627 } else {
2628 // Vectors are aligned.
2629 ArgOffset = ((ArgOffset+15)/16)*16;
2630 CurArgOffset = ArgOffset;
2631 ArgOffset += 16;
2632 needsLoad = true;
2633 }
2634 break;
2635 }
2636
2637 // We need to load the argument to a virtual register if we determined
2638 // above that we ran out of physical registers of the appropriate type.
2639 if (needsLoad) {
2640 int FI = MFI->CreateFixedObject(ObjSize,
2641 CurArgOffset + (ArgSize - ObjSize),
2642 isImmutable);
2643 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2644 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2645 false, false, false, 0);
2646 }
2647
2648 InVals.push_back(ArgVal);
2649 }
2650
2651 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002652 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002653 // taking the difference between two stack areas will result in an aligned
2654 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002655 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002656
2657 // If the function takes variable number of arguments, make a frame index for
2658 // the start of the first vararg value... for expansion of llvm.va_start.
2659 if (isVarArg) {
2660 int Depth = ArgOffset;
2661
2662 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00002663 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002664 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2665
2666 // If this function is vararg, store any remaining integer argument regs
2667 // to their spots on the stack so that they may be loaded by deferencing the
2668 // result of va_next.
2669 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2670 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2671 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2672 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2673 MachinePointerInfo(), false, false, 0);
2674 MemOps.push_back(Store);
2675 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00002676 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002677 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2678 }
2679 }
2680
2681 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002682 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002683
2684 return Chain;
2685}
2686
2687SDValue
2688PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002689 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002690 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002691 const SmallVectorImpl<ISD::InputArg>
2692 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002693 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002694 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002695 // TODO: add description of PPC stack frame format, or at least some docs.
2696 //
2697 MachineFunction &MF = DAG.getMachineFunction();
2698 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002699 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002700
Owen Anderson53aa7a92009-08-10 22:56:29 +00002701 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002702 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002703 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002704 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2705 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00002706 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey48850c12006-11-16 22:43:37 +00002707
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002708 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002709 // Area that is at least reserved in caller of this function.
2710 unsigned MinReservedArea = ArgOffset;
2711
Craig Topper840beec2014-04-04 05:16:06 +00002712 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002713 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2714 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2715 };
Craig Topper840beec2014-04-04 05:16:06 +00002716 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00002717 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2718 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2719 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00002720
Craig Topper840beec2014-04-04 05:16:06 +00002721 static const MCPhysReg *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002722
Craig Topper840beec2014-04-04 05:16:06 +00002723 static const MCPhysReg VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002724 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2725 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2726 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00002727
Owen Andersone2f23a32007-09-07 04:06:50 +00002728 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002729 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00002730 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00002731
2732 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002733
Craig Topper840beec2014-04-04 05:16:06 +00002734 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002735
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002736 // In 32-bit non-varargs functions, the stack space for vectors is after the
2737 // stack space for non-vectors. We do not use this space unless we have
2738 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00002739 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002740 // that out...for the pathological case, compute VecArgOffset as the
2741 // start of the vector parameter area. Computing VecArgOffset is the
2742 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002743 unsigned VecArgOffset = ArgOffset;
2744 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002745 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002746 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002747 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002748 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002749
Duncan Sandsd97eea32008-03-21 09:14:45 +00002750 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002751 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00002752 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002753 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002754 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2755 VecArgOffset += ArgSize;
2756 continue;
2757 }
2758
Owen Anderson9f944592009-08-11 20:47:22 +00002759 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002760 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002761 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002762 case MVT::i32:
2763 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002764 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002765 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002766 case MVT::i64: // PPC64
2767 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002768 // FIXME: We are guaranteed to be !isPPC64 at this point.
2769 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002770 VecArgOffset += 8;
2771 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002772 case MVT::v4f32:
2773 case MVT::v4i32:
2774 case MVT::v8i16:
2775 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002776 // Nothing to do, we're only looking at Nonvector args here.
2777 break;
2778 }
2779 }
2780 }
2781 // We've found where the vector parameter area in memory is. Skip the
2782 // first 12 parameters; these don't use that memory.
2783 VecArgOffset = ((VecArgOffset+15)/16)*16;
2784 VecArgOffset += 12*16;
2785
Chris Lattner4302e8f2006-05-16 18:18:50 +00002786 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00002787 // entry to a function on PPC, the arguments start after the linkage area,
2788 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00002789
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002790 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002791 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00002792 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002793 unsigned CurArgIdx = 0;
2794 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002795 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002796 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002797 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00002798 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00002799 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002800 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002801 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2802 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002803
Chris Lattner318f0d22006-05-16 18:51:52 +00002804 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002805
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002806 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002807 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2808 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002809 if (isVarArg || isPPC64) {
2810 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002811 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002812 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002813 PtrByteSize);
2814 } else nAltivecParamsAtEnd++;
2815 } else
2816 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002817 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002818 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002819 PtrByteSize);
2820
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002821 // FIXME the codegen can be much improved in some cases.
2822 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002823 if (Flags.isByVal()) {
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002824 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002825 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002826 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002827 // Objects of size 1 and 2 are right justified, everything else is
2828 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00002829 if (ObjSize==1 || ObjSize==2) {
2830 CurArgOffset = CurArgOffset + (4 - ObjSize);
2831 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002832 // The value of the object is its address.
Evan Cheng0664a672010-07-03 00:40:23 +00002833 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002834 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002835 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002836 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00002837 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002838 unsigned VReg;
2839 if (isPPC64)
2840 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2841 else
2842 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002843 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002844 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002845 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002846 MachinePointerInfo(FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002847 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00002848 MemOps.push_back(Store);
2849 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00002850 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002851
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002852 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00002853
Dale Johannesen21a8f142008-03-08 01:41:42 +00002854 continue;
2855 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002856 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2857 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002858 // to memory. ArgOffset will be the address of the beginning
2859 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002860 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002861 unsigned VReg;
2862 if (isPPC64)
2863 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2864 else
2865 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00002866 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002867 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002868 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002869 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002870 MachinePointerInfo(FuncArg, j),
David Greene87a5abe2010-02-15 16:56:53 +00002871 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002872 MemOps.push_back(Store);
2873 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002874 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002875 } else {
2876 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2877 break;
2878 }
2879 }
2880 continue;
2881 }
2882
Owen Anderson9f944592009-08-11 20:47:22 +00002883 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002884 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002885 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002886 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00002887 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00002888 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002889 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002890 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Hal Finkel7f908e82014-03-06 00:45:19 +00002891
2892 if (ObjectVT == MVT::i1)
2893 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal);
2894
Bill Wendling968f32c2008-03-07 20:49:02 +00002895 ++GPR_idx;
2896 } else {
2897 needsLoad = true;
2898 ArgSize = PtrByteSize;
2899 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002900 // All int arguments reserve stack space in the Darwin ABI.
2901 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00002902 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002903 }
Bill Wendling968f32c2008-03-07 20:49:02 +00002904 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00002905 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00002906 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002907 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002908 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00002909
Hal Finkel940ab932014-02-28 00:27:01 +00002910 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00002911 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00002912 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002913 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00002914
Chris Lattnerec78cad2006-06-26 22:48:35 +00002915 ++GPR_idx;
2916 } else {
2917 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00002918 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002919 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002920 // All int arguments reserve stack space in the Darwin ABI.
2921 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002922 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002923
Owen Anderson9f944592009-08-11 20:47:22 +00002924 case MVT::f32:
2925 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00002926 // Every 4 bytes of argument space consumes one of the GPRs available for
2927 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002928 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002929 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00002930 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002931 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00002932 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002933 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002934 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002935
Owen Anderson9f944592009-08-11 20:47:22 +00002936 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00002937 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002938 else
Devang Patelf3292b22011-02-21 23:21:26 +00002939 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002940
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002941 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002942 ++FPR_idx;
2943 } else {
2944 needsLoad = true;
2945 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002946
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002947 // All FP arguments reserve stack space in the Darwin ABI.
2948 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002949 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002950 case MVT::v4f32:
2951 case MVT::v4i32:
2952 case MVT::v8i16:
2953 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00002954 // Note that vector arguments in registers don't reserve stack space,
2955 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002956 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002957 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002958 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00002959 if (isVarArg) {
2960 while ((ArgOffset % 16) != 0) {
2961 ArgOffset += PtrByteSize;
2962 if (GPR_idx != Num_GPR_Regs)
2963 GPR_idx++;
2964 }
2965 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002966 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00002967 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002968 ++VR_idx;
2969 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002970 if (!isVarArg && !isPPC64) {
2971 // Vectors go after all the nonvectors.
2972 CurArgOffset = VecArgOffset;
2973 VecArgOffset += 16;
2974 } else {
2975 // Vectors are aligned.
2976 ArgOffset = ((ArgOffset+15)/16)*16;
2977 CurArgOffset = ArgOffset;
2978 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00002979 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002980 needsLoad = true;
2981 }
2982 break;
2983 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002984
Chris Lattner4302e8f2006-05-16 18:18:50 +00002985 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002986 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002987 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002988 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002989 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00002990 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002991 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002992 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002993 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002994 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002995
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002996 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002997 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002998
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002999 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003000 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003001 // taking the difference between two stack areas will result in an aligned
3002 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003003 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003004
Chris Lattner4302e8f2006-05-16 18:18:50 +00003005 // If the function takes variable number of arguments, make a frame index for
3006 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003007 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003008 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003009
Dan Gohman31ae5862010-04-17 14:41:14 +00003010 FuncInfo->setVarArgsFrameIndex(
3011 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00003012 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00003013 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003014
Chris Lattner4302e8f2006-05-16 18:18:50 +00003015 // If this function is vararg, store any remaining integer argument regs
3016 // to their spots on the stack so that they may be loaded by deferencing the
3017 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003018 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00003019 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00003020
Chris Lattner2cca3852006-11-18 01:57:19 +00003021 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00003022 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003023 else
Devang Patelf3292b22011-02-21 23:21:26 +00003024 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003025
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003026 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00003027 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3028 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003029 MemOps.push_back(Store);
3030 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003031 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00003032 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003033 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003034 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003035
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003036 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003037 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003038
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003039 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003040}
3041
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003042/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
3043/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003044static unsigned
3045CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
3046 bool isPPC64,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003047 bool isVarArg,
3048 unsigned CC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003049 const SmallVectorImpl<ISD::OutputArg>
3050 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003051 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003052 unsigned &nAltivecParamsAtEnd) {
3053 // Count how many bytes are to be pushed on the stack, including the linkage
3054 // area, and parameter passing area. We start with 24/48 bytes, which is
3055 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003056 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003057 unsigned NumOps = Outs.size();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003058 unsigned PtrByteSize = isPPC64 ? 8 : 4;
3059
3060 // Add up all the space actually used.
3061 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
3062 // they all go in registers, but we must reserve stack space for them for
3063 // possible use by the caller. In varargs or 64-bit calls, parameters are
3064 // assigned stack space in order, with padding so Altivec parameters are
3065 // 16-byte aligned.
3066 nAltivecParamsAtEnd = 0;
3067 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003068 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003069 EVT ArgVT = Outs[i].VT;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003070 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00003071 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
Hal Finkel27774d92014-03-13 07:58:58 +00003072 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8 ||
Hal Finkela6c8b512014-03-26 16:12:58 +00003073 ArgVT==MVT::v2f64 || ArgVT==MVT::v2i64) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003074 if (!isVarArg && !isPPC64) {
3075 // Non-varargs Altivec parameters go after all the non-Altivec
3076 // parameters; handle those later so we know how much padding we need.
3077 nAltivecParamsAtEnd++;
3078 continue;
3079 }
3080 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
3081 NumBytes = ((NumBytes+15)/16)*16;
3082 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003083 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003084 }
3085
3086 // Allow for Altivec parameters at the end, if needed.
3087 if (nAltivecParamsAtEnd) {
3088 NumBytes = ((NumBytes+15)/16)*16;
3089 NumBytes += 16*nAltivecParamsAtEnd;
3090 }
3091
3092 // The prolog code of the callee may store up to 8 GPR argument registers to
3093 // the stack, allowing va_start to index over them in memory if its varargs.
3094 // Because we cannot tell if this is needed on the caller side, we have to
3095 // conservatively assume that it is needed. As such, make sure we have at
3096 // least enough stack space for the caller to store the 8 GPRs.
3097 NumBytes = std::max(NumBytes,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003098 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003099
3100 // Tail call needs the stack to be aligned.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003101 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
3102 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
3103 getFrameLowering()->getStackAlignment();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003104 unsigned AlignMask = TargetAlign-1;
3105 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
3106 }
3107
3108 return NumBytes;
3109}
3110
3111/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003112/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00003113static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003114 unsigned ParamSize) {
3115
Dale Johannesen86dcae12009-11-24 01:09:07 +00003116 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003117
3118 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
3119 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3120 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3121 // Remember only if the new adjustement is bigger.
3122 if (SPDiff < FI->getTailCallSPDelta())
3123 FI->setTailCallSPDelta(SPDiff);
3124
3125 return SPDiff;
3126}
3127
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003128/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3129/// for tail call optimization. Targets which want to do tail call
3130/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003131bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003132PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003133 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003134 bool isVarArg,
3135 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003136 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003137 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00003138 return false;
3139
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003140 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003141 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003142 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003143
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003144 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00003145 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003146 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
3147 // Functions containing by val parameters are not supported.
3148 for (unsigned i = 0; i != Ins.size(); i++) {
3149 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3150 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003151 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003152
Alp Tokerf907b892013-12-05 05:44:44 +00003153 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003154 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
3155 return true;
3156
3157 // At the moment we can only do local tail calls (in same module, hidden
3158 // or protected) if we are generating PIC.
3159 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
3160 return G->getGlobal()->hasHiddenVisibility()
3161 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003162 }
3163
3164 return false;
3165}
3166
Chris Lattnereb755fc2006-05-17 19:00:46 +00003167/// isCallCompatibleAddress - Return the immediate to use if the specified
3168/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003169static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00003170 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003171 if (!C) return nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003172
Dan Gohmaneffb8942008-09-12 16:56:44 +00003173 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003174 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00003175 SignExtend32<26>(Addr) != Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +00003176 return nullptr; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003177
Dan Gohmaneffb8942008-09-12 16:56:44 +00003178 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00003179 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003180}
3181
Dan Gohmand78c4002008-05-13 00:00:25 +00003182namespace {
3183
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003184struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003185 SDValue Arg;
3186 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003187 int FrameIdx;
3188
3189 TailCallArgumentInfo() : FrameIdx(0) {}
3190};
3191
Dan Gohmand78c4002008-05-13 00:00:25 +00003192}
3193
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003194/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3195static void
3196StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003197 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003198 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3199 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003200 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003201 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003202 SDValue Arg = TailCallArgs[i].Arg;
3203 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003204 int FI = TailCallArgs[i].FrameIdx;
3205 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003206 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003207 MachinePointerInfo::getFixedStack(FI),
3208 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003209 }
3210}
3211
3212/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3213/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003214static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003215 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003216 SDValue Chain,
3217 SDValue OldRetAddr,
3218 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003219 int SPDiff,
3220 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003221 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003222 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003223 if (SPDiff) {
3224 // Calculate the new stack slot for the return address.
3225 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003226 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003227 isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003228 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003229 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003230 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003231 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003232 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003233 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003234 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003235
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003236 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3237 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003238 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003239 int NewFPLoc =
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003240 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene1fbe0542009-11-12 20:49:22 +00003241 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003242 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003243 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3244 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003245 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003246 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003247 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003248 }
3249 return Chain;
3250}
3251
3252/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3253/// the position of the argument.
3254static void
3255CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003256 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003257 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003258 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003259 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003260 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003261 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003262 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003263 TailCallArgumentInfo Info;
3264 Info.Arg = Arg;
3265 Info.FrameIdxOp = FIN;
3266 Info.FrameIdx = FI;
3267 TailCallArguments.push_back(Info);
3268}
3269
3270/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3271/// stack slot. Returns the chain as result and the loaded frame pointers in
3272/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003273SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003274 int SPDiff,
3275 SDValue Chain,
3276 SDValue &LROpOut,
3277 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003278 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003279 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003280 if (SPDiff) {
3281 // Load the LR and FP stack slot for later adjusting.
Owen Anderson9f944592009-08-11 20:47:22 +00003282 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003283 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003284 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003285 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003286 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003287
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003288 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3289 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003290 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003291 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003292 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003293 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003294 Chain = SDValue(FPOpOut.getNode(), 1);
3295 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003296 }
3297 return Chain;
3298}
3299
Dale Johannesen85d41a12008-03-04 23:17:14 +00003300/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003301/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003302/// specified by the specific parameter attribute. The copy will be passed as
3303/// a byval function parameter.
3304/// Sometimes what we are copying is the end of a larger object, the part that
3305/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003306static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003307CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003308 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003309 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003310 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003311 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003312 false, false, MachinePointerInfo(),
3313 MachinePointerInfo());
Dale Johannesen85d41a12008-03-04 23:17:14 +00003314}
Chris Lattner43df5b32007-02-25 05:34:32 +00003315
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003316/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3317/// tail calls.
3318static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003319LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3320 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003321 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003322 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3323 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003324 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003325 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003326 if (!isTailCall) {
3327 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003328 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003329 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003330 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003331 else
Owen Anderson9f944592009-08-11 20:47:22 +00003332 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003333 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003334 DAG.getConstant(ArgOffset, PtrVT));
3335 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003336 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3337 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003338 // Calculate and remember argument location.
3339 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3340 TailCallArguments);
3341}
3342
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003343static
3344void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003345 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003346 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003347 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003348 MachineFunction &MF = DAG.getMachineFunction();
3349
3350 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3351 // might overwrite each other in case of tail call optimization.
3352 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003353 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003354 InFlag = SDValue();
3355 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3356 MemOpChains2, dl);
3357 if (!MemOpChains2.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003358 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003359
3360 // Store the return address to the appropriate stack slot.
3361 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3362 isPPC64, isDarwinABI, dl);
3363
3364 // Emit callseq_end just before tailcall node.
3365 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003366 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003367 InFlag = Chain.getValue(1);
3368}
3369
3370static
3371unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003372 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003373 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3374 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003375 const PPCSubtarget &PPCSubTarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003376
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003377 bool isPPC64 = PPCSubTarget.isPPC64();
3378 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3379
Owen Anderson53aa7a92009-08-10 22:56:29 +00003380 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003381 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003382 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003383
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003384 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003385
Torok Edwin31e90d22010-08-04 20:47:44 +00003386 bool needIndirectCall = true;
3387 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003388 // If this is an absolute destination address, use the munged value.
3389 Callee = SDValue(Dest, 0);
Torok Edwin31e90d22010-08-04 20:47:44 +00003390 needIndirectCall = false;
3391 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003392
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003393 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3394 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3395 // Use indirect calls for ALL functions calls in JIT mode, since the
3396 // far-call stubs may be outside relocation limits for a BL instruction.
3397 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3398 unsigned OpFlags = 0;
3399 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003400 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003401 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003402 (G->getGlobal()->isDeclaration() ||
3403 G->getGlobal()->isWeakForLinker())) {
3404 // PC-relative references to external symbols should go through $stub,
3405 // unless we're building with the leopard linker or later, which
3406 // automatically synthesizes these stubs.
3407 OpFlags = PPCII::MO_DARWIN_STUB;
3408 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003409
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003410 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3411 // every direct call is) turn it into a TargetGlobalAddress /
3412 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin31e90d22010-08-04 20:47:44 +00003413 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003414 Callee.getValueType(),
3415 0, OpFlags);
Torok Edwin31e90d22010-08-04 20:47:44 +00003416 needIndirectCall = false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003417 }
Torok Edwin31e90d22010-08-04 20:47:44 +00003418 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003419
Torok Edwin31e90d22010-08-04 20:47:44 +00003420 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003421 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003422
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003423 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003424 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003425 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003426 // PC-relative references to external symbols should go through $stub,
3427 // unless we're building with the leopard linker or later, which
3428 // automatically synthesizes these stubs.
3429 OpFlags = PPCII::MO_DARWIN_STUB;
3430 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003431
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003432 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3433 OpFlags);
3434 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003435 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003436
Torok Edwin31e90d22010-08-04 20:47:44 +00003437 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003438 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3439 // to do the call, we can't use PPCISD::CALL.
3440 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003441
3442 if (isSVR4ABI && isPPC64) {
3443 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3444 // entry point, but to the function descriptor (the function entry point
3445 // address is part of the function descriptor though).
3446 // The function descriptor is a three doubleword structure with the
3447 // following fields: function entry point, TOC base address and
3448 // environment pointer.
3449 // Thus for a call through a function pointer, the following actions need
3450 // to be performed:
3451 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003452 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003453 // 2. Load the address of the function entry point from the function
3454 // descriptor.
3455 // 3. Load the TOC of the callee from the function descriptor into r2.
3456 // 4. Load the environment pointer from the function descriptor into
3457 // r11.
3458 // 5. Branch to the function entry point address.
3459 // 6. On return of the callee, the TOC of the caller needs to be
3460 // restored (this is done in FinishCall()).
3461 //
3462 // All those operations are flagged together to ensure that no other
3463 // operations can be scheduled in between. E.g. without flagging the
3464 // operations together, a TOC access in the caller could be scheduled
3465 // between the load of the callee TOC and the branch to the callee, which
3466 // results in the TOC access going through the TOC of the callee instead
3467 // of going through the TOC of the caller, which leads to incorrect code.
3468
3469 // Load the address of the function entry point from the function
3470 // descriptor.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003471 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00003472 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00003473 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
Tilmann Scheller79fef932009-12-18 13:00:15 +00003474 Chain = LoadFuncPtr.getValue(1);
3475 InFlag = LoadFuncPtr.getValue(2);
3476
3477 // Load environment pointer into r11.
3478 // Offset of the environment pointer within the function descriptor.
3479 SDValue PtrOff = DAG.getIntPtrConstant(16);
3480
3481 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3482 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3483 InFlag);
3484 Chain = LoadEnvPtr.getValue(1);
3485 InFlag = LoadEnvPtr.getValue(2);
3486
3487 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3488 InFlag);
3489 Chain = EnvVal.getValue(0);
3490 InFlag = EnvVal.getValue(1);
3491
3492 // Load TOC of the callee into r2. We are using a target-specific load
3493 // with r2 hard coded, because the result of a target-independent load
3494 // would never go directly into r2, since r2 is a reserved register (which
3495 // prevents the register allocator from allocating it), resulting in an
3496 // additional register being allocated and an unnecessary move instruction
3497 // being generated.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003498 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003499 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3500 Callee, InFlag);
3501 Chain = LoadTOCPtr.getValue(0);
3502 InFlag = LoadTOCPtr.getValue(1);
3503
3504 MTCTROps[0] = Chain;
3505 MTCTROps[1] = LoadFuncPtr;
3506 MTCTROps[2] = InFlag;
3507 }
3508
Craig Topper48d114b2014-04-26 18:35:24 +00003509 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00003510 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003511 InFlag = Chain.getValue(1);
3512
3513 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00003514 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003515 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003516 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003517 CallOpc = PPCISD::BCTRL;
Craig Topper062a2ba2014-04-25 05:30:21 +00003518 Callee.setNode(nullptr);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003519 // Add use of X11 (holding environment pointer)
3520 if (isSVR4ABI && isPPC64)
3521 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003522 // Add CTR register as callee so a bctr can be emitted later.
3523 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00003524 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003525 }
3526
3527 // If this is a direct call, pass the chain and the callee.
3528 if (Callee.getNode()) {
3529 Ops.push_back(Chain);
3530 Ops.push_back(Callee);
3531 }
3532 // If this is a tail call add stack pointer delta.
3533 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00003534 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003535
3536 // Add argument registers to the end of the list so that they are known live
3537 // into the call.
3538 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3539 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3540 RegsToPass[i].second.getValueType()));
3541
3542 return CallOpc;
3543}
3544
Roman Divacky76293062012-09-18 16:47:58 +00003545static
3546bool isLocalCall(const SDValue &Callee)
3547{
3548 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00003549 return !G->getGlobal()->isDeclaration() &&
3550 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00003551 return false;
3552}
3553
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003554SDValue
3555PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003556 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003557 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003558 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003559 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003560
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003561 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003562 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003563 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003564 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003565
3566 // Copy all of the result registers out of their specified physreg.
3567 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3568 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003569 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00003570
3571 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3572 VA.getLocReg(), VA.getLocVT(), InFlag);
3573 Chain = Val.getValue(1);
3574 InFlag = Val.getValue(2);
3575
3576 switch (VA.getLocInfo()) {
3577 default: llvm_unreachable("Unknown loc info!");
3578 case CCValAssign::Full: break;
3579 case CCValAssign::AExt:
3580 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3581 break;
3582 case CCValAssign::ZExt:
3583 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3584 DAG.getValueType(VA.getValVT()));
3585 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3586 break;
3587 case CCValAssign::SExt:
3588 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3589 DAG.getValueType(VA.getValVT()));
3590 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3591 break;
3592 }
3593
3594 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003595 }
3596
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003597 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003598}
3599
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003600SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00003601PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003602 bool isTailCall, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003603 SelectionDAG &DAG,
3604 SmallVector<std::pair<unsigned, SDValue>, 8>
3605 &RegsToPass,
3606 SDValue InFlag, SDValue Chain,
3607 SDValue &Callee,
3608 int SPDiff, unsigned NumBytes,
3609 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003610 SmallVectorImpl<SDValue> &InVals) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003611 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003612 SmallVector<SDValue, 8> Ops;
3613 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3614 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003615 PPCSubTarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003616
Hal Finkel5ab37802012-08-28 02:10:27 +00003617 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3618 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3619 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3620
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003621 // When performing tail call optimization the callee pops its arguments off
3622 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00003623 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003624 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003625 (CallConv == CallingConv::Fast &&
3626 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003627
Roman Divackyef21be22012-03-06 16:41:49 +00003628 // Add a register mask operand representing the call-preserved registers.
3629 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3630 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3631 assert(Mask && "Missing call preserved mask for calling convention");
3632 Ops.push_back(DAG.getRegisterMask(Mask));
3633
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003634 if (InFlag.getNode())
3635 Ops.push_back(InFlag);
3636
3637 // Emit tail call.
3638 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003639 assert(((Callee.getOpcode() == ISD::Register &&
3640 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3641 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3642 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3643 isa<ConstantSDNode>(Callee)) &&
3644 "Expecting an global address, external symbol, absolute value or register");
3645
Craig Topper48d114b2014-04-26 18:35:24 +00003646 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003647 }
3648
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003649 // Add a NOP immediately after the branch instruction when using the 64-bit
3650 // SVR4 ABI. At link time, if caller and callee are in a different module and
3651 // thus have a different TOC, the call will be replaced with a call to a stub
3652 // function which saves the current TOC, loads the TOC of the callee and
3653 // branches to the callee. The NOP will be replaced with a load instruction
3654 // which restores the TOC of the caller from the TOC save slot of the current
3655 // stack frame. If caller and callee belong to the same module (and have the
3656 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00003657
3658 bool needsTOCRestore = false;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003659 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003660 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003661 // This is a call through a function pointer.
3662 // Restore the caller TOC from the save area into R2.
3663 // See PrepareCall() for more information about calls through function
3664 // pointers in the 64-bit SVR4 ABI.
3665 // We are using a target-specific load with r2 hard coded, because the
3666 // result of a target-independent load would never go directly into r2,
3667 // since r2 is a reserved register (which prevents the register allocator
3668 // from allocating it), resulting in an additional register being
3669 // allocated and an unnecessary move instruction being generated.
Hal Finkel51861b42012-03-31 14:45:15 +00003670 needsTOCRestore = true;
Bill Schmidtcea15962013-09-26 17:09:28 +00003671 } else if ((CallOpc == PPCISD::CALL) &&
3672 (!isLocalCall(Callee) ||
3673 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Roman Divacky76293062012-09-18 16:47:58 +00003674 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003675 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller79fef932009-12-18 13:00:15 +00003676 }
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003677 }
3678
Craig Topper48d114b2014-04-26 18:35:24 +00003679 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Hal Finkel51861b42012-03-31 14:45:15 +00003680 InFlag = Chain.getValue(1);
3681
3682 if (needsTOCRestore) {
3683 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3684 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3685 InFlag = Chain.getValue(1);
3686 }
3687
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003688 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3689 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003690 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003691 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003692 InFlag = Chain.getValue(1);
3693
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003694 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3695 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003696}
3697
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003698SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00003699PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003700 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00003701 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00003702 SDLoc &dl = CLI.DL;
3703 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3704 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3705 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00003706 SDValue Chain = CLI.Chain;
3707 SDValue Callee = CLI.Callee;
3708 bool &isTailCall = CLI.IsTailCall;
3709 CallingConv::ID CallConv = CLI.CallConv;
3710 bool isVarArg = CLI.IsVarArg;
3711
Evan Cheng67a69dd2010-01-27 00:07:07 +00003712 if (isTailCall)
3713 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3714 Ins, DAG);
3715
Reid Kleckner5772b772014-04-24 20:14:34 +00003716 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
3717 report_fatal_error("failed to perform tail call elimination on a call "
3718 "site marked musttail");
3719
Bill Schmidt57d6de52012-10-23 15:51:16 +00003720 if (PPCSubTarget.isSVR4ABI()) {
3721 if (PPCSubTarget.isPPC64())
3722 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3723 isTailCall, Outs, OutVals, Ins,
3724 dl, DAG, InVals);
3725 else
3726 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3727 isTailCall, Outs, OutVals, Ins,
3728 dl, DAG, InVals);
3729 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003730
Bill Schmidt57d6de52012-10-23 15:51:16 +00003731 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3732 isTailCall, Outs, OutVals, Ins,
3733 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003734}
3735
3736SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003737PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3738 CallingConv::ID CallConv, bool isVarArg,
3739 bool isTailCall,
3740 const SmallVectorImpl<ISD::OutputArg> &Outs,
3741 const SmallVectorImpl<SDValue> &OutVals,
3742 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003743 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003744 SmallVectorImpl<SDValue> &InVals) const {
3745 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003746 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003747
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003748 assert((CallConv == CallingConv::C ||
3749 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003750
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003751 unsigned PtrByteSize = 4;
3752
3753 MachineFunction &MF = DAG.getMachineFunction();
3754
3755 // Mark this function as potentially containing a function that contains a
3756 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3757 // and restoring the callers stack pointer in this functions epilog. This is
3758 // done because by tail calling the called function might overwrite the value
3759 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003760 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3761 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003762 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00003763
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003764 // Count how many bytes are to be pushed on the stack, including the linkage
3765 // area, parameter list area and the part of the local variable space which
3766 // contains copies of aggregates which are passed by value.
3767
3768 // Assign locations to all of the outgoing arguments.
3769 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003770 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003771 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003772
3773 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003774 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003775
3776 if (isVarArg) {
3777 // Handle fixed and variable vector arguments differently.
3778 // Fixed vector arguments go into registers as long as registers are
3779 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003780 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00003781
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003782 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003783 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003784 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003785 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00003786
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003787 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00003788 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3789 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003790 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00003791 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3792 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003793 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003794
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003795 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003796#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00003797 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00003798 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003799#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003800 llvm_unreachable(nullptr);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003801 }
3802 }
3803 } else {
3804 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00003805 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003806 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003807
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003808 // Assign locations to all of the outgoing aggregate by value arguments.
3809 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003810 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003811 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003812
3813 // Reserve stack space for the allocations in CCInfo.
3814 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3815
Bill Schmidtef17c142013-02-06 17:33:58 +00003816 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003817
3818 // Size of the linkage area, parameter list area and the part of the local
3819 // space variable where copies of aggregates which are passed by value are
3820 // stored.
3821 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003822
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003823 // Calculate by how many bytes the stack has to be adjusted in case of tail
3824 // call optimization.
3825 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3826
3827 // Adjust the stack pointer for the new arguments...
3828 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003829 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3830 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003831 SDValue CallSeqStart = Chain;
3832
3833 // Load the return address and frame pointer so it can be moved somewhere else
3834 // later.
3835 SDValue LROp, FPOp;
3836 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3837 dl);
3838
3839 // Set up a copy of the stack pointer for use loading and storing any
3840 // arguments that may not fit in the registers available for argument
3841 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00003842 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00003843
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003844 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3845 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3846 SmallVector<SDValue, 8> MemOpChains;
3847
Roman Divacky71038e72011-08-30 17:04:16 +00003848 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003849 // Walk the register/memloc assignments, inserting copies/loads.
3850 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3851 i != e;
3852 ++i) {
3853 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003854 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003855 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00003856
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003857 if (Flags.isByVal()) {
3858 // Argument is an aggregate which is passed by value, thus we need to
3859 // create a copy of it in the local variable space of the current stack
3860 // frame (which is the stack frame of the caller) and pass the address of
3861 // this copy to the callee.
3862 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3863 CCValAssign &ByValVA = ByValArgLocs[j++];
3864 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00003865
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003866 // Memory reserved in the local variable space of the callers stack frame.
3867 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003868
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003869 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3870 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00003871
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003872 // Create a copy of the argument in the local area of the current
3873 // stack frame.
3874 SDValue MemcpyCall =
3875 CreateCopyOfByValArgument(Arg, PtrOff,
3876 CallSeqStart.getNode()->getOperand(0),
3877 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00003878
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003879 // This must go outside the CALLSEQ_START..END.
3880 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003881 CallSeqStart.getNode()->getOperand(1),
3882 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003883 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3884 NewCallSeqStart.getNode());
3885 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00003886
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003887 // Pass the address of the aggregate copy on the stack either in a
3888 // physical register or in the parameter list area of the current stack
3889 // frame to the callee.
3890 Arg = PtrOff;
3891 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003892
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003893 if (VA.isRegLoc()) {
Hal Finkel2a9d3182014-03-06 00:23:33 +00003894 if (Arg.getValueType() == MVT::i1)
3895 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg);
3896
Roman Divacky71038e72011-08-30 17:04:16 +00003897 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003898 // Put argument in a physical register.
3899 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3900 } else {
3901 // Put argument in the parameter list area of the current stack frame.
3902 assert(VA.isMemLoc());
3903 unsigned LocMemOffset = VA.getLocMemOffset();
3904
3905 if (!isTailCall) {
3906 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3907 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3908
3909 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00003910 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00003911 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003912 } else {
3913 // Calculate and remember argument location.
3914 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3915 TailCallArguments);
3916 }
3917 }
3918 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003919
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003920 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003921 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Wesley Peck527da1b2010-11-23 03:31:01 +00003922
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003923 // Build a sequence of copy-to-reg nodes chained together with token chain
3924 // and flag operands which copy the outgoing args into the appropriate regs.
3925 SDValue InFlag;
3926 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3927 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3928 RegsToPass[i].second, InFlag);
3929 InFlag = Chain.getValue(1);
3930 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003931
Hal Finkel5ab37802012-08-28 02:10:27 +00003932 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3933 // registers.
3934 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003935 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3936 SDValue Ops[] = { Chain, InFlag };
3937
Hal Finkel5ab37802012-08-28 02:10:27 +00003938 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00003939 dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1));
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003940
Hal Finkel5ab37802012-08-28 02:10:27 +00003941 InFlag = Chain.getValue(1);
3942 }
3943
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003944 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003945 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3946 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003947
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003948 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3949 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3950 Ins, InVals);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003951}
3952
Bill Schmidt57d6de52012-10-23 15:51:16 +00003953// Copy an argument into memory, being careful to do this outside the
3954// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003955SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00003956PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3957 SDValue CallSeqStart,
3958 ISD::ArgFlagsTy Flags,
3959 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003960 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003961 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3962 CallSeqStart.getNode()->getOperand(0),
3963 Flags, DAG, dl);
3964 // The MEMCPY must go outside the CALLSEQ_START..END.
3965 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003966 CallSeqStart.getNode()->getOperand(1),
3967 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00003968 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3969 NewCallSeqStart.getNode());
3970 return NewCallSeqStart;
3971}
3972
3973SDValue
3974PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003975 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003976 bool isTailCall,
3977 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003978 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003979 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003980 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003981 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003982
Bill Schmidt57d6de52012-10-23 15:51:16 +00003983 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003984
Bill Schmidt57d6de52012-10-23 15:51:16 +00003985 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3986 unsigned PtrByteSize = 8;
3987
3988 MachineFunction &MF = DAG.getMachineFunction();
3989
3990 // Mark this function as potentially containing a function that contains a
3991 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3992 // and restoring the callers stack pointer in this functions epilog. This is
3993 // done because by tail calling the called function might overwrite the value
3994 // in this function's (MF) stack pointer stack slot 0(SP).
3995 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3996 CallConv == CallingConv::Fast)
3997 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3998
3999 unsigned nAltivecParamsAtEnd = 0;
4000
4001 // Count how many bytes are to be pushed on the stack, including the linkage
4002 // area, and parameter passing area. We start with at least 48 bytes, which
4003 // is reserved space for [SP][CR][LR][3 x unused].
4004 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
4005 // of this call.
4006 unsigned NumBytes =
4007 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
4008 Outs, OutVals, nAltivecParamsAtEnd);
4009
4010 // Calculate by how many bytes the stack has to be adjusted in case of tail
4011 // call optimization.
4012 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4013
4014 // To protect arguments on the stack from being clobbered in a tail call,
4015 // force all the loads to happen before doing any other lowering.
4016 if (isTailCall)
4017 Chain = DAG.getStackArgumentTokenFactor(Chain);
4018
4019 // Adjust the stack pointer for the new arguments...
4020 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004021 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4022 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004023 SDValue CallSeqStart = Chain;
4024
4025 // Load the return address and frame pointer so it can be move somewhere else
4026 // later.
4027 SDValue LROp, FPOp;
4028 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4029 dl);
4030
4031 // Set up a copy of the stack pointer for use loading and storing any
4032 // arguments that may not fit in the registers available for argument
4033 // passing.
4034 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
4035
4036 // Figure out which arguments are going to go in registers, and which in
4037 // memory. Also, if this is a vararg function, floating point operations
4038 // must be stored to our stack, and loaded into integer regs as well, if
4039 // any integer regs are available for argument passing.
4040 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
4041 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
4042
Craig Topper840beec2014-04-04 05:16:06 +00004043 static const MCPhysReg GPR[] = {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004044 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4045 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4046 };
Craig Topper840beec2014-04-04 05:16:06 +00004047 static const MCPhysReg *FPR = GetFPR();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004048
Craig Topper840beec2014-04-04 05:16:06 +00004049 static const MCPhysReg VR[] = {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004050 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4051 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4052 };
Craig Topper840beec2014-04-04 05:16:06 +00004053 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00004054 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
4055 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
4056 };
4057
Bill Schmidt57d6de52012-10-23 15:51:16 +00004058 const unsigned NumGPRs = array_lengthof(GPR);
4059 const unsigned NumFPRs = 13;
4060 const unsigned NumVRs = array_lengthof(VR);
4061
4062 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4063 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4064
4065 SmallVector<SDValue, 8> MemOpChains;
4066 for (unsigned i = 0; i != NumOps; ++i) {
4067 SDValue Arg = OutVals[i];
4068 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4069
4070 // PtrOff will be used to store the current argument to the stack if a
4071 // register cannot be found for it.
4072 SDValue PtrOff;
4073
4074 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
4075
4076 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4077
4078 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00004079 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004080 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4081 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4082 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
4083 }
4084
4085 // FIXME memcpy is used way more than necessary. Correctness first.
4086 // Note: "by value" is code for passing a structure by value, not
4087 // basic types.
4088 if (Flags.isByVal()) {
4089 // Note: Size includes alignment padding, so
4090 // struct x { short a; char b; }
4091 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
4092 // These are the proper values we need for right-justifying the
4093 // aggregate in a parameter register.
4094 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00004095
4096 // An empty aggregate parameter takes up no storage and no
4097 // registers.
4098 if (Size == 0)
4099 continue;
4100
Hal Finkel262a2242013-09-12 23:20:06 +00004101 unsigned BVAlign = Flags.getByValAlign();
4102 if (BVAlign > 8) {
4103 if (BVAlign % PtrByteSize != 0)
4104 llvm_unreachable(
4105 "ByVal alignment is not a multiple of the pointer size");
4106
4107 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
4108 }
4109
Bill Schmidt57d6de52012-10-23 15:51:16 +00004110 // All aggregates smaller than 8 bytes must be passed right-justified.
4111 if (Size==1 || Size==2 || Size==4) {
4112 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
4113 if (GPR_idx != NumGPRs) {
4114 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
4115 MachinePointerInfo(), VT,
4116 false, false, 0);
4117 MemOpChains.push_back(Load.getValue(1));
4118 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4119
4120 ArgOffset += PtrByteSize;
4121 continue;
4122 }
4123 }
4124
4125 if (GPR_idx == NumGPRs && Size < 8) {
4126 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4127 PtrOff.getValueType());
4128 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4129 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4130 CallSeqStart,
4131 Flags, DAG, dl);
4132 ArgOffset += PtrByteSize;
4133 continue;
4134 }
4135 // Copy entire object into memory. There are cases where gcc-generated
4136 // code assumes it is there, even if it could be put entirely into
4137 // registers. (This is not what the doc says.)
4138
4139 // FIXME: The above statement is likely due to a misunderstanding of the
4140 // documents. All arguments must be copied into the parameter area BY
4141 // THE CALLEE in the event that the callee takes the address of any
4142 // formal argument. That has not yet been implemented. However, it is
4143 // reasonable to use the stack area as a staging area for the register
4144 // load.
4145
4146 // Skip this for small aggregates, as we will use the same slot for a
4147 // right-justified copy, below.
4148 if (Size >= 8)
4149 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4150 CallSeqStart,
4151 Flags, DAG, dl);
4152
4153 // When a register is available, pass a small aggregate right-justified.
4154 if (Size < 8 && GPR_idx != NumGPRs) {
4155 // The easiest way to get this right-justified in a register
4156 // is to copy the structure into the rightmost portion of a
4157 // local variable slot, then load the whole slot into the
4158 // register.
4159 // FIXME: The memcpy seems to produce pretty awful code for
4160 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00004161 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00004162 // parameter save area instead of a new local variable.
4163 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
4164 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4165 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4166 CallSeqStart,
4167 Flags, DAG, dl);
4168
4169 // Load the slot into the register.
4170 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
4171 MachinePointerInfo(),
4172 false, false, false, 0);
4173 MemOpChains.push_back(Load.getValue(1));
4174 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4175
4176 // Done with this argument.
4177 ArgOffset += PtrByteSize;
4178 continue;
4179 }
4180
4181 // For aggregates larger than PtrByteSize, copy the pieces of the
4182 // object that fit into registers from the parameter save area.
4183 for (unsigned j=0; j<Size; j+=PtrByteSize) {
4184 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
4185 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
4186 if (GPR_idx != NumGPRs) {
4187 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4188 MachinePointerInfo(),
4189 false, false, false, 0);
4190 MemOpChains.push_back(Load.getValue(1));
4191 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4192 ArgOffset += PtrByteSize;
4193 } else {
4194 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4195 break;
4196 }
4197 }
4198 continue;
4199 }
4200
Craig Topper56710102013-08-15 02:33:50 +00004201 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004202 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00004203 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004204 case MVT::i32:
4205 case MVT::i64:
4206 if (GPR_idx != NumGPRs) {
4207 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
4208 } else {
4209 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4210 true, isTailCall, false, MemOpChains,
4211 TailCallArguments, dl);
4212 }
4213 ArgOffset += PtrByteSize;
4214 break;
4215 case MVT::f32:
4216 case MVT::f64:
4217 if (FPR_idx != NumFPRs) {
4218 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4219
4220 if (isVarArg) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004221 // A single float or an aggregate containing only a single float
4222 // must be passed right-justified in the stack doubleword, and
4223 // in the GPR, if one is available.
4224 SDValue StoreOff;
Craig Topper56710102013-08-15 02:33:50 +00004225 if (Arg.getSimpleValueType().SimpleTy == MVT::f32) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004226 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4227 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4228 } else
4229 StoreOff = PtrOff;
4230
4231 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004232 MachinePointerInfo(), false, false, 0);
4233 MemOpChains.push_back(Store);
4234
4235 // Float varargs are always shadowed in available integer registers
4236 if (GPR_idx != NumGPRs) {
4237 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4238 MachinePointerInfo(), false, false,
4239 false, 0);
4240 MemOpChains.push_back(Load.getValue(1));
4241 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4242 }
4243 } else if (GPR_idx != NumGPRs)
4244 // If we have any FPRs remaining, we may also have GPRs remaining.
4245 ++GPR_idx;
4246 } else {
4247 // Single-precision floating-point values are mapped to the
4248 // second (rightmost) word of the stack doubleword.
4249 if (Arg.getValueType() == MVT::f32) {
4250 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4251 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4252 }
4253
4254 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4255 true, isTailCall, false, MemOpChains,
4256 TailCallArguments, dl);
4257 }
4258 ArgOffset += 8;
4259 break;
4260 case MVT::v4f32:
4261 case MVT::v4i32:
4262 case MVT::v8i16:
4263 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00004264 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00004265 case MVT::v2i64:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004266 if (isVarArg) {
4267 // These go aligned on the stack, or in the corresponding R registers
4268 // when within range. The Darwin PPC ABI doc claims they also go in
4269 // V registers; in fact gcc does this only for arguments that are
4270 // prototyped, not for those that match the ... We do it for all
4271 // arguments, seems to work.
4272 while (ArgOffset % 16 !=0) {
4273 ArgOffset += PtrByteSize;
4274 if (GPR_idx != NumGPRs)
4275 GPR_idx++;
4276 }
4277 // We could elide this store in the case where the object fits
4278 // entirely in R registers. Maybe later.
4279 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
4280 DAG.getConstant(ArgOffset, PtrVT));
4281 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4282 MachinePointerInfo(), false, false, 0);
4283 MemOpChains.push_back(Store);
4284 if (VR_idx != NumVRs) {
4285 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4286 MachinePointerInfo(),
4287 false, false, false, 0);
4288 MemOpChains.push_back(Load.getValue(1));
Hal Finkel7811c612014-03-28 19:58:11 +00004289
4290 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4291 Arg.getSimpleValueType() == MVT::v2i64) ?
4292 VSRH[VR_idx] : VR[VR_idx];
4293 ++VR_idx;
4294
4295 RegsToPass.push_back(std::make_pair(VReg, Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004296 }
4297 ArgOffset += 16;
4298 for (unsigned i=0; i<16; i+=PtrByteSize) {
4299 if (GPR_idx == NumGPRs)
4300 break;
4301 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4302 DAG.getConstant(i, PtrVT));
4303 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4304 false, false, false, 0);
4305 MemOpChains.push_back(Load.getValue(1));
4306 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4307 }
4308 break;
4309 }
4310
4311 // Non-varargs Altivec params generally go in registers, but have
4312 // stack space allocated at the end.
4313 if (VR_idx != NumVRs) {
4314 // Doesn't have GPR space allocated.
Hal Finkel7811c612014-03-28 19:58:11 +00004315 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4316 Arg.getSimpleValueType() == MVT::v2i64) ?
4317 VSRH[VR_idx] : VR[VR_idx];
4318 ++VR_idx;
4319
4320 RegsToPass.push_back(std::make_pair(VReg, Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004321 } else {
4322 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4323 true, isTailCall, true, MemOpChains,
4324 TailCallArguments, dl);
4325 ArgOffset += 16;
4326 }
4327 break;
4328 }
4329 }
4330
4331 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004332 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004333
4334 // Check if this is an indirect call (MTCTR/BCTRL).
4335 // See PrepareCall() for more information about calls through function
4336 // pointers in the 64-bit SVR4 ABI.
4337 if (!isTailCall &&
4338 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4339 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4340 !isBLACompatibleAddress(Callee, DAG)) {
4341 // Load r2 into a virtual register and store it to the TOC save area.
4342 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4343 // TOC save area offset.
4344 SDValue PtrOff = DAG.getIntPtrConstant(40);
4345 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4346 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4347 false, false, 0);
4348 // R12 must contain the address of an indirect callee. This does not
4349 // mean the MTCTR instruction must use R12; it's easier to model this
4350 // as an extra parameter, so do that.
4351 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4352 }
4353
4354 // Build a sequence of copy-to-reg nodes chained together with token chain
4355 // and flag operands which copy the outgoing args into the appropriate regs.
4356 SDValue InFlag;
4357 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4358 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4359 RegsToPass[i].second, InFlag);
4360 InFlag = Chain.getValue(1);
4361 }
4362
4363 if (isTailCall)
4364 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4365 FPOp, true, TailCallArguments);
4366
4367 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4368 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4369 Ins, InVals);
4370}
4371
4372SDValue
4373PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4374 CallingConv::ID CallConv, bool isVarArg,
4375 bool isTailCall,
4376 const SmallVectorImpl<ISD::OutputArg> &Outs,
4377 const SmallVectorImpl<SDValue> &OutVals,
4378 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004379 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004380 SmallVectorImpl<SDValue> &InVals) const {
4381
4382 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004383
Owen Anderson53aa7a92009-08-10 22:56:29 +00004384 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00004385 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004386 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004387
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004388 MachineFunction &MF = DAG.getMachineFunction();
4389
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004390 // Mark this function as potentially containing a function that contains a
4391 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4392 // and restoring the callers stack pointer in this functions epilog. This is
4393 // done because by tail calling the called function might overwrite the value
4394 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004395 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4396 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004397 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4398
4399 unsigned nAltivecParamsAtEnd = 0;
4400
Chris Lattneraa40ec12006-05-16 22:56:08 +00004401 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00004402 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00004403 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004404 unsigned NumBytes =
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004405 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004406 Outs, OutVals,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004407 nAltivecParamsAtEnd);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004408
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004409 // Calculate by how many bytes the stack has to be adjusted in case of tail
4410 // call optimization.
4411 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004412
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004413 // To protect arguments on the stack from being clobbered in a tail call,
4414 // force all the loads to happen before doing any other lowering.
4415 if (isTailCall)
4416 Chain = DAG.getStackArgumentTokenFactor(Chain);
4417
Chris Lattnerb7552a82006-05-17 00:15:40 +00004418 // Adjust the stack pointer for the new arguments...
4419 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004420 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4421 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004422 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004423
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004424 // Load the return address and frame pointer so it can be move somewhere else
4425 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004426 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004427 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4428 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004429
Chris Lattnerb7552a82006-05-17 00:15:40 +00004430 // Set up a copy of the stack pointer for use loading and storing any
4431 // arguments that may not fit in the registers available for argument
4432 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004433 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004434 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004435 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004436 else
Owen Anderson9f944592009-08-11 20:47:22 +00004437 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004438
Chris Lattnerb7552a82006-05-17 00:15:40 +00004439 // Figure out which arguments are going to go in registers, and which in
4440 // memory. Also, if this is a vararg function, floating point operations
4441 // must be stored to our stack, and loaded into integer regs as well, if
4442 // any integer regs are available for argument passing.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004443 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004444 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004445
Craig Topper840beec2014-04-04 05:16:06 +00004446 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004447 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4448 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4449 };
Craig Topper840beec2014-04-04 05:16:06 +00004450 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00004451 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4452 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4453 };
Craig Topper840beec2014-04-04 05:16:06 +00004454 static const MCPhysReg *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004455
Craig Topper840beec2014-04-04 05:16:06 +00004456 static const MCPhysReg VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004457 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4458 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4459 };
Owen Andersone2f23a32007-09-07 04:06:50 +00004460 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004461 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00004462 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004463
Craig Topper840beec2014-04-04 05:16:06 +00004464 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004465
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004466 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004467 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4468
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004469 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00004470 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004471 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004472 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004473
Chris Lattnerb7552a82006-05-17 00:15:40 +00004474 // PtrOff will be used to store the current argument to the stack if a
4475 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004476 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004477
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004478 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004479
Dale Johannesen679073b2009-02-04 02:34:38 +00004480 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004481
4482 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00004483 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00004484 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4485 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00004486 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004487 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00004488
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004489 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004490 // Note: "by value" is code for passing a structure by value, not
4491 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00004492 if (Flags.isByVal()) {
4493 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004494 // Very small objects are passed right-justified. Everything else is
4495 // passed left-justified.
4496 if (Size==1 || Size==2) {
4497 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004498 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00004499 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00004500 MachinePointerInfo(), VT,
4501 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004502 MemOpChains.push_back(Load.getValue(1));
4503 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004504
4505 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004506 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00004507 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4508 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004509 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004510 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4511 CallSeqStart,
4512 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004513 ArgOffset += PtrByteSize;
4514 }
4515 continue;
4516 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004517 // Copy entire object into memory. There are cases where gcc-generated
4518 // code assumes it is there, even if it could be put entirely into
4519 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004520 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4521 CallSeqStart,
4522 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004523
4524 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4525 // copy the pieces of the object that fit into registers from the
4526 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00004527 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004528 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004529 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00004530 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004531 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4532 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004533 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00004534 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00004535 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004536 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004537 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004538 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004539 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004540 }
4541 }
4542 continue;
4543 }
4544
Craig Topper56710102013-08-15 02:33:50 +00004545 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004546 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00004547 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00004548 case MVT::i32:
4549 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004550 if (GPR_idx != NumGPRs) {
Hal Finkel7f908e82014-03-06 00:45:19 +00004551 if (Arg.getValueType() == MVT::i1)
4552 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg);
4553
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004554 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004555 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004556 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4557 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004558 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00004559 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004560 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004561 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004562 case MVT::f32:
4563 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004564 if (FPR_idx != NumFPRs) {
4565 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4566
Chris Lattnerb7552a82006-05-17 00:15:40 +00004567 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00004568 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4569 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004570 MemOpChains.push_back(Store);
4571
Chris Lattnerb7552a82006-05-17 00:15:40 +00004572 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004573 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004574 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004575 MachinePointerInfo(), false, false,
4576 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004577 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004578 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004579 }
Owen Anderson9f944592009-08-11 20:47:22 +00004580 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004581 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004582 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00004583 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4584 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004585 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004586 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004587 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00004588 }
4589 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00004590 // If we have any FPRs remaining, we may also have GPRs remaining.
4591 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4592 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004593 if (GPR_idx != NumGPRs)
4594 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00004595 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004596 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4597 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004598 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004599 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004600 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4601 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004602 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004603 if (isPPC64)
4604 ArgOffset += 8;
4605 else
Owen Anderson9f944592009-08-11 20:47:22 +00004606 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004607 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004608 case MVT::v4f32:
4609 case MVT::v4i32:
4610 case MVT::v8i16:
4611 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00004612 if (isVarArg) {
4613 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00004614 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00004615 // V registers; in fact gcc does this only for arguments that are
4616 // prototyped, not for those that match the ... We do it for all
4617 // arguments, seems to work.
4618 while (ArgOffset % 16 !=0) {
4619 ArgOffset += PtrByteSize;
4620 if (GPR_idx != NumGPRs)
4621 GPR_idx++;
4622 }
4623 // We could elide this store in the case where the object fits
4624 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004625 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004626 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00004627 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4628 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004629 MemOpChains.push_back(Store);
4630 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004631 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00004632 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004633 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004634 MemOpChains.push_back(Load.getValue(1));
4635 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4636 }
4637 ArgOffset += 16;
4638 for (unsigned i=0; i<16; i+=PtrByteSize) {
4639 if (GPR_idx == NumGPRs)
4640 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00004641 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004642 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00004643 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004644 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004645 MemOpChains.push_back(Load.getValue(1));
4646 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4647 }
4648 break;
4649 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004650
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004651 // Non-varargs Altivec params generally go in registers, but have
4652 // stack space allocated at the end.
4653 if (VR_idx != NumVRs) {
4654 // Doesn't have GPR space allocated.
4655 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4656 } else if (nAltivecParamsAtEnd==0) {
4657 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004658 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4659 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004660 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004661 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00004662 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00004663 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004664 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00004665 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004666 // If all Altivec parameters fit in registers, as they usually do,
4667 // they get stack space following the non-Altivec parameters. We
4668 // don't track this here because nobody below needs it.
4669 // If there are more Altivec parameters than fit in registers emit
4670 // the stores here.
4671 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4672 unsigned j = 0;
4673 // Offset is aligned; skip 1st 12 params which go in V registers.
4674 ArgOffset = ((ArgOffset+15)/16)*16;
4675 ArgOffset += 12*16;
4676 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004677 SDValue Arg = OutVals[i];
4678 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00004679 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4680 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004681 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004682 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004683 // We are emitting Altivec params in order.
4684 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4685 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004686 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004687 ArgOffset += 16;
4688 }
4689 }
4690 }
4691 }
4692
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004693 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004694 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004695
Dale Johannesen90eab672010-03-09 20:15:42 +00004696 // On Darwin, R12 must contain the address of an indirect callee. This does
4697 // not mean the MTCTR instruction must use R12; it's easier to model this as
4698 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004699 if (!isTailCall &&
Dale Johannesen90eab672010-03-09 20:15:42 +00004700 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4701 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4702 !isBLACompatibleAddress(Callee, DAG))
4703 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4704 PPC::R12), Callee));
4705
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004706 // Build a sequence of copy-to-reg nodes chained together with token chain
4707 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004708 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004709 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00004710 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00004711 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004712 InFlag = Chain.getValue(1);
4713 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004714
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004715 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004716 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4717 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004718
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004719 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4720 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4721 Ins, InVals);
Chris Lattneraa40ec12006-05-16 22:56:08 +00004722}
4723
Hal Finkel450128a2011-10-14 19:51:36 +00004724bool
4725PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4726 MachineFunction &MF, bool isVarArg,
4727 const SmallVectorImpl<ISD::OutputArg> &Outs,
4728 LLVMContext &Context) const {
4729 SmallVector<CCValAssign, 16> RVLocs;
4730 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4731 RVLocs, Context);
4732 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4733}
4734
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004735SDValue
4736PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004737 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004738 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004739 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004740 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004741
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004742 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004743 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00004744 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004745 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004746
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004747 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004748 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004749
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004750 // Copy the result values into the output registers.
4751 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4752 CCValAssign &VA = RVLocs[i];
4753 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004754
4755 SDValue Arg = OutVals[i];
4756
4757 switch (VA.getLocInfo()) {
4758 default: llvm_unreachable("Unknown loc info!");
4759 case CCValAssign::Full: break;
4760 case CCValAssign::AExt:
4761 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4762 break;
4763 case CCValAssign::ZExt:
4764 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4765 break;
4766 case CCValAssign::SExt:
4767 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4768 break;
4769 }
4770
4771 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004772 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004773 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004774 }
4775
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004776 RetOps[0] = Chain; // Update chain.
4777
4778 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00004779 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004780 RetOps.push_back(Flag);
4781
Craig Topper48d114b2014-04-26 18:35:24 +00004782 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps);
Chris Lattner4211ca92006-04-14 06:01:58 +00004783}
4784
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004785SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004786 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00004787 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004788 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004789
Jim Laskeye4f4d042006-12-04 22:04:42 +00004790 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004791 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00004792
4793 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00004794 bool isPPC64 = Subtarget.isPPC64();
4795 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004796 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004797
4798 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004799 SDValue Chain = Op.getOperand(0);
4800 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004801
Jim Laskeye4f4d042006-12-04 22:04:42 +00004802 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00004803 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4804 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004805 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004806
Jim Laskeye4f4d042006-12-04 22:04:42 +00004807 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00004808 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004809
Jim Laskeye4f4d042006-12-04 22:04:42 +00004810 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00004811 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004812 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004813}
4814
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004815
4816
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004817SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004818PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004819 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004820 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004821 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004822 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004823
4824 // Get current frame pointer save index. The users of this index will be
4825 // primarily DYNALLOC instructions.
4826 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4827 int RASI = FI->getReturnAddrSaveIndex();
4828
4829 // If the frame pointer save index hasn't been defined yet.
4830 if (!RASI) {
4831 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004832 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004833 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004834 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004835 // Save the result.
4836 FI->setReturnAddrSaveIndex(RASI);
4837 }
4838 return DAG.getFrameIndex(RASI, PtrVT);
4839}
4840
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004841SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004842PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4843 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004844 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004845 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004846 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004847
4848 // Get current frame pointer save index. The users of this index will be
4849 // primarily DYNALLOC instructions.
4850 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4851 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004852
Jim Laskey48850c12006-11-16 22:43:37 +00004853 // If the frame pointer save index hasn't been defined yet.
4854 if (!FPSI) {
4855 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004856 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004857 isDarwinABI);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004858
Jim Laskey48850c12006-11-16 22:43:37 +00004859 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004860 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00004861 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004862 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00004863 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004864 return DAG.getFrameIndex(FPSI, PtrVT);
4865}
Jim Laskey48850c12006-11-16 22:43:37 +00004866
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004867SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004868 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004869 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004870 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004871 SDValue Chain = Op.getOperand(0);
4872 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004873 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004874
Jim Laskey48850c12006-11-16 22:43:37 +00004875 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004876 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004877 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004878 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00004879 DAG.getConstant(0, PtrVT), Size);
4880 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004881 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00004882 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004883 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00004884 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Craig Topper48d114b2014-04-26 18:35:24 +00004885 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops);
Jim Laskey48850c12006-11-16 22:43:37 +00004886}
4887
Hal Finkel756810f2013-03-21 21:37:52 +00004888SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
4889 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004890 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004891 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
4892 DAG.getVTList(MVT::i32, MVT::Other),
4893 Op.getOperand(0), Op.getOperand(1));
4894}
4895
4896SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
4897 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004898 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004899 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
4900 Op.getOperand(0), Op.getOperand(1));
4901}
4902
Hal Finkel940ab932014-02-28 00:27:01 +00004903SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
4904 assert(Op.getValueType() == MVT::i1 &&
4905 "Custom lowering only for i1 loads");
4906
4907 // First, load 8 bits into 32 bits, then truncate to 1 bit.
4908
4909 SDLoc dl(Op);
4910 LoadSDNode *LD = cast<LoadSDNode>(Op);
4911
4912 SDValue Chain = LD->getChain();
4913 SDValue BasePtr = LD->getBasePtr();
4914 MachineMemOperand *MMO = LD->getMemOperand();
4915
4916 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(), Chain,
4917 BasePtr, MVT::i8, MMO);
4918 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
4919
4920 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00004921 return DAG.getMergeValues(Ops, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00004922}
4923
4924SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
4925 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
4926 "Custom lowering only for i1 stores");
4927
4928 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
4929
4930 SDLoc dl(Op);
4931 StoreSDNode *ST = cast<StoreSDNode>(Op);
4932
4933 SDValue Chain = ST->getChain();
4934 SDValue BasePtr = ST->getBasePtr();
4935 SDValue Value = ST->getValue();
4936 MachineMemOperand *MMO = ST->getMemOperand();
4937
4938 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(), Value);
4939 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
4940}
4941
4942// FIXME: Remove this once the ANDI glue bug is fixed:
4943SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
4944 assert(Op.getValueType() == MVT::i1 &&
4945 "Custom lowering only for i1 results");
4946
4947 SDLoc DL(Op);
4948 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
4949 Op.getOperand(0));
4950}
4951
Chris Lattner4211ca92006-04-14 06:01:58 +00004952/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4953/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004954SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00004955 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00004956 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4957 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00004958 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004959
Hal Finkel81f87992013-04-07 22:11:09 +00004960 // We might be able to do better than this under some circumstances, but in
4961 // general, fsel-based lowering of select is a finite-math-only optimization.
4962 // For more information, see section F.3 of the 2.06 ISA specification.
4963 if (!DAG.getTarget().Options.NoInfsFPMath ||
4964 !DAG.getTarget().Options.NoNaNsFPMath)
4965 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004966
Hal Finkel81f87992013-04-07 22:11:09 +00004967 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004968
Owen Anderson53aa7a92009-08-10 22:56:29 +00004969 EVT ResVT = Op.getValueType();
4970 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004971 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4972 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004973 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004974
Chris Lattner4211ca92006-04-14 06:01:58 +00004975 // If the RHS of the comparison is a 0.0, we don't need to do the
4976 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00004977 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00004978 if (isFloatingPointZero(RHS))
4979 switch (CC) {
4980 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004981 case ISD::SETNE:
4982 std::swap(TV, FV);
4983 case ISD::SETEQ:
4984 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4985 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
4986 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
4987 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4988 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4989 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4990 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004991 case ISD::SETULT:
4992 case ISD::SETLT:
4993 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004994 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004995 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00004996 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4997 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004998 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004999 case ISD::SETUGT:
5000 case ISD::SETGT:
5001 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005002 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005003 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00005004 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5005 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005006 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005007 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005008 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005009
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005010 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00005011 switch (CC) {
5012 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00005013 case ISD::SETNE:
5014 std::swap(TV, FV);
5015 case ISD::SETEQ:
5016 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
5017 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5018 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
5019 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
5020 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
5021 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
5022 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5023 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005024 case ISD::SETULT:
5025 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005026 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005027 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5028 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005029 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005030 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005031 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005032 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005033 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5034 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005035 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005036 case ISD::SETUGT:
5037 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005038 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005039 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5040 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005041 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005042 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005043 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005044 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005045 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5046 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005047 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005048 }
Eli Friedman5806e182009-05-28 04:31:08 +00005049 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00005050}
5051
Chris Lattner57ee7c62007-11-28 18:44:47 +00005052// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005053SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005054 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00005055 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005056 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00005057 if (Src.getValueType() == MVT::f32)
5058 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00005059
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005060 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00005061 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005062 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00005063 case MVT::i32:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005064 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Hal Finkelf6d45f22013-04-01 17:52:07 +00005065 (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ :
5066 PPCISD::FCTIDZ),
Owen Anderson9f944592009-08-11 20:47:22 +00005067 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005068 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005069 case MVT::i64:
Hal Finkel3f88d082013-04-01 18:42:58 +00005070 assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) &&
5071 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00005072 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
5073 PPCISD::FCTIDUZ,
5074 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005075 break;
5076 }
Duncan Sands2a287912008-07-19 16:26:02 +00005077
Chris Lattner4211ca92006-04-14 06:01:58 +00005078 // Convert the FP value to an int value through memory.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005079 bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() &&
5080 (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT());
5081 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
5082 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
5083 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00005084
Chris Lattner06a49542007-10-15 20:14:52 +00005085 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005086 SDValue Chain;
5087 if (i32Stack) {
5088 MachineFunction &MF = DAG.getMachineFunction();
5089 MachineMemOperand *MMO =
5090 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
5091 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
5092 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00005093 DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005094 } else
5095 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
5096 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00005097
5098 // Result is a load from the stack slot. If loading 4 bytes, make sure to
5099 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005100 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00005101 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00005102 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkelf6d45f22013-04-01 17:52:07 +00005103 MPI = MachinePointerInfo();
5104 }
5105
5106 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooper82cd9e82011-11-08 18:42:53 +00005107 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005108}
5109
Hal Finkelf6d45f22013-04-01 17:52:07 +00005110SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005111 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005112 SDLoc dl(Op);
Dan Gohmand6819da2008-03-11 01:59:03 +00005113 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00005114 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005115 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00005116
Hal Finkel6a56b212014-03-05 22:14:00 +00005117 if (Op.getOperand(0).getValueType() == MVT::i1)
5118 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0),
5119 DAG.getConstantFP(1.0, Op.getValueType()),
5120 DAG.getConstantFP(0.0, Op.getValueType()));
5121
Hal Finkelf6d45f22013-04-01 17:52:07 +00005122 assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) &&
5123 "UINT_TO_FP is supported only with FPCVT");
5124
5125 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00005126 // Otherwise, convert to double-precision and then round.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005127 unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
5128 (Op.getOpcode() == ISD::UINT_TO_FP ?
5129 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
5130 (Op.getOpcode() == ISD::UINT_TO_FP ?
5131 PPCISD::FCFIDU : PPCISD::FCFID);
5132 MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
5133 MVT::f32 : MVT::f64;
5134
Owen Anderson9f944592009-08-11 20:47:22 +00005135 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005136 SDValue SINT = Op.getOperand(0);
5137 // When converting to single-precision, we actually need to convert
5138 // to double-precision first and then round to single-precision.
5139 // To avoid double-rounding effects during that operation, we have
5140 // to prepare the input operand. Bits that might be truncated when
5141 // converting to double-precision are replaced by a bit that won't
5142 // be lost at this stage, but is below the single-precision rounding
5143 // position.
5144 //
5145 // However, if -enable-unsafe-fp-math is in effect, accept double
5146 // rounding to avoid the extra overhead.
5147 if (Op.getValueType() == MVT::f32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005148 !PPCSubTarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005149 !DAG.getTarget().Options.UnsafeFPMath) {
5150
5151 // Twiddle input to make sure the low 11 bits are zero. (If this
5152 // is the case, we are guaranteed the value will fit into the 53 bit
5153 // mantissa of an IEEE double-precision value without rounding.)
5154 // If any of those low 11 bits were not zero originally, make sure
5155 // bit 12 (value 2048) is set instead, so that the final rounding
5156 // to single-precision gets the correct result.
5157 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5158 SINT, DAG.getConstant(2047, MVT::i64));
5159 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
5160 Round, DAG.getConstant(2047, MVT::i64));
5161 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
5162 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5163 Round, DAG.getConstant(-2048, MVT::i64));
5164
5165 // However, we cannot use that value unconditionally: if the magnitude
5166 // of the input value is small, the bit-twiddling we did above might
5167 // end up visibly changing the output. Fortunately, in that case, we
5168 // don't need to twiddle bits since the original input will convert
5169 // exactly to double-precision floating-point already. Therefore,
5170 // construct a conditional to use the original value if the top 11
5171 // bits are all sign-bit copies, and use the rounded value computed
5172 // above otherwise.
5173 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
5174 SINT, DAG.getConstant(53, MVT::i32));
5175 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
5176 Cond, DAG.getConstant(1, MVT::i64));
5177 Cond = DAG.getSetCC(dl, MVT::i32,
5178 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
5179
5180 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
5181 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00005182
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005183 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005184 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
5185
5186 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00005187 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005188 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005189 return FP;
5190 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005191
Owen Anderson9f944592009-08-11 20:47:22 +00005192 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005193 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005194 // Since we only generate this in 64-bit mode, we can take advantage of
5195 // 64-bit registers. In particular, sign extend the input value into the
5196 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
5197 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00005198 MachineFunction &MF = DAG.getMachineFunction();
5199 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005200 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005201
Hal Finkelbeb296b2013-03-31 10:12:51 +00005202 SDValue Ld;
Hal Finkelf6d45f22013-04-01 17:52:07 +00005203 if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) {
Hal Finkelbeb296b2013-03-31 10:12:51 +00005204 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
5205 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005206
Hal Finkelbeb296b2013-03-31 10:12:51 +00005207 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
5208 MachinePointerInfo::getFixedStack(FrameIdx),
5209 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00005210
Hal Finkelbeb296b2013-03-31 10:12:51 +00005211 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
5212 "Expected an i32 store");
5213 MachineMemOperand *MMO =
5214 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
5215 MachineMemOperand::MOLoad, 4, 4);
5216 SDValue Ops[] = { Store, FIdx };
Hal Finkelf6d45f22013-04-01 17:52:07 +00005217 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
5218 PPCISD::LFIWZX : PPCISD::LFIWAX,
5219 dl, DAG.getVTList(MVT::f64, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00005220 Ops, MVT::i32, MMO);
Hal Finkelbeb296b2013-03-31 10:12:51 +00005221 } else {
Hal Finkelf6d45f22013-04-01 17:52:07 +00005222 assert(PPCSubTarget.isPPC64() &&
5223 "i32->FP without LFIWAX supported only on PPC64");
5224
Hal Finkelbeb296b2013-03-31 10:12:51 +00005225 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
5226 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
5227
5228 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
5229 Op.getOperand(0));
5230
5231 // STD the extended value into the stack slot.
5232 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
5233 MachinePointerInfo::getFixedStack(FrameIdx),
5234 false, false, 0);
5235
5236 // Load the value as a double.
5237 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
5238 MachinePointerInfo::getFixedStack(FrameIdx),
5239 false, false, false, 0);
5240 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005241
Chris Lattner4211ca92006-04-14 06:01:58 +00005242 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005243 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
5244 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00005245 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005246 return FP;
5247}
5248
Dan Gohman21cea8a2010-04-17 15:26:15 +00005249SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
5250 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005251 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005252 /*
5253 The rounding mode is in bits 30:31 of FPSR, and has the following
5254 settings:
5255 00 Round to nearest
5256 01 Round to 0
5257 10 Round to +inf
5258 11 Round to -inf
5259
5260 FLT_ROUNDS, on the other hand, expects the following:
5261 -1 Undefined
5262 0 Round to 0
5263 1 Round to nearest
5264 2 Round to +inf
5265 3 Round to -inf
5266
5267 To perform the conversion, we do:
5268 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
5269 */
5270
5271 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005272 EVT VT = Op.getValueType();
5273 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005274
5275 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005276 EVT NodeTys[] = {
5277 MVT::f64, // return register
5278 MVT::Glue // unused in this context
5279 };
Craig Topper2d2aa0c2014-04-30 07:17:30 +00005280 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, None);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005281
5282 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00005283 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005284 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005285 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00005286 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005287
5288 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005289 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005290 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00005291 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005292 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005293
5294 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005295 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00005296 DAG.getNode(ISD::AND, dl, MVT::i32,
5297 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005298 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00005299 DAG.getNode(ISD::SRL, dl, MVT::i32,
5300 DAG.getNode(ISD::AND, dl, MVT::i32,
5301 DAG.getNode(ISD::XOR, dl, MVT::i32,
5302 CWD, DAG.getConstant(3, MVT::i32)),
5303 DAG.getConstant(3, MVT::i32)),
5304 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005305
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005306 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00005307 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005308
Duncan Sands13237ac2008-06-06 12:08:01 +00005309 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00005310 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005311}
5312
Dan Gohman21cea8a2010-04-17 15:26:15 +00005313SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005314 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005315 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005316 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00005317 assert(Op.getNumOperands() == 3 &&
5318 VT == Op.getOperand(1).getValueType() &&
5319 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005320
Chris Lattner601b8652006-09-20 03:47:40 +00005321 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005322 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005323 SDValue Lo = Op.getOperand(0);
5324 SDValue Hi = Op.getOperand(1);
5325 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005326 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005327
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005328 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005329 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005330 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5331 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5332 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5333 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005334 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005335 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5336 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5337 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005338 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005339 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005340}
5341
Dan Gohman21cea8a2010-04-17 15:26:15 +00005342SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005343 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005344 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00005345 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005346 assert(Op.getNumOperands() == 3 &&
5347 VT == Op.getOperand(1).getValueType() &&
5348 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005349
Dan Gohman8d2ead22008-03-07 20:36:53 +00005350 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005351 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005352 SDValue Lo = Op.getOperand(0);
5353 SDValue Hi = Op.getOperand(1);
5354 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005355 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005356
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005357 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005358 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005359 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5360 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5361 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5362 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005363 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005364 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5365 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5366 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005367 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005368 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005369}
5370
Dan Gohman21cea8a2010-04-17 15:26:15 +00005371SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005372 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005373 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005374 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005375 assert(Op.getNumOperands() == 3 &&
5376 VT == Op.getOperand(1).getValueType() &&
5377 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005378
Dan Gohman8d2ead22008-03-07 20:36:53 +00005379 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005380 SDValue Lo = Op.getOperand(0);
5381 SDValue Hi = Op.getOperand(1);
5382 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005383 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005384
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005385 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005386 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005387 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5388 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5389 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5390 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005391 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005392 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5393 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5394 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00005395 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005396 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005397 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005398}
5399
5400//===----------------------------------------------------------------------===//
5401// Vector related lowering.
5402//
5403
Chris Lattner2a099c02006-04-17 06:00:21 +00005404/// BuildSplatI - Build a canonical splati of Val with an element size of
5405/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005406static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005407 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00005408 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005409
Owen Anderson53aa7a92009-08-10 22:56:29 +00005410 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00005411 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00005412 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005413
Owen Anderson9f944592009-08-11 20:47:22 +00005414 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005415
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005416 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5417 if (Val == -1)
5418 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005419
Owen Anderson53aa7a92009-08-10 22:56:29 +00005420 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005421
Chris Lattner2a099c02006-04-17 06:00:21 +00005422 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00005423 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005424 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00005425 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Craig Topper48d114b2014-04-26 18:35:24 +00005426 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005427 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005428}
5429
Hal Finkelcf2e9082013-05-24 23:00:14 +00005430/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5431/// specified intrinsic ID.
5432static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005433 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00005434 EVT DestVT = MVT::Other) {
5435 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5436 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5437 DAG.getConstant(IID, MVT::i32), Op);
5438}
5439
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005440/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00005441/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005442static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005443 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005444 EVT DestVT = MVT::Other) {
5445 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005446 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005447 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005448}
5449
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005450/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5451/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005452static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005453 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005454 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00005455 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005456 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005457 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005458}
5459
5460
Chris Lattner264c9082006-04-17 17:55:10 +00005461/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5462/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005463static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005464 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00005465 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00005466 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5467 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005468
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005469 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00005470 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005471 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00005472 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005473 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00005474}
5475
Chris Lattner19e90552006-04-14 05:19:18 +00005476// If this is a case we can't handle, return null and let the default
5477// expansion code take care of it. If we CAN select this case, and if it
5478// selects to a single instruction, return Op. Otherwise, if we can codegen
5479// this case more efficiently than a constant pool load, lower it to the
5480// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005481SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5482 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005483 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005484 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
Craig Toppere73658d2014-04-28 04:05:08 +00005485 assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00005486
Bob Wilson85cefe82009-03-02 23:24:16 +00005487 // Check if this is a splat of a constant value.
5488 APInt APSplatBits, APSplatUndef;
5489 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005490 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00005491 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00005492 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00005493 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00005494
Bob Wilson530e0382009-03-03 19:26:27 +00005495 unsigned SplatBits = APSplatBits.getZExtValue();
5496 unsigned SplatUndef = APSplatUndef.getZExtValue();
5497 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005498
Bob Wilson530e0382009-03-03 19:26:27 +00005499 // First, handle single instruction cases.
5500
5501 // All zeros?
5502 if (SplatBits == 0) {
5503 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00005504 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5505 SDValue Z = DAG.getConstant(0, MVT::i32);
5506 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00005507 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00005508 }
Bob Wilson530e0382009-03-03 19:26:27 +00005509 return Op;
5510 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00005511
Bob Wilson530e0382009-03-03 19:26:27 +00005512 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5513 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5514 (32-SplatBitSize));
5515 if (SextVal >= -16 && SextVal <= 15)
5516 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005517
5518
Bob Wilson530e0382009-03-03 19:26:27 +00005519 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005520
Bob Wilson530e0382009-03-03 19:26:27 +00005521 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00005522 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5523 // If this value is in the range [17,31] and is odd, use:
5524 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5525 // If this value is in the range [-31,-17] and is odd, use:
5526 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5527 // Note the last two are three-instruction sequences.
5528 if (SextVal >= -32 && SextVal <= 31) {
5529 // To avoid having these optimizations undone by constant folding,
5530 // we convert to a pseudo that will be expanded later into one of
5531 // the above forms.
5532 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt51e79512013-02-20 15:50:31 +00005533 EVT VT = Op.getValueType();
5534 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5535 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5536 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilson530e0382009-03-03 19:26:27 +00005537 }
5538
5539 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5540 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5541 // for fneg/fabs.
5542 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5543 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00005544 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005545
5546 // Make the VSLW intrinsic, computing 0x8000_0000.
5547 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5548 OnesV, DAG, dl);
5549
5550 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00005551 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00005552 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005553 }
5554
5555 // Check to see if this is a wide variety of vsplti*, binop self cases.
5556 static const signed char SplatCsts[] = {
5557 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5558 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5559 };
5560
5561 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5562 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5563 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5564 int i = SplatCsts[idx];
5565
5566 // Figure out what shift amount will be used by altivec if shifted by i in
5567 // this splat size.
5568 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5569
5570 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00005571 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005572 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005573 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5574 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5575 Intrinsic::ppc_altivec_vslw
5576 };
5577 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005578 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005579 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005580
Bob Wilson530e0382009-03-03 19:26:27 +00005581 // vsplti + srl self.
5582 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005583 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005584 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5585 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5586 Intrinsic::ppc_altivec_vsrw
5587 };
5588 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005589 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005590 }
5591
Bob Wilson530e0382009-03-03 19:26:27 +00005592 // vsplti + sra self.
5593 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005594 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005595 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5596 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5597 Intrinsic::ppc_altivec_vsraw
5598 };
5599 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005600 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005601 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005602
Bob Wilson530e0382009-03-03 19:26:27 +00005603 // vsplti + rol self.
5604 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5605 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005606 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005607 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5608 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5609 Intrinsic::ppc_altivec_vrlw
5610 };
5611 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005612 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005613 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005614
Bob Wilson530e0382009-03-03 19:26:27 +00005615 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00005616 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005617 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005618 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00005619 }
Bob Wilson530e0382009-03-03 19:26:27 +00005620 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00005621 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005622 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005623 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00005624 }
Bob Wilson530e0382009-03-03 19:26:27 +00005625 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00005626 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005627 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005628 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5629 }
5630 }
5631
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005632 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00005633}
5634
Chris Lattner071ad012006-04-17 05:28:54 +00005635/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5636/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005637static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005638 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005639 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00005640 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00005641 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00005642 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005643
Chris Lattner071ad012006-04-17 05:28:54 +00005644 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00005645 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 +00005646 OP_VMRGHW,
5647 OP_VMRGLW,
5648 OP_VSPLTISW0,
5649 OP_VSPLTISW1,
5650 OP_VSPLTISW2,
5651 OP_VSPLTISW3,
5652 OP_VSLDOI4,
5653 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00005654 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00005655 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00005656
Chris Lattner071ad012006-04-17 05:28:54 +00005657 if (OpNum == OP_COPY) {
5658 if (LHSID == (1*9+2)*9+3) return LHS;
5659 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5660 return RHS;
5661 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005662
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005663 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005664 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5665 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005666
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005667 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00005668 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005669 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00005670 case OP_VMRGHW:
5671 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5672 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5673 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5674 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5675 break;
5676 case OP_VMRGLW:
5677 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5678 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5679 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5680 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5681 break;
5682 case OP_VSPLTISW0:
5683 for (unsigned i = 0; i != 16; ++i)
5684 ShufIdxs[i] = (i&3)+0;
5685 break;
5686 case OP_VSPLTISW1:
5687 for (unsigned i = 0; i != 16; ++i)
5688 ShufIdxs[i] = (i&3)+4;
5689 break;
5690 case OP_VSPLTISW2:
5691 for (unsigned i = 0; i != 16; ++i)
5692 ShufIdxs[i] = (i&3)+8;
5693 break;
5694 case OP_VSPLTISW3:
5695 for (unsigned i = 0; i != 16; ++i)
5696 ShufIdxs[i] = (i&3)+12;
5697 break;
5698 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005699 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005700 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005701 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005702 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005703 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005704 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00005705 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00005706 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5707 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005708 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00005709 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00005710}
5711
Chris Lattner19e90552006-04-14 05:19:18 +00005712/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5713/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5714/// return the code it can be lowered into. Worst case, it can always be
5715/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005716SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005717 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005718 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005719 SDValue V1 = Op.getOperand(0);
5720 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005721 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005722 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005723
Chris Lattner19e90552006-04-14 05:19:18 +00005724 // Cases that are handled by instructions that take permute immediates
5725 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5726 // selected by the instruction selector.
5727 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005728 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5729 PPC::isSplatShuffleMask(SVOp, 2) ||
5730 PPC::isSplatShuffleMask(SVOp, 4) ||
5731 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5732 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5733 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5734 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5735 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5736 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5737 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5738 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5739 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattner19e90552006-04-14 05:19:18 +00005740 return Op;
5741 }
5742 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005743
Chris Lattner19e90552006-04-14 05:19:18 +00005744 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5745 // and produce a fixed permutation. If any of these match, do not lower to
5746 // VPERM.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005747 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5748 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5749 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5750 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5751 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5752 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5753 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5754 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5755 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattner19e90552006-04-14 05:19:18 +00005756 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005757
Chris Lattner071ad012006-04-17 05:28:54 +00005758 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5759 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005760 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00005761
Chris Lattner071ad012006-04-17 05:28:54 +00005762 unsigned PFIndexes[4];
5763 bool isFourElementShuffle = true;
5764 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5765 unsigned EltNo = 8; // Start out undef.
5766 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005767 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00005768 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005769
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005770 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00005771 if ((ByteSource & 3) != j) {
5772 isFourElementShuffle = false;
5773 break;
5774 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005775
Chris Lattner071ad012006-04-17 05:28:54 +00005776 if (EltNo == 8) {
5777 EltNo = ByteSource/4;
5778 } else if (EltNo != ByteSource/4) {
5779 isFourElementShuffle = false;
5780 break;
5781 }
5782 }
5783 PFIndexes[i] = EltNo;
5784 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005785
5786 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00005787 // perfect shuffle vector to determine if it is cost effective to do this as
5788 // discrete instructions, or whether we should use a vperm.
5789 if (isFourElementShuffle) {
5790 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005791 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00005792 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005793
Chris Lattner071ad012006-04-17 05:28:54 +00005794 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5795 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005796
Chris Lattner071ad012006-04-17 05:28:54 +00005797 // Determining when to avoid vperm is tricky. Many things affect the cost
5798 // of vperm, particularly how many times the perm mask needs to be computed.
5799 // For example, if the perm mask can be hoisted out of a loop or is already
5800 // used (perhaps because there are multiple permutes with the same shuffle
5801 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5802 // the loop requires an extra register.
5803 //
5804 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00005805 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00005806 // available, if this block is within a loop, we should avoid using vperm
5807 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005808 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005809 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005810 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005811
Chris Lattner19e90552006-04-14 05:19:18 +00005812 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5813 // vector that will get spilled to the constant pool.
5814 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005815
Chris Lattner19e90552006-04-14 05:19:18 +00005816 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5817 // that it is in input element units, not in bytes. Convert now.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005818 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005819 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005820
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005821 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005822 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5823 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005824
Chris Lattner19e90552006-04-14 05:19:18 +00005825 for (unsigned j = 0; j != BytesPerElement; ++j)
5826 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson9f944592009-08-11 20:47:22 +00005827 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00005828 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005829
Owen Anderson9f944592009-08-11 20:47:22 +00005830 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Craig Topper48d114b2014-04-26 18:35:24 +00005831 ResultMask);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005832 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00005833}
5834
Chris Lattner9754d142006-04-18 17:59:36 +00005835/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5836/// altivec comparison. If it is, return true and fill in Opc/isDot with
5837/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005838static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner9754d142006-04-18 17:59:36 +00005839 bool &isDot) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005840 unsigned IntrinsicID =
5841 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00005842 CompareOpc = -1;
5843 isDot = false;
5844 switch (IntrinsicID) {
5845 default: return false;
5846 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00005847 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5848 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5849 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5850 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5851 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5852 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5853 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5854 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5855 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5856 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5857 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5858 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5859 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005860
Chris Lattner4211ca92006-04-14 06:01:58 +00005861 // Normal Comparisons.
5862 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5863 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5864 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5865 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5866 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5867 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5868 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5869 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5870 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5871 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5872 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5873 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5874 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5875 }
Chris Lattner9754d142006-04-18 17:59:36 +00005876 return true;
5877}
5878
5879/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5880/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005881SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005882 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00005883 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5884 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005885 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00005886 int CompareOpc;
5887 bool isDot;
5888 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005889 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005890
Chris Lattner9754d142006-04-18 17:59:36 +00005891 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00005892 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00005893 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00005894 Op.getOperand(1), Op.getOperand(2),
5895 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00005896 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00005897 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005898
Chris Lattner4211ca92006-04-14 06:01:58 +00005899 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005900 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005901 Op.getOperand(2), // LHS
5902 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00005903 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005904 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005905 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00005906 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005907
Chris Lattner4211ca92006-04-14 06:01:58 +00005908 // Now that we have the comparison, emit a copy from the CR to a GPR.
5909 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00005910 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00005911 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00005912 CompNode.getValue(1));
5913
Chris Lattner4211ca92006-04-14 06:01:58 +00005914 // Unpack the result based on how the target uses it.
5915 unsigned BitNo; // Bit # of CR6.
5916 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00005917 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00005918 default: // Can't happen, don't crash on invalid number though.
5919 case 0: // Return the value of the EQ bit of CR6.
5920 BitNo = 0; InvertBit = false;
5921 break;
5922 case 1: // Return the inverted value of the EQ bit of CR6.
5923 BitNo = 0; InvertBit = true;
5924 break;
5925 case 2: // Return the value of the LT bit of CR6.
5926 BitNo = 2; InvertBit = false;
5927 break;
5928 case 3: // Return the inverted value of the LT bit of CR6.
5929 BitNo = 2; InvertBit = true;
5930 break;
5931 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005932
Chris Lattner4211ca92006-04-14 06:01:58 +00005933 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00005934 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5935 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005936 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00005937 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5938 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00005939
Chris Lattner4211ca92006-04-14 06:01:58 +00005940 // If we are supposed to, toggle the bit.
5941 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00005942 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5943 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005944 return Flags;
5945}
5946
Hal Finkel5c0d1452014-03-30 13:22:59 +00005947SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
5948 SelectionDAG &DAG) const {
5949 SDLoc dl(Op);
5950 // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int
5951 // instructions), but for smaller types, we need to first extend up to v2i32
5952 // before doing going farther.
5953 if (Op.getValueType() == MVT::v2i64) {
5954 EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
5955 if (ExtVT != MVT::v2i32) {
5956 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0));
5957 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op,
5958 DAG.getValueType(EVT::getVectorVT(*DAG.getContext(),
5959 ExtVT.getVectorElementType(), 4)));
5960 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op);
5961 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op,
5962 DAG.getValueType(MVT::v2i32));
5963 }
5964
5965 return Op;
5966 }
5967
5968 return SDValue();
5969}
5970
Scott Michelcf0da6c2009-02-17 22:15:04 +00005971SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005972 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005973 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00005974 // Create a stack slot that is 16-byte aligned.
5975 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00005976 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00005977 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005978 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005979
Chris Lattner4211ca92006-04-14 06:01:58 +00005980 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00005981 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00005982 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005983 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005984 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00005985 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005986 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005987}
5988
Dan Gohman21cea8a2010-04-17 15:26:15 +00005989SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005990 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005991 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005992 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005993
Owen Anderson9f944592009-08-11 20:47:22 +00005994 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
5995 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005996
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005997 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005998 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005999
Chris Lattner7e4398742006-04-18 03:43:48 +00006000 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00006001 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
6002 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
6003 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006004
Chris Lattner7e4398742006-04-18 03:43:48 +00006005 // Low parts multiplied together, generating 32-bit results (we ignore the
6006 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006007 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00006008 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006009
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006010 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00006011 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00006012 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006013 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006014 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00006015 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
6016 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006017 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006018
Owen Anderson9f944592009-08-11 20:47:22 +00006019 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00006020
Chris Lattner96d50482006-04-18 04:28:57 +00006021 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006022 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00006023 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006024 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006025
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006026 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006027 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00006028 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00006029 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006030
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006031 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006032 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00006033 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00006034 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006035
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006036 // Merge the results together.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006037 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006038 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006039 Ops[i*2 ] = 2*i+1;
6040 Ops[i*2+1] = 2*i+1+16;
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006041 }
Owen Anderson9f944592009-08-11 20:47:22 +00006042 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00006043 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006044 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00006045 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006046}
6047
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006048/// LowerOperation - Provide custom lowering hooks for some operations.
6049///
Dan Gohman21cea8a2010-04-17 15:26:15 +00006050SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006051 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006052 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00006053 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00006054 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006055 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00006056 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00006057 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006058 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00006059 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
6060 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006061 case ISD::VASTART:
Dan Gohman31ae5862010-04-17 14:41:14 +00006062 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006063
6064 case ISD::VAARG:
Dan Gohman31ae5862010-04-17 14:41:14 +00006065 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00006066
Roman Divackyc3825df2013-07-25 21:36:47 +00006067 case ISD::VACOPY:
6068 return LowerVACOPY(Op, DAG, PPCSubTarget);
6069
Jim Laskeye4f4d042006-12-04 22:04:42 +00006070 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00006071 case ISD::DYNAMIC_STACKALLOC:
6072 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng51096af2008-04-19 01:30:48 +00006073
Hal Finkel756810f2013-03-21 21:37:52 +00006074 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
6075 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
6076
Hal Finkel940ab932014-02-28 00:27:01 +00006077 case ISD::LOAD: return LowerLOAD(Op, DAG);
6078 case ISD::STORE: return LowerSTORE(Op, DAG);
6079 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006080 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006081 case ISD::FP_TO_UINT:
6082 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006083 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00006084 case ISD::UINT_TO_FP:
6085 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00006086 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006087
Chris Lattner4211ca92006-04-14 06:01:58 +00006088 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00006089 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
6090 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
6091 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006092
Chris Lattner4211ca92006-04-14 06:01:58 +00006093 // Vector-related lowering.
6094 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6095 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6096 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6097 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Hal Finkel5c0d1452014-03-30 13:22:59 +00006098 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006099 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006100
Hal Finkel25c19922013-05-15 21:37:41 +00006101 // For counter-based loop handling.
6102 case ISD::INTRINSIC_W_CHAIN: return SDValue();
6103
Chris Lattnerf6a81562007-12-08 06:59:59 +00006104 // Frame & Return address.
6105 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00006106 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00006107 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006108}
6109
Duncan Sands6ed40142008-12-01 11:39:25 +00006110void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
6111 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006112 SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00006113 const TargetMachine &TM = getTargetMachine();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006114 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00006115 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00006116 default:
Craig Toppere55c5562012-02-07 02:50:20 +00006117 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkel25c19922013-05-15 21:37:41 +00006118 case ISD::INTRINSIC_W_CHAIN: {
6119 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
6120 Intrinsic::ppc_is_decremented_ctr_nonzero)
6121 break;
6122
6123 assert(N->getValueType(0) == MVT::i1 &&
6124 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00006125 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00006126 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
6127 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
6128 N->getOperand(1));
6129
6130 Results.push_back(NewInt);
6131 Results.push_back(NewInt.getValue(1));
6132 break;
6133 }
Roman Divacky4394e682011-06-28 15:30:42 +00006134 case ISD::VAARG: {
6135 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
6136 || TM.getSubtarget<PPCSubtarget>().isPPC64())
6137 return;
6138
6139 EVT VT = N->getValueType(0);
6140
6141 if (VT == MVT::i64) {
6142 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
6143
6144 Results.push_back(NewNode);
6145 Results.push_back(NewNode.getValue(1));
6146 }
6147 return;
6148 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006149 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00006150 assert(N->getValueType(0) == MVT::ppcf128);
6151 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006152 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006153 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006154 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00006155 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006156 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006157 DAG.getIntPtrConstant(1));
6158
Ulrich Weigand874fc622013-03-26 10:56:22 +00006159 // Add the two halves of the long double in round-to-zero mode.
6160 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00006161
6162 // We know the low half is about to be thrown away, so just use something
6163 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00006164 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00006165 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00006166 return;
Duncan Sands2a287912008-07-19 16:26:02 +00006167 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006168 case ISD::FP_TO_SINT:
Bill Schmidt41221692013-07-09 18:50:20 +00006169 // LowerFP_TO_INT() can only handle f32 and f64.
6170 if (N->getOperand(0).getValueType() == MVT::ppcf128)
6171 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006172 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00006173 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00006174 }
6175}
6176
6177
Chris Lattner4211ca92006-04-14 06:01:58 +00006178//===----------------------------------------------------------------------===//
6179// Other Lowering Code
6180//===----------------------------------------------------------------------===//
6181
Chris Lattner9b577f12005-08-26 21:23:58 +00006182MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00006183PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman747e55b2009-02-07 16:15:20 +00006184 bool is64bit, unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006185 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesend4eb0522008-08-25 22:34:37 +00006186 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6187
6188 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6189 MachineFunction *F = BB->getParent();
6190 MachineFunction::iterator It = BB;
6191 ++It;
6192
6193 unsigned dest = MI->getOperand(0).getReg();
6194 unsigned ptrA = MI->getOperand(1).getReg();
6195 unsigned ptrB = MI->getOperand(2).getReg();
6196 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006197 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00006198
6199 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6200 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6201 F->insert(It, loopMBB);
6202 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006203 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006204 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006205 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006206
6207 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006208 unsigned TmpReg = (!BinOpcode) ? incr :
6209 RegInfo.createVirtualRegister(
Dale Johannesenbc698292008-09-02 20:30:23 +00006210 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6211 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006212
6213 // thisMBB:
6214 // ...
6215 // fallthrough --> loopMBB
6216 BB->addSuccessor(loopMBB);
6217
6218 // loopMBB:
6219 // l[wd]arx dest, ptr
6220 // add r0, dest, incr
6221 // st[wd]cx. r0, ptr
6222 // bne- loopMBB
6223 // fallthrough --> exitMBB
6224 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006225 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00006226 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006227 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006228 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
6229 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesend4eb0522008-08-25 22:34:37 +00006230 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006231 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006232 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006233 BB->addSuccessor(loopMBB);
6234 BB->addSuccessor(exitMBB);
6235
6236 // exitMBB:
6237 // ...
6238 BB = exitMBB;
6239 return BB;
6240}
6241
6242MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00006243PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00006244 MachineBasicBlock *BB,
6245 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00006246 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006247 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesena32affb2008-08-28 17:53:09 +00006248 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6249 // In 64 bit mode we have to use 64 bits for addresses, even though the
6250 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
6251 // registers without caring whether they're 32 or 64, but here we're
6252 // doing actual arithmetic on the addresses.
6253 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00006254 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00006255
6256 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6257 MachineFunction *F = BB->getParent();
6258 MachineFunction::iterator It = BB;
6259 ++It;
6260
6261 unsigned dest = MI->getOperand(0).getReg();
6262 unsigned ptrA = MI->getOperand(1).getReg();
6263 unsigned ptrB = MI->getOperand(2).getReg();
6264 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006265 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00006266
6267 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6268 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6269 F->insert(It, loopMBB);
6270 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006271 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006272 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006273 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006274
6275 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006276 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006277 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6278 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00006279 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6280 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6281 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6282 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
6283 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6284 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6285 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6286 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6287 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
6288 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006289 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006290 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006291 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006292
6293 // thisMBB:
6294 // ...
6295 // fallthrough --> loopMBB
6296 BB->addSuccessor(loopMBB);
6297
6298 // The 4-byte load must be aligned, while a char or short may be
6299 // anywhere in the word. Hence all this nasty bookkeeping code.
6300 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6301 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006302 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00006303 // rlwinm ptr, ptr1, 0, 0, 29
6304 // slw incr2, incr, shift
6305 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6306 // slw mask, mask2, shift
6307 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00006308 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006309 // add tmp, tmpDest, incr2
6310 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00006311 // and tmp3, tmp, mask
6312 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00006313 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00006314 // bne- loopMBB
6315 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006316 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006317 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00006318 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006319 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006320 .addReg(ptrA).addReg(ptrB);
6321 } else {
6322 Ptr1Reg = ptrB;
6323 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006324 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006325 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006326 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006327 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6328 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006329 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006330 .addReg(Ptr1Reg).addImm(0).addImm(61);
6331 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006332 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006333 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006334 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006335 .addReg(incr).addReg(ShiftReg);
6336 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006337 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00006338 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006339 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6340 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00006341 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006342 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006343 .addReg(Mask2Reg).addReg(ShiftReg);
6344
6345 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006346 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006347 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006348 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006349 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006350 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006351 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006352 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006353 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006354 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006355 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006356 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00006357 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006358 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006359 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006360 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006361 BB->addSuccessor(loopMBB);
6362 BB->addSuccessor(exitMBB);
6363
6364 // exitMBB:
6365 // ...
6366 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006367 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6368 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00006369 return BB;
6370}
6371
Hal Finkel756810f2013-03-21 21:37:52 +00006372llvm::MachineBasicBlock*
6373PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6374 MachineBasicBlock *MBB) const {
6375 DebugLoc DL = MI->getDebugLoc();
6376 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6377
6378 MachineFunction *MF = MBB->getParent();
6379 MachineRegisterInfo &MRI = MF->getRegInfo();
6380
6381 const BasicBlock *BB = MBB->getBasicBlock();
6382 MachineFunction::iterator I = MBB;
6383 ++I;
6384
6385 // Memory Reference
6386 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6387 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6388
6389 unsigned DstReg = MI->getOperand(0).getReg();
6390 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6391 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6392 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6393 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6394
6395 MVT PVT = getPointerTy();
6396 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6397 "Invalid Pointer Size!");
6398 // For v = setjmp(buf), we generate
6399 //
6400 // thisMBB:
6401 // SjLjSetup mainMBB
6402 // bl mainMBB
6403 // v_restore = 1
6404 // b sinkMBB
6405 //
6406 // mainMBB:
6407 // buf[LabelOffset] = LR
6408 // v_main = 0
6409 //
6410 // sinkMBB:
6411 // v = phi(main, restore)
6412 //
6413
6414 MachineBasicBlock *thisMBB = MBB;
6415 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6416 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6417 MF->insert(I, mainMBB);
6418 MF->insert(I, sinkMBB);
6419
6420 MachineInstrBuilder MIB;
6421
6422 // Transfer the remainder of BB and its successor edges to sinkMBB.
6423 sinkMBB->splice(sinkMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006424 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Hal Finkel756810f2013-03-21 21:37:52 +00006425 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6426
6427 // Note that the structure of the jmp_buf used here is not compatible
6428 // with that used by libc, and is not designed to be. Specifically, it
6429 // stores only those 'reserved' registers that LLVM does not otherwise
6430 // understand how to spill. Also, by convention, by the time this
6431 // intrinsic is called, Clang has already stored the frame address in the
6432 // first slot of the buffer and stack address in the third. Following the
6433 // X86 target code, we'll store the jump address in the second slot. We also
6434 // need to save the TOC pointer (R2) to handle jumps between shared
6435 // libraries, and that will be stored in the fourth slot. The thread
6436 // identifier (R13) is not affected.
6437
6438 // thisMBB:
6439 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6440 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006441 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006442
6443 // Prepare IP either in reg.
6444 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6445 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6446 unsigned BufReg = MI->getOperand(1).getReg();
6447
6448 if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) {
6449 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6450 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006451 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006452 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006453 MIB.setMemRefs(MMOBegin, MMOEnd);
6454 }
6455
Hal Finkelf05d6c72013-07-17 23:50:51 +00006456 // Naked functions never have a base pointer, and so we use r1. For all
6457 // other functions, this decision must be delayed until during PEI.
6458 unsigned BaseReg;
6459 if (MF->getFunction()->getAttributes().hasAttribute(
6460 AttributeSet::FunctionIndex, Attribute::Naked))
6461 BaseReg = PPCSubTarget.isPPC64() ? PPC::X1 : PPC::R1;
6462 else
6463 BaseReg = PPCSubTarget.isPPC64() ? PPC::BP8 : PPC::BP;
6464
6465 MIB = BuildMI(*thisMBB, MI, DL,
6466 TII->get(PPCSubTarget.isPPC64() ? PPC::STD : PPC::STW))
6467 .addReg(BaseReg)
6468 .addImm(BPOffset)
6469 .addReg(BufReg);
6470 MIB.setMemRefs(MMOBegin, MMOEnd);
6471
Hal Finkel756810f2013-03-21 21:37:52 +00006472 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00006473 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling5e7656b2013-06-07 07:55:53 +00006474 const PPCRegisterInfo *TRI =
6475 static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo());
6476 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00006477
6478 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6479
6480 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6481 .addMBB(mainMBB);
6482 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6483
6484 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6485 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6486
6487 // mainMBB:
6488 // mainDstReg = 0
6489 MIB = BuildMI(mainMBB, DL,
6490 TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
6491
6492 // Store IP
6493 if (PPCSubTarget.isPPC64()) {
6494 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6495 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006496 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006497 .addReg(BufReg);
6498 } else {
6499 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6500 .addReg(LabelReg)
6501 .addImm(LabelOffset)
6502 .addReg(BufReg);
6503 }
6504
6505 MIB.setMemRefs(MMOBegin, MMOEnd);
6506
6507 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6508 mainMBB->addSuccessor(sinkMBB);
6509
6510 // sinkMBB:
6511 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6512 TII->get(PPC::PHI), DstReg)
6513 .addReg(mainDstReg).addMBB(mainMBB)
6514 .addReg(restoreDstReg).addMBB(thisMBB);
6515
6516 MI->eraseFromParent();
6517 return sinkMBB;
6518}
6519
6520MachineBasicBlock *
6521PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6522 MachineBasicBlock *MBB) const {
6523 DebugLoc DL = MI->getDebugLoc();
6524 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6525
6526 MachineFunction *MF = MBB->getParent();
6527 MachineRegisterInfo &MRI = MF->getRegInfo();
6528
6529 // Memory Reference
6530 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6531 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6532
6533 MVT PVT = getPointerTy();
6534 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6535 "Invalid Pointer Size!");
6536
6537 const TargetRegisterClass *RC =
6538 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6539 unsigned Tmp = MRI.createVirtualRegister(RC);
6540 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6541 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6542 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006543 unsigned BP = (PVT == MVT::i64) ? PPC::X30 : PPC::R30;
Hal Finkel756810f2013-03-21 21:37:52 +00006544
6545 MachineInstrBuilder MIB;
6546
6547 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6548 const int64_t SPOffset = 2 * PVT.getStoreSize();
6549 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006550 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006551
6552 unsigned BufReg = MI->getOperand(0).getReg();
6553
6554 // Reload FP (the jumped-to function may not have had a
6555 // frame pointer, and if so, then its r31 will be restored
6556 // as necessary).
6557 if (PVT == MVT::i64) {
6558 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6559 .addImm(0)
6560 .addReg(BufReg);
6561 } else {
6562 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6563 .addImm(0)
6564 .addReg(BufReg);
6565 }
6566 MIB.setMemRefs(MMOBegin, MMOEnd);
6567
6568 // Reload IP
6569 if (PVT == MVT::i64) {
6570 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006571 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006572 .addReg(BufReg);
6573 } else {
6574 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6575 .addImm(LabelOffset)
6576 .addReg(BufReg);
6577 }
6578 MIB.setMemRefs(MMOBegin, MMOEnd);
6579
6580 // Reload SP
6581 if (PVT == MVT::i64) {
6582 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006583 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006584 .addReg(BufReg);
6585 } else {
6586 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6587 .addImm(SPOffset)
6588 .addReg(BufReg);
6589 }
6590 MIB.setMemRefs(MMOBegin, MMOEnd);
6591
Hal Finkelf05d6c72013-07-17 23:50:51 +00006592 // Reload BP
6593 if (PVT == MVT::i64) {
6594 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
6595 .addImm(BPOffset)
6596 .addReg(BufReg);
6597 } else {
6598 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
6599 .addImm(BPOffset)
6600 .addReg(BufReg);
6601 }
6602 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00006603
6604 // Reload TOC
6605 if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) {
6606 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006607 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006608 .addReg(BufReg);
6609
6610 MIB.setMemRefs(MMOBegin, MMOEnd);
6611 }
6612
6613 // Jump
6614 BuildMI(*MBB, MI, DL,
6615 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6616 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6617
6618 MI->eraseFromParent();
6619 return MBB;
6620}
6621
Dale Johannesena32affb2008-08-28 17:53:09 +00006622MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00006623PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00006624 MachineBasicBlock *BB) const {
Hal Finkel756810f2013-03-21 21:37:52 +00006625 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6626 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6627 return emitEHSjLjSetJmp(MI, BB);
6628 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6629 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6630 return emitEHSjLjLongJmp(MI, BB);
6631 }
6632
Evan Cheng20350c42006-11-27 23:37:22 +00006633 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00006634
6635 // To "insert" these instructions we actually have to insert their
6636 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00006637 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00006638 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00006639 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00006640
Dan Gohman3b460302008-07-07 23:14:23 +00006641 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00006642
Hal Finkel460e94d2012-06-22 23:10:08 +00006643 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006644 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6645 MI->getOpcode() == PPC::SELECT_I4 ||
6646 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00006647 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00006648 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6649 MI->getOpcode() == PPC::SELECT_CC_I8)
6650 Cond.push_back(MI->getOperand(4));
6651 else
6652 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00006653 Cond.push_back(MI->getOperand(1));
6654
Hal Finkel460e94d2012-06-22 23:10:08 +00006655 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00006656 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6657 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
6658 Cond, MI->getOperand(2).getReg(),
6659 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00006660 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6661 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6662 MI->getOpcode() == PPC::SELECT_CC_F4 ||
6663 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006664 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
6665 MI->getOpcode() == PPC::SELECT_I4 ||
6666 MI->getOpcode() == PPC::SELECT_I8 ||
6667 MI->getOpcode() == PPC::SELECT_F4 ||
6668 MI->getOpcode() == PPC::SELECT_F8 ||
6669 MI->getOpcode() == PPC::SELECT_VRRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00006670 // The incoming instruction knows the destination vreg to set, the
6671 // condition code register to branch on, the true/false values to
6672 // select between, and a branch opcode to use.
6673
6674 // thisMBB:
6675 // ...
6676 // TrueVal = ...
6677 // cmpTY ccX, r1, r2
6678 // bCC copy1MBB
6679 // fallthrough --> copy0MBB
6680 MachineBasicBlock *thisMBB = BB;
6681 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6682 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006683 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006684 F->insert(It, copy0MBB);
6685 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006686
6687 // Transfer the remainder of BB and its successor edges to sinkMBB.
6688 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006689 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006690 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6691
Evan Cheng32e376f2008-07-12 02:23:19 +00006692 // Next, add the true and fallthrough blocks as its successors.
6693 BB->addSuccessor(copy0MBB);
6694 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006695
Hal Finkel940ab932014-02-28 00:27:01 +00006696 if (MI->getOpcode() == PPC::SELECT_I4 ||
6697 MI->getOpcode() == PPC::SELECT_I8 ||
6698 MI->getOpcode() == PPC::SELECT_F4 ||
6699 MI->getOpcode() == PPC::SELECT_F8 ||
6700 MI->getOpcode() == PPC::SELECT_VRRC) {
6701 BuildMI(BB, dl, TII->get(PPC::BC))
6702 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6703 } else {
6704 unsigned SelectPred = MI->getOperand(4).getImm();
6705 BuildMI(BB, dl, TII->get(PPC::BCC))
6706 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6707 }
Dan Gohman34396292010-07-06 20:24:04 +00006708
Evan Cheng32e376f2008-07-12 02:23:19 +00006709 // copy0MBB:
6710 // %FalseValue = ...
6711 // # fallthrough to sinkMBB
6712 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006713
Evan Cheng32e376f2008-07-12 02:23:19 +00006714 // Update machine-CFG edges
6715 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006716
Evan Cheng32e376f2008-07-12 02:23:19 +00006717 // sinkMBB:
6718 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6719 // ...
6720 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00006721 BuildMI(*BB, BB->begin(), dl,
6722 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00006723 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
6724 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6725 }
Dale Johannesena32affb2008-08-28 17:53:09 +00006726 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
6727 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
6728 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
6729 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006730 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
6731 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
6732 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
6733 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006734
6735 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
6736 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
6737 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
6738 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006739 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
6740 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
6741 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
6742 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006743
6744 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
6745 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
6746 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
6747 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006748 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
6749 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
6750 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
6751 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006752
6753 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
6754 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
6755 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
6756 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006757 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
6758 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
6759 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
6760 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006761
6762 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006763 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006764 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006765 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006766 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006767 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006768 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006769 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006770
6771 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6772 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6773 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6774 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006775 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6776 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6777 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6778 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006779
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006780 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6781 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6782 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6783 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6784 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6785 BB = EmitAtomicBinary(MI, BB, false, 0);
6786 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6787 BB = EmitAtomicBinary(MI, BB, true, 0);
6788
Evan Cheng32e376f2008-07-12 02:23:19 +00006789 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6790 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6791 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6792
6793 unsigned dest = MI->getOperand(0).getReg();
6794 unsigned ptrA = MI->getOperand(1).getReg();
6795 unsigned ptrB = MI->getOperand(2).getReg();
6796 unsigned oldval = MI->getOperand(3).getReg();
6797 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006798 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006799
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006800 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6801 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6802 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006803 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006804 F->insert(It, loop1MBB);
6805 F->insert(It, loop2MBB);
6806 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006807 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006808 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006809 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006810 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006811
6812 // thisMBB:
6813 // ...
6814 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006815 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006816
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006817 // loop1MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006818 // l[wd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006819 // cmp[wd] dest, oldval
6820 // bne- midMBB
6821 // loop2MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006822 // st[wd]cx. newval, ptr
6823 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006824 // b exitBB
6825 // midMBB:
6826 // st[wd]cx. dest, ptr
6827 // exitBB:
6828 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006829 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00006830 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006831 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00006832 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006833 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006834 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6835 BB->addSuccessor(loop2MBB);
6836 BB->addSuccessor(midMBB);
6837
6838 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006839 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng32e376f2008-07-12 02:23:19 +00006840 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006841 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006842 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006843 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006844 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006845 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006846
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006847 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006848 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006849 .addReg(dest).addReg(ptrA).addReg(ptrB);
6850 BB->addSuccessor(exitMBB);
6851
Evan Cheng32e376f2008-07-12 02:23:19 +00006852 // exitMBB:
6853 // ...
6854 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00006855 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6856 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6857 // We must use 64-bit registers for addresses when targeting 64-bit,
6858 // since we're actually doing arithmetic on them. Other registers
6859 // can be 32-bit.
6860 bool is64bit = PPCSubTarget.isPPC64();
6861 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6862
6863 unsigned dest = MI->getOperand(0).getReg();
6864 unsigned ptrA = MI->getOperand(1).getReg();
6865 unsigned ptrB = MI->getOperand(2).getReg();
6866 unsigned oldval = MI->getOperand(3).getReg();
6867 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006868 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00006869
6870 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6871 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6872 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6873 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6874 F->insert(It, loop1MBB);
6875 F->insert(It, loop2MBB);
6876 F->insert(It, midMBB);
6877 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006878 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006879 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006880 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006881
6882 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006883 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006884 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6885 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00006886 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6887 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6888 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6889 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6890 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6891 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6892 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6893 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6894 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6895 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6896 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6897 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6898 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6899 unsigned Ptr1Reg;
6900 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00006901 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00006902 // thisMBB:
6903 // ...
6904 // fallthrough --> loopMBB
6905 BB->addSuccessor(loop1MBB);
6906
6907 // The 4-byte load must be aligned, while a char or short may be
6908 // anywhere in the word. Hence all this nasty bookkeeping code.
6909 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6910 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006911 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00006912 // rlwinm ptr, ptr1, 0, 0, 29
6913 // slw newval2, newval, shift
6914 // slw oldval2, oldval,shift
6915 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6916 // slw mask, mask2, shift
6917 // and newval3, newval2, mask
6918 // and oldval3, oldval2, mask
6919 // loop1MBB:
6920 // lwarx tmpDest, ptr
6921 // and tmp, tmpDest, mask
6922 // cmpw tmp, oldval3
6923 // bne- midMBB
6924 // loop2MBB:
6925 // andc tmp2, tmpDest, mask
6926 // or tmp4, tmp2, newval3
6927 // stwcx. tmp4, ptr
6928 // bne- loop1MBB
6929 // b exitBB
6930 // midMBB:
6931 // stwcx. tmpDest, ptr
6932 // exitBB:
6933 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006934 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00006935 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006936 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006937 .addReg(ptrA).addReg(ptrB);
6938 } else {
6939 Ptr1Reg = ptrB;
6940 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006941 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006942 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006943 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006944 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6945 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006946 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006947 .addReg(Ptr1Reg).addImm(0).addImm(61);
6948 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006949 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006950 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006951 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006952 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006953 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006954 .addReg(oldval).addReg(ShiftReg);
6955 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006956 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00006957 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006958 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6959 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6960 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00006961 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006962 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006963 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006964 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006965 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006966 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006967 .addReg(OldVal2Reg).addReg(MaskReg);
6968
6969 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006970 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006971 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006972 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6973 .addReg(TmpDestReg).addReg(MaskReg);
6974 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00006975 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006976 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006977 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6978 BB->addSuccessor(loop2MBB);
6979 BB->addSuccessor(midMBB);
6980
6981 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006982 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6983 .addReg(TmpDestReg).addReg(MaskReg);
6984 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6985 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6986 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006987 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006988 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006989 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006990 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006991 BB->addSuccessor(loop1MBB);
6992 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006993
Dale Johannesen340d2642008-08-30 00:08:53 +00006994 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006995 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006996 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00006997 BB->addSuccessor(exitMBB);
6998
6999 // exitMBB:
7000 // ...
7001 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00007002 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
7003 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00007004 } else if (MI->getOpcode() == PPC::FADDrtz) {
7005 // This pseudo performs an FADD with rounding mode temporarily forced
7006 // to round-to-zero. We emit this via custom inserter since the FPSCR
7007 // is not modeled at the SelectionDAG level.
7008 unsigned Dest = MI->getOperand(0).getReg();
7009 unsigned Src1 = MI->getOperand(1).getReg();
7010 unsigned Src2 = MI->getOperand(2).getReg();
7011 DebugLoc dl = MI->getDebugLoc();
7012
7013 MachineRegisterInfo &RegInfo = F->getRegInfo();
7014 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
7015
7016 // Save FPSCR value.
7017 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
7018
7019 // Set rounding mode to round-to-zero.
7020 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
7021 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
7022
7023 // Perform addition.
7024 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
7025
7026 // Restore FPSCR value.
7027 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00007028 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
7029 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
7030 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
7031 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
7032 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
7033 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
7034 PPC::ANDIo8 : PPC::ANDIo;
7035 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
7036 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
7037
7038 MachineRegisterInfo &RegInfo = F->getRegInfo();
7039 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
7040 &PPC::GPRCRegClass :
7041 &PPC::G8RCRegClass);
7042
7043 DebugLoc dl = MI->getDebugLoc();
7044 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
7045 .addReg(MI->getOperand(1).getReg()).addImm(1);
7046 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
7047 MI->getOperand(0).getReg())
7048 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Dale Johannesen340d2642008-08-30 00:08:53 +00007049 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007050 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00007051 }
Chris Lattner9b577f12005-08-26 21:23:58 +00007052
Dan Gohman34396292010-07-06 20:24:04 +00007053 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00007054 return BB;
7055}
7056
Chris Lattner4211ca92006-04-14 06:01:58 +00007057//===----------------------------------------------------------------------===//
7058// Target Optimization Hooks
7059//===----------------------------------------------------------------------===//
7060
Hal Finkelb0c810f2013-04-03 17:44:56 +00007061SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
7062 DAGCombinerInfo &DCI) const {
Hal Finkel2e103312013-04-03 04:01:11 +00007063 if (DCI.isAfterLegalizeVectorOps())
7064 return SDValue();
7065
Hal Finkelb0c810f2013-04-03 17:44:56 +00007066 EVT VT = Op.getValueType();
7067
7068 if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) ||
7069 (VT == MVT::f64 && PPCSubTarget.hasFRE()) ||
Hal Finkel27774d92014-03-13 07:58:58 +00007070 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec()) ||
7071 (VT == MVT::v2f64 && PPCSubTarget.hasVSX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00007072
7073 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
7074 // For the reciprocal, we need to find the zero of the function:
7075 // F(X) = A X - 1 [which has a zero at X = 1/A]
7076 // =>
7077 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
7078 // does not require additional intermediate precision]
7079
7080 // Convergence is quadratic, so we essentially double the number of digits
7081 // correct after every iteration. The minimum architected relative
7082 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
7083 // 23 digits and double has 52 digits.
7084 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00007085 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00007086 ++Iterations;
7087
7088 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007089 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007090
7091 SDValue FPOne =
Hal Finkelb0c810f2013-04-03 17:44:56 +00007092 DAG.getConstantFP(1.0, VT.getScalarType());
7093 if (VT.isVector()) {
7094 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00007095 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00007096 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel2e103312013-04-03 04:01:11 +00007097 FPOne, FPOne, FPOne, FPOne);
7098 }
7099
Hal Finkelb0c810f2013-04-03 17:44:56 +00007100 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007101 DCI.AddToWorklist(Est.getNode());
7102
7103 // Newton iterations: Est = Est + Est (1 - Arg * Est)
7104 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007105 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00007106 DCI.AddToWorklist(NewEst.getNode());
7107
Hal Finkelb0c810f2013-04-03 17:44:56 +00007108 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007109 DCI.AddToWorklist(NewEst.getNode());
7110
Hal Finkelb0c810f2013-04-03 17:44:56 +00007111 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007112 DCI.AddToWorklist(NewEst.getNode());
7113
Hal Finkelb0c810f2013-04-03 17:44:56 +00007114 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007115 DCI.AddToWorklist(Est.getNode());
7116 }
7117
7118 return Est;
7119 }
7120
7121 return SDValue();
7122}
7123
Hal Finkelb0c810f2013-04-03 17:44:56 +00007124SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel2e103312013-04-03 04:01:11 +00007125 DAGCombinerInfo &DCI) const {
7126 if (DCI.isAfterLegalizeVectorOps())
7127 return SDValue();
7128
Hal Finkelb0c810f2013-04-03 17:44:56 +00007129 EVT VT = Op.getValueType();
7130
7131 if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) ||
7132 (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) ||
Hal Finkel27774d92014-03-13 07:58:58 +00007133 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec()) ||
7134 (VT == MVT::v2f64 && PPCSubTarget.hasVSX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00007135
7136 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
7137 // For the reciprocal sqrt, we need to find the zero of the function:
7138 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
7139 // =>
7140 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
7141 // As a result, we precompute A/2 prior to the iteration loop.
7142
7143 // Convergence is quadratic, so we essentially double the number of digits
7144 // correct after every iteration. The minimum architected relative
7145 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
7146 // 23 digits and double has 52 digits.
7147 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00007148 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00007149 ++Iterations;
7150
7151 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007152 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007153
Hal Finkelb0c810f2013-04-03 17:44:56 +00007154 SDValue FPThreeHalves =
7155 DAG.getConstantFP(1.5, VT.getScalarType());
7156 if (VT.isVector()) {
7157 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00007158 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00007159 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
7160 FPThreeHalves, FPThreeHalves,
7161 FPThreeHalves, FPThreeHalves);
Hal Finkel2e103312013-04-03 04:01:11 +00007162 }
7163
Hal Finkelb0c810f2013-04-03 17:44:56 +00007164 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007165 DCI.AddToWorklist(Est.getNode());
7166
7167 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
7168 // this entire sequence requires only one FP constant.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007169 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007170 DCI.AddToWorklist(HalfArg.getNode());
7171
Hal Finkelb0c810f2013-04-03 17:44:56 +00007172 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007173 DCI.AddToWorklist(HalfArg.getNode());
7174
7175 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
7176 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007177 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00007178 DCI.AddToWorklist(NewEst.getNode());
7179
Hal Finkelb0c810f2013-04-03 17:44:56 +00007180 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007181 DCI.AddToWorklist(NewEst.getNode());
7182
Hal Finkelb0c810f2013-04-03 17:44:56 +00007183 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007184 DCI.AddToWorklist(NewEst.getNode());
7185
Hal Finkelb0c810f2013-04-03 17:44:56 +00007186 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007187 DCI.AddToWorklist(Est.getNode());
7188 }
7189
7190 return Est;
7191 }
7192
7193 return SDValue();
7194}
7195
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007196// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
7197// not enforce equality of the chain operands.
7198static bool isConsecutiveLS(LSBaseSDNode *LS, LSBaseSDNode *Base,
7199 unsigned Bytes, int Dist,
7200 SelectionDAG &DAG) {
7201 EVT VT = LS->getMemoryVT();
7202 if (VT.getSizeInBits() / 8 != Bytes)
7203 return false;
7204
7205 SDValue Loc = LS->getBasePtr();
7206 SDValue BaseLoc = Base->getBasePtr();
7207 if (Loc.getOpcode() == ISD::FrameIndex) {
7208 if (BaseLoc.getOpcode() != ISD::FrameIndex)
7209 return false;
7210 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7211 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
7212 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
7213 int FS = MFI->getObjectSize(FI);
7214 int BFS = MFI->getObjectSize(BFI);
7215 if (FS != BFS || FS != (int)Bytes) return false;
7216 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
7217 }
7218
7219 // Handle X+C
7220 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
7221 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
7222 return true;
7223
7224 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Craig Topper062a2ba2014-04-25 05:30:21 +00007225 const GlobalValue *GV1 = nullptr;
7226 const GlobalValue *GV2 = nullptr;
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007227 int64_t Offset1 = 0;
7228 int64_t Offset2 = 0;
7229 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
7230 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
7231 if (isGA1 && isGA2 && GV1 == GV2)
7232 return Offset1 == (Offset2 + Dist*Bytes);
7233 return false;
7234}
7235
Hal Finkel7d8a6912013-05-26 18:08:30 +00007236// Return true is there is a nearyby consecutive load to the one provided
7237// (regardless of alignment). We search up and down the chain, looking though
7238// token factors and other loads (but nothing else). As a result, a true
7239// results indicates that it is safe to create a new consecutive load adjacent
7240// to the load provided.
7241static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
7242 SDValue Chain = LD->getChain();
7243 EVT VT = LD->getMemoryVT();
7244
7245 SmallSet<SDNode *, 16> LoadRoots;
7246 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
7247 SmallSet<SDNode *, 16> Visited;
7248
7249 // First, search up the chain, branching to follow all token-factor operands.
7250 // If we find a consecutive load, then we're done, otherwise, record all
7251 // nodes just above the top-level loads and token factors.
7252 while (!Queue.empty()) {
7253 SDNode *ChainNext = Queue.pop_back_val();
7254 if (!Visited.insert(ChainNext))
7255 continue;
7256
7257 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007258 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007259 return true;
7260
7261 if (!Visited.count(ChainLD->getChain().getNode()))
7262 Queue.push_back(ChainLD->getChain().getNode());
7263 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
7264 for (SDNode::op_iterator O = ChainNext->op_begin(),
7265 OE = ChainNext->op_end(); O != OE; ++O)
7266 if (!Visited.count(O->getNode()))
7267 Queue.push_back(O->getNode());
7268 } else
7269 LoadRoots.insert(ChainNext);
7270 }
7271
7272 // Second, search down the chain, starting from the top-level nodes recorded
7273 // in the first phase. These top-level nodes are the nodes just above all
7274 // loads and token factors. Starting with their uses, recursively look though
7275 // all loads (just the chain uses) and token factors to find a consecutive
7276 // load.
7277 Visited.clear();
7278 Queue.clear();
7279
7280 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
7281 IE = LoadRoots.end(); I != IE; ++I) {
7282 Queue.push_back(*I);
7283
7284 while (!Queue.empty()) {
7285 SDNode *LoadRoot = Queue.pop_back_val();
7286 if (!Visited.insert(LoadRoot))
7287 continue;
7288
7289 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007290 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007291 return true;
7292
7293 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
7294 UE = LoadRoot->use_end(); UI != UE; ++UI)
7295 if (((isa<LoadSDNode>(*UI) &&
7296 cast<LoadSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
7297 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
7298 Queue.push_back(*UI);
7299 }
7300 }
7301
7302 return false;
7303}
7304
Hal Finkel940ab932014-02-28 00:27:01 +00007305SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
7306 DAGCombinerInfo &DCI) const {
7307 SelectionDAG &DAG = DCI.DAG;
7308 SDLoc dl(N);
7309
7310 assert(PPCSubTarget.useCRBits() &&
7311 "Expecting to be tracking CR bits");
7312 // If we're tracking CR bits, we need to be careful that we don't have:
7313 // trunc(binary-ops(zext(x), zext(y)))
7314 // or
7315 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
7316 // such that we're unnecessarily moving things into GPRs when it would be
7317 // better to keep them in CR bits.
7318
7319 // Note that trunc here can be an actual i1 trunc, or can be the effective
7320 // truncation that comes from a setcc or select_cc.
7321 if (N->getOpcode() == ISD::TRUNCATE &&
7322 N->getValueType(0) != MVT::i1)
7323 return SDValue();
7324
7325 if (N->getOperand(0).getValueType() != MVT::i32 &&
7326 N->getOperand(0).getValueType() != MVT::i64)
7327 return SDValue();
7328
7329 if (N->getOpcode() == ISD::SETCC ||
7330 N->getOpcode() == ISD::SELECT_CC) {
7331 // If we're looking at a comparison, then we need to make sure that the
7332 // high bits (all except for the first) don't matter the result.
7333 ISD::CondCode CC =
7334 cast<CondCodeSDNode>(N->getOperand(
7335 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
7336 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
7337
7338 if (ISD::isSignedIntSetCC(CC)) {
7339 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
7340 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
7341 return SDValue();
7342 } else if (ISD::isUnsignedIntSetCC(CC)) {
7343 if (!DAG.MaskedValueIsZero(N->getOperand(0),
7344 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
7345 !DAG.MaskedValueIsZero(N->getOperand(1),
7346 APInt::getHighBitsSet(OpBits, OpBits-1)))
7347 return SDValue();
7348 } else {
7349 // This is neither a signed nor an unsigned comparison, just make sure
7350 // that the high bits are equal.
7351 APInt Op1Zero, Op1One;
7352 APInt Op2Zero, Op2One;
Jay Foada0653a32014-05-14 21:14:37 +00007353 DAG.computeKnownBits(N->getOperand(0), Op1Zero, Op1One);
7354 DAG.computeKnownBits(N->getOperand(1), Op2Zero, Op2One);
Hal Finkel940ab932014-02-28 00:27:01 +00007355
7356 // We don't really care about what is known about the first bit (if
7357 // anything), so clear it in all masks prior to comparing them.
7358 Op1Zero.clearBit(0); Op1One.clearBit(0);
7359 Op2Zero.clearBit(0); Op2One.clearBit(0);
7360
7361 if (Op1Zero != Op2Zero || Op1One != Op2One)
7362 return SDValue();
7363 }
7364 }
7365
7366 // We now know that the higher-order bits are irrelevant, we just need to
7367 // make sure that all of the intermediate operations are bit operations, and
7368 // all inputs are extensions.
7369 if (N->getOperand(0).getOpcode() != ISD::AND &&
7370 N->getOperand(0).getOpcode() != ISD::OR &&
7371 N->getOperand(0).getOpcode() != ISD::XOR &&
7372 N->getOperand(0).getOpcode() != ISD::SELECT &&
7373 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
7374 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
7375 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
7376 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
7377 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
7378 return SDValue();
7379
7380 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
7381 N->getOperand(1).getOpcode() != ISD::AND &&
7382 N->getOperand(1).getOpcode() != ISD::OR &&
7383 N->getOperand(1).getOpcode() != ISD::XOR &&
7384 N->getOperand(1).getOpcode() != ISD::SELECT &&
7385 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
7386 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
7387 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
7388 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
7389 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
7390 return SDValue();
7391
7392 SmallVector<SDValue, 4> Inputs;
7393 SmallVector<SDValue, 8> BinOps, PromOps;
7394 SmallPtrSet<SDNode *, 16> Visited;
7395
7396 for (unsigned i = 0; i < 2; ++i) {
7397 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7398 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7399 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7400 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7401 isa<ConstantSDNode>(N->getOperand(i)))
7402 Inputs.push_back(N->getOperand(i));
7403 else
7404 BinOps.push_back(N->getOperand(i));
7405
7406 if (N->getOpcode() == ISD::TRUNCATE)
7407 break;
7408 }
7409
7410 // Visit all inputs, collect all binary operations (and, or, xor and
7411 // select) that are all fed by extensions.
7412 while (!BinOps.empty()) {
7413 SDValue BinOp = BinOps.back();
7414 BinOps.pop_back();
7415
7416 if (!Visited.insert(BinOp.getNode()))
7417 continue;
7418
7419 PromOps.push_back(BinOp);
7420
7421 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7422 // The condition of the select is not promoted.
7423 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7424 continue;
7425 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7426 continue;
7427
7428 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7429 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7430 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7431 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7432 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7433 Inputs.push_back(BinOp.getOperand(i));
7434 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7435 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7436 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7437 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7438 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
7439 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7440 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7441 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7442 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
7443 BinOps.push_back(BinOp.getOperand(i));
7444 } else {
7445 // We have an input that is not an extension or another binary
7446 // operation; we'll abort this transformation.
7447 return SDValue();
7448 }
7449 }
7450 }
7451
7452 // Make sure that this is a self-contained cluster of operations (which
7453 // is not quite the same thing as saying that everything has only one
7454 // use).
7455 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7456 if (isa<ConstantSDNode>(Inputs[i]))
7457 continue;
7458
7459 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7460 UE = Inputs[i].getNode()->use_end();
7461 UI != UE; ++UI) {
7462 SDNode *User = *UI;
7463 if (User != N && !Visited.count(User))
7464 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007465
7466 // Make sure that we're not going to promote the non-output-value
7467 // operand(s) or SELECT or SELECT_CC.
7468 // FIXME: Although we could sometimes handle this, and it does occur in
7469 // practice that one of the condition inputs to the select is also one of
7470 // the outputs, we currently can't deal with this.
7471 if (User->getOpcode() == ISD::SELECT) {
7472 if (User->getOperand(0) == Inputs[i])
7473 return SDValue();
7474 } else if (User->getOpcode() == ISD::SELECT_CC) {
7475 if (User->getOperand(0) == Inputs[i] ||
7476 User->getOperand(1) == Inputs[i])
7477 return SDValue();
7478 }
Hal Finkel940ab932014-02-28 00:27:01 +00007479 }
7480 }
7481
7482 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7483 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7484 UE = PromOps[i].getNode()->use_end();
7485 UI != UE; ++UI) {
7486 SDNode *User = *UI;
7487 if (User != N && !Visited.count(User))
7488 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007489
7490 // Make sure that we're not going to promote the non-output-value
7491 // operand(s) or SELECT or SELECT_CC.
7492 // FIXME: Although we could sometimes handle this, and it does occur in
7493 // practice that one of the condition inputs to the select is also one of
7494 // the outputs, we currently can't deal with this.
7495 if (User->getOpcode() == ISD::SELECT) {
7496 if (User->getOperand(0) == PromOps[i])
7497 return SDValue();
7498 } else if (User->getOpcode() == ISD::SELECT_CC) {
7499 if (User->getOperand(0) == PromOps[i] ||
7500 User->getOperand(1) == PromOps[i])
7501 return SDValue();
7502 }
Hal Finkel940ab932014-02-28 00:27:01 +00007503 }
7504 }
7505
7506 // Replace all inputs with the extension operand.
7507 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7508 // Constants may have users outside the cluster of to-be-promoted nodes,
7509 // and so we need to replace those as we do the promotions.
7510 if (isa<ConstantSDNode>(Inputs[i]))
7511 continue;
7512 else
7513 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
7514 }
7515
7516 // Replace all operations (these are all the same, but have a different
7517 // (i1) return type). DAG.getNode will validate that the types of
7518 // a binary operator match, so go through the list in reverse so that
7519 // we've likely promoted both operands first. Any intermediate truncations or
7520 // extensions disappear.
7521 while (!PromOps.empty()) {
7522 SDValue PromOp = PromOps.back();
7523 PromOps.pop_back();
7524
7525 if (PromOp.getOpcode() == ISD::TRUNCATE ||
7526 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
7527 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
7528 PromOp.getOpcode() == ISD::ANY_EXTEND) {
7529 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
7530 PromOp.getOperand(0).getValueType() != MVT::i1) {
7531 // The operand is not yet ready (see comment below).
7532 PromOps.insert(PromOps.begin(), PromOp);
7533 continue;
7534 }
7535
7536 SDValue RepValue = PromOp.getOperand(0);
7537 if (isa<ConstantSDNode>(RepValue))
7538 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
7539
7540 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
7541 continue;
7542 }
7543
7544 unsigned C;
7545 switch (PromOp.getOpcode()) {
7546 default: C = 0; break;
7547 case ISD::SELECT: C = 1; break;
7548 case ISD::SELECT_CC: C = 2; break;
7549 }
7550
7551 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7552 PromOp.getOperand(C).getValueType() != MVT::i1) ||
7553 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7554 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
7555 // The to-be-promoted operands of this node have not yet been
7556 // promoted (this should be rare because we're going through the
7557 // list backward, but if one of the operands has several users in
7558 // this cluster of to-be-promoted nodes, it is possible).
7559 PromOps.insert(PromOps.begin(), PromOp);
7560 continue;
7561 }
7562
7563 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7564 PromOp.getNode()->op_end());
7565
7566 // If there are any constant inputs, make sure they're replaced now.
7567 for (unsigned i = 0; i < 2; ++i)
7568 if (isa<ConstantSDNode>(Ops[C+i]))
7569 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
7570
7571 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00007572 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00007573 }
7574
7575 // Now we're left with the initial truncation itself.
7576 if (N->getOpcode() == ISD::TRUNCATE)
7577 return N->getOperand(0);
7578
7579 // Otherwise, this is a comparison. The operands to be compared have just
7580 // changed type (to i1), but everything else is the same.
7581 return SDValue(N, 0);
7582}
7583
7584SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
7585 DAGCombinerInfo &DCI) const {
7586 SelectionDAG &DAG = DCI.DAG;
7587 SDLoc dl(N);
7588
Hal Finkel940ab932014-02-28 00:27:01 +00007589 // If we're tracking CR bits, we need to be careful that we don't have:
7590 // zext(binary-ops(trunc(x), trunc(y)))
7591 // or
7592 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
7593 // such that we're unnecessarily moving things into CR bits that can more
7594 // efficiently stay in GPRs. Note that if we're not certain that the high
7595 // bits are set as required by the final extension, we still may need to do
7596 // some masking to get the proper behavior.
7597
Hal Finkel46043ed2014-03-01 21:36:57 +00007598 // This same functionality is important on PPC64 when dealing with
7599 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
7600 // the return values of functions. Because it is so similar, it is handled
7601 // here as well.
7602
Hal Finkel940ab932014-02-28 00:27:01 +00007603 if (N->getValueType(0) != MVT::i32 &&
7604 N->getValueType(0) != MVT::i64)
7605 return SDValue();
7606
Hal Finkel46043ed2014-03-01 21:36:57 +00007607 if (!((N->getOperand(0).getValueType() == MVT::i1 &&
7608 PPCSubTarget.useCRBits()) ||
7609 (N->getOperand(0).getValueType() == MVT::i32 &&
7610 PPCSubTarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +00007611 return SDValue();
7612
7613 if (N->getOperand(0).getOpcode() != ISD::AND &&
7614 N->getOperand(0).getOpcode() != ISD::OR &&
7615 N->getOperand(0).getOpcode() != ISD::XOR &&
7616 N->getOperand(0).getOpcode() != ISD::SELECT &&
7617 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
7618 return SDValue();
7619
7620 SmallVector<SDValue, 4> Inputs;
7621 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
7622 SmallPtrSet<SDNode *, 16> Visited;
7623
7624 // Visit all inputs, collect all binary operations (and, or, xor and
7625 // select) that are all fed by truncations.
7626 while (!BinOps.empty()) {
7627 SDValue BinOp = BinOps.back();
7628 BinOps.pop_back();
7629
7630 if (!Visited.insert(BinOp.getNode()))
7631 continue;
7632
7633 PromOps.push_back(BinOp);
7634
7635 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7636 // The condition of the select is not promoted.
7637 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7638 continue;
7639 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7640 continue;
7641
7642 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7643 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7644 Inputs.push_back(BinOp.getOperand(i));
7645 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7646 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7647 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7648 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7649 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
7650 BinOps.push_back(BinOp.getOperand(i));
7651 } else {
7652 // We have an input that is not a truncation or another binary
7653 // operation; we'll abort this transformation.
7654 return SDValue();
7655 }
7656 }
7657 }
7658
7659 // Make sure that this is a self-contained cluster of operations (which
7660 // is not quite the same thing as saying that everything has only one
7661 // use).
7662 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7663 if (isa<ConstantSDNode>(Inputs[i]))
7664 continue;
7665
7666 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7667 UE = Inputs[i].getNode()->use_end();
7668 UI != UE; ++UI) {
7669 SDNode *User = *UI;
7670 if (User != N && !Visited.count(User))
7671 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007672
7673 // Make sure that we're not going to promote the non-output-value
7674 // operand(s) or SELECT or SELECT_CC.
7675 // FIXME: Although we could sometimes handle this, and it does occur in
7676 // practice that one of the condition inputs to the select is also one of
7677 // the outputs, we currently can't deal with this.
7678 if (User->getOpcode() == ISD::SELECT) {
7679 if (User->getOperand(0) == Inputs[i])
7680 return SDValue();
7681 } else if (User->getOpcode() == ISD::SELECT_CC) {
7682 if (User->getOperand(0) == Inputs[i] ||
7683 User->getOperand(1) == Inputs[i])
7684 return SDValue();
7685 }
Hal Finkel940ab932014-02-28 00:27:01 +00007686 }
7687 }
7688
7689 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7690 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7691 UE = PromOps[i].getNode()->use_end();
7692 UI != UE; ++UI) {
7693 SDNode *User = *UI;
7694 if (User != N && !Visited.count(User))
7695 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007696
7697 // Make sure that we're not going to promote the non-output-value
7698 // operand(s) or SELECT or SELECT_CC.
7699 // FIXME: Although we could sometimes handle this, and it does occur in
7700 // practice that one of the condition inputs to the select is also one of
7701 // the outputs, we currently can't deal with this.
7702 if (User->getOpcode() == ISD::SELECT) {
7703 if (User->getOperand(0) == PromOps[i])
7704 return SDValue();
7705 } else if (User->getOpcode() == ISD::SELECT_CC) {
7706 if (User->getOperand(0) == PromOps[i] ||
7707 User->getOperand(1) == PromOps[i])
7708 return SDValue();
7709 }
Hal Finkel940ab932014-02-28 00:27:01 +00007710 }
7711 }
7712
Hal Finkel46043ed2014-03-01 21:36:57 +00007713 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +00007714 bool ReallyNeedsExt = false;
7715 if (N->getOpcode() != ISD::ANY_EXTEND) {
7716 // If all of the inputs are not already sign/zero extended, then
7717 // we'll still need to do that at the end.
7718 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7719 if (isa<ConstantSDNode>(Inputs[i]))
7720 continue;
7721
7722 unsigned OpBits =
7723 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +00007724 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
7725
Hal Finkel940ab932014-02-28 00:27:01 +00007726 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
7727 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00007728 APInt::getHighBitsSet(OpBits,
7729 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +00007730 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +00007731 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
7732 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +00007733 ReallyNeedsExt = true;
7734 break;
7735 }
7736 }
7737 }
7738
7739 // Replace all inputs, either with the truncation operand, or a
7740 // truncation or extension to the final output type.
7741 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7742 // Constant inputs need to be replaced with the to-be-promoted nodes that
7743 // use them because they might have users outside of the cluster of
7744 // promoted nodes.
7745 if (isa<ConstantSDNode>(Inputs[i]))
7746 continue;
7747
7748 SDValue InSrc = Inputs[i].getOperand(0);
7749 if (Inputs[i].getValueType() == N->getValueType(0))
7750 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
7751 else if (N->getOpcode() == ISD::SIGN_EXTEND)
7752 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7753 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
7754 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7755 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7756 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
7757 else
7758 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7759 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
7760 }
7761
7762 // Replace all operations (these are all the same, but have a different
7763 // (promoted) return type). DAG.getNode will validate that the types of
7764 // a binary operator match, so go through the list in reverse so that
7765 // we've likely promoted both operands first.
7766 while (!PromOps.empty()) {
7767 SDValue PromOp = PromOps.back();
7768 PromOps.pop_back();
7769
7770 unsigned C;
7771 switch (PromOp.getOpcode()) {
7772 default: C = 0; break;
7773 case ISD::SELECT: C = 1; break;
7774 case ISD::SELECT_CC: C = 2; break;
7775 }
7776
7777 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7778 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
7779 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7780 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
7781 // The to-be-promoted operands of this node have not yet been
7782 // promoted (this should be rare because we're going through the
7783 // list backward, but if one of the operands has several users in
7784 // this cluster of to-be-promoted nodes, it is possible).
7785 PromOps.insert(PromOps.begin(), PromOp);
7786 continue;
7787 }
7788
7789 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7790 PromOp.getNode()->op_end());
7791
7792 // If this node has constant inputs, then they'll need to be promoted here.
7793 for (unsigned i = 0; i < 2; ++i) {
7794 if (!isa<ConstantSDNode>(Ops[C+i]))
7795 continue;
7796 if (Ops[C+i].getValueType() == N->getValueType(0))
7797 continue;
7798
7799 if (N->getOpcode() == ISD::SIGN_EXTEND)
7800 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7801 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7802 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7803 else
7804 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7805 }
7806
7807 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00007808 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00007809 }
7810
7811 // Now we're left with the initial extension itself.
7812 if (!ReallyNeedsExt)
7813 return N->getOperand(0);
7814
Hal Finkel46043ed2014-03-01 21:36:57 +00007815 // To zero extend, just mask off everything except for the first bit (in the
7816 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +00007817 if (N->getOpcode() == ISD::ZERO_EXTEND)
7818 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00007819 DAG.getConstant(APInt::getLowBitsSet(
7820 N->getValueSizeInBits(0), PromBits),
7821 N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +00007822
7823 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
7824 "Invalid extension type");
7825 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0));
7826 SDValue ShiftCst =
Hal Finkel46043ed2014-03-01 21:36:57 +00007827 DAG.getConstant(N->getValueSizeInBits(0)-PromBits, ShiftAmountTy);
Hal Finkel940ab932014-02-28 00:27:01 +00007828 return DAG.getNode(ISD::SRA, dl, N->getValueType(0),
7829 DAG.getNode(ISD::SHL, dl, N->getValueType(0),
7830 N->getOperand(0), ShiftCst), ShiftCst);
7831}
7832
Duncan Sandsdc2dac12008-11-24 14:53:14 +00007833SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
7834 DAGCombinerInfo &DCI) const {
Dan Gohman57c732b2010-04-21 01:34:56 +00007835 const TargetMachine &TM = getTargetMachine();
Chris Lattnerf4184352006-03-01 04:57:39 +00007836 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007837 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00007838 switch (N->getOpcode()) {
7839 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00007840 case PPCISD::SHL:
7841 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007842 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007843 return N->getOperand(0);
7844 }
7845 break;
7846 case PPCISD::SRL:
7847 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007848 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007849 return N->getOperand(0);
7850 }
7851 break;
7852 case PPCISD::SRA:
7853 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007854 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007855 C->isAllOnesValue()) // -1 >>s V -> -1.
7856 return N->getOperand(0);
7857 }
7858 break;
Hal Finkel940ab932014-02-28 00:27:01 +00007859 case ISD::SIGN_EXTEND:
7860 case ISD::ZERO_EXTEND:
7861 case ISD::ANY_EXTEND:
7862 return DAGCombineExtBoolTrunc(N, DCI);
7863 case ISD::TRUNCATE:
7864 case ISD::SETCC:
7865 case ISD::SELECT_CC:
7866 return DAGCombineTruncBoolExt(N, DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007867 case ISD::FDIV: {
7868 assert(TM.Options.UnsafeFPMath &&
7869 "Reciprocal estimates require UnsafeFPMath");
Scott Michelcf0da6c2009-02-17 22:15:04 +00007870
Hal Finkel2e103312013-04-03 04:01:11 +00007871 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007872 SDValue RV =
7873 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007874 if (RV.getNode()) {
Hal Finkel2e103312013-04-03 04:01:11 +00007875 DCI.AddToWorklist(RV.getNode());
7876 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7877 N->getOperand(0), RV);
7878 }
Hal Finkelf96c18e2013-04-04 22:44:12 +00007879 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
7880 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7881 SDValue RV =
7882 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7883 DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007884 if (RV.getNode()) {
Hal Finkelf96c18e2013-04-04 22:44:12 +00007885 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007886 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007887 N->getValueType(0), RV);
7888 DCI.AddToWorklist(RV.getNode());
7889 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7890 N->getOperand(0), RV);
7891 }
7892 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
7893 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7894 SDValue RV =
7895 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7896 DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007897 if (RV.getNode()) {
Hal Finkelf96c18e2013-04-04 22:44:12 +00007898 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007899 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007900 N->getValueType(0), RV,
7901 N->getOperand(1).getOperand(1));
7902 DCI.AddToWorklist(RV.getNode());
7903 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7904 N->getOperand(0), RV);
7905 }
Hal Finkel2e103312013-04-03 04:01:11 +00007906 }
7907
Hal Finkelb0c810f2013-04-03 17:44:56 +00007908 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007909 if (RV.getNode()) {
Hal Finkel2e103312013-04-03 04:01:11 +00007910 DCI.AddToWorklist(RV.getNode());
7911 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7912 N->getOperand(0), RV);
7913 }
7914
7915 }
7916 break;
7917 case ISD::FSQRT: {
7918 assert(TM.Options.UnsafeFPMath &&
7919 "Reciprocal estimates require UnsafeFPMath");
7920
7921 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
7922 // reciprocal sqrt.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007923 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007924 if (RV.getNode()) {
Hal Finkel2e103312013-04-03 04:01:11 +00007925 DCI.AddToWorklist(RV.getNode());
Hal Finkelb0c810f2013-04-03 17:44:56 +00007926 RV = DAGCombineFastRecip(RV, DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007927 if (RV.getNode()) {
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007928 // Unfortunately, RV is now NaN if the input was exactly 0. Select out
7929 // this case and force the answer to 0.
7930
7931 EVT VT = RV.getValueType();
7932
7933 SDValue Zero = DAG.getConstantFP(0.0, VT.getScalarType());
7934 if (VT.isVector()) {
7935 assert(VT.getVectorNumElements() == 4 && "Unknown vector type");
7936 Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Zero, Zero, Zero, Zero);
7937 }
7938
7939 SDValue ZeroCmp =
7940 DAG.getSetCC(dl, getSetCCResultType(*DAG.getContext(), VT),
7941 N->getOperand(0), Zero, ISD::SETEQ);
7942 DCI.AddToWorklist(ZeroCmp.getNode());
7943 DCI.AddToWorklist(RV.getNode());
7944
7945 RV = DAG.getNode(VT.isVector() ? ISD::VSELECT : ISD::SELECT, dl, VT,
7946 ZeroCmp, Zero, RV);
Hal Finkel2e103312013-04-03 04:01:11 +00007947 return RV;
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007948 }
Hal Finkel2e103312013-04-03 04:01:11 +00007949 }
7950
7951 }
7952 break;
Chris Lattnerf4184352006-03-01 04:57:39 +00007953 case ISD::SINT_TO_FP:
Chris Lattnera35f3062006-06-16 17:34:12 +00007954 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007955 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
7956 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
7957 // We allow the src/dst to be either f32/f64, but the intermediate
7958 // type must be i64.
Owen Anderson9f944592009-08-11 20:47:22 +00007959 if (N->getOperand(0).getValueType() == MVT::i64 &&
7960 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007961 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007962 if (Val.getValueType() == MVT::f32) {
7963 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007964 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007965 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007966
Owen Anderson9f944592009-08-11 20:47:22 +00007967 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007968 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007969 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007970 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007971 if (N->getValueType(0) == MVT::f32) {
7972 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner72733e52008-01-17 07:00:52 +00007973 DAG.getIntPtrConstant(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007974 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007975 }
7976 return Val;
Owen Anderson9f944592009-08-11 20:47:22 +00007977 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007978 // If the intermediate type is i32, we can avoid the load/store here
7979 // too.
Chris Lattnerf4184352006-03-01 04:57:39 +00007980 }
Chris Lattnerf4184352006-03-01 04:57:39 +00007981 }
7982 }
7983 break;
Chris Lattner27f53452006-03-01 05:50:56 +00007984 case ISD::STORE:
7985 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
7986 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnerf5b46f72008-01-18 16:54:56 +00007987 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00007988 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00007989 N->getOperand(1).getValueType() == MVT::i32 &&
7990 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007991 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007992 if (Val.getValueType() == MVT::f32) {
7993 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007994 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007995 }
Owen Anderson9f944592009-08-11 20:47:22 +00007996 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007997 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007998
Hal Finkel60c75102013-04-01 15:37:53 +00007999 SDValue Ops[] = {
8000 N->getOperand(0), Val, N->getOperand(2),
8001 DAG.getValueType(N->getOperand(1).getValueType())
8002 };
8003
8004 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00008005 DAG.getVTList(MVT::Other), Ops,
Hal Finkel60c75102013-04-01 15:37:53 +00008006 cast<StoreSDNode>(N)->getMemoryVT(),
8007 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00008008 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008009 return Val;
8010 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008011
Chris Lattnera7976d32006-07-10 20:56:58 +00008012 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00008013 if (cast<StoreSDNode>(N)->isUnindexed() &&
8014 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00008015 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00008016 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00008017 N->getOperand(1).getValueType() == MVT::i16 ||
8018 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008019 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008020 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008021 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008022 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00008023 if (BSwapOp.getValueType() == MVT::i16)
8024 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00008025
Dan Gohman48b185d2009-09-25 20:36:54 +00008026 SDValue Ops[] = {
8027 N->getOperand(0), BSwapOp, N->getOperand(2),
8028 DAG.getValueType(N->getOperand(1).getValueType())
8029 };
8030 return
8031 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00008032 Ops, cast<StoreSDNode>(N)->getMemoryVT(),
Dan Gohman48b185d2009-09-25 20:36:54 +00008033 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008034 }
8035 break;
Hal Finkelcf2e9082013-05-24 23:00:14 +00008036 case ISD::LOAD: {
8037 LoadSDNode *LD = cast<LoadSDNode>(N);
8038 EVT VT = LD->getValueType(0);
8039 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
8040 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
8041 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
8042 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
Hal Finkel40c34782013-09-15 22:09:58 +00008043 (VT == MVT::v16i8 || VT == MVT::v8i16 ||
8044 VT == MVT::v4i32 || VT == MVT::v4f32) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +00008045 LD->getAlignment() < ABIAlignment) {
8046 // This is a type-legal unaligned Altivec load.
8047 SDValue Chain = LD->getChain();
8048 SDValue Ptr = LD->getBasePtr();
8049
8050 // This implements the loading of unaligned vectors as described in
8051 // the venerable Apple Velocity Engine overview. Specifically:
8052 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
8053 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
8054 //
8055 // The general idea is to expand a sequence of one or more unaligned
8056 // loads into a alignment-based permutation-control instruction (lvsl),
8057 // a series of regular vector loads (which always truncate their
8058 // input address to an aligned address), and a series of permutations.
8059 // The results of these permutations are the requested loaded values.
8060 // The trick is that the last "extra" load is not taken from the address
8061 // you might suspect (sizeof(vector) bytes after the last requested
8062 // load), but rather sizeof(vector) - 1 bytes after the last
8063 // requested vector. The point of this is to avoid a page fault if the
Alp Tokercb402912014-01-24 17:20:08 +00008064 // base address happened to be aligned. This works because if the base
Hal Finkelcf2e9082013-05-24 23:00:14 +00008065 // address is aligned, then adding less than a full vector length will
8066 // cause the last vector in the sequence to be (re)loaded. Otherwise,
8067 // the next vector will be fetched as you might suspect was necessary.
8068
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008069 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00008070 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008071 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
8072 // optimization later.
Hal Finkelcf2e9082013-05-24 23:00:14 +00008073 SDValue PermCntl = BuildIntrinsicOp(Intrinsic::ppc_altivec_lvsl, Ptr,
8074 DAG, dl, MVT::v16i8);
8075
8076 // Refine the alignment of the original load (a "new" load created here
8077 // which was identical to the first except for the alignment would be
8078 // merged with the existing node regardless).
8079 MachineFunction &MF = DAG.getMachineFunction();
8080 MachineMemOperand *MMO =
8081 MF.getMachineMemOperand(LD->getPointerInfo(),
8082 LD->getMemOperand()->getFlags(),
8083 LD->getMemoryVT().getStoreSize(),
8084 ABIAlignment);
8085 LD->refineAlignment(MMO);
8086 SDValue BaseLoad = SDValue(LD, 0);
8087
8088 // Note that the value of IncOffset (which is provided to the next
8089 // load's pointer info offset value, and thus used to calculate the
8090 // alignment), and the value of IncValue (which is actually used to
8091 // increment the pointer value) are different! This is because we
8092 // require the next load to appear to be aligned, even though it
8093 // is actually offset from the base pointer by a lesser amount.
8094 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +00008095 int IncValue = IncOffset;
8096
8097 // Walk (both up and down) the chain looking for another load at the real
8098 // (aligned) offset (the alignment of the other load does not matter in
8099 // this case). If found, then do not use the offset reduction trick, as
8100 // that will prevent the loads from being later combined (as they would
8101 // otherwise be duplicates).
8102 if (!findConsecutiveLoad(LD, DAG))
8103 --IncValue;
8104
Hal Finkelcf2e9082013-05-24 23:00:14 +00008105 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
8106 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
8107
Hal Finkelcf2e9082013-05-24 23:00:14 +00008108 SDValue ExtraLoad =
8109 DAG.getLoad(VT, dl, Chain, Ptr,
8110 LD->getPointerInfo().getWithOffset(IncOffset),
8111 LD->isVolatile(), LD->isNonTemporal(),
8112 LD->isInvariant(), ABIAlignment);
8113
8114 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8115 BaseLoad.getValue(1), ExtraLoad.getValue(1));
8116
8117 if (BaseLoad.getValueType() != MVT::v4i32)
8118 BaseLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, BaseLoad);
8119
8120 if (ExtraLoad.getValueType() != MVT::v4i32)
8121 ExtraLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ExtraLoad);
8122
8123 SDValue Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
8124 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
8125
8126 if (VT != MVT::v4i32)
8127 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
8128
8129 // Now we need to be really careful about how we update the users of the
8130 // original load. We cannot just call DCI.CombineTo (or
8131 // DAG.ReplaceAllUsesWith for that matter), because the load still has
8132 // uses created here (the permutation for example) that need to stay.
8133 SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
8134 while (UI != UE) {
8135 SDUse &Use = UI.getUse();
8136 SDNode *User = *UI;
8137 // Note: BaseLoad is checked here because it might not be N, but a
8138 // bitcast of N.
8139 if (User == Perm.getNode() || User == BaseLoad.getNode() ||
8140 User == TF.getNode() || Use.getResNo() > 1) {
8141 ++UI;
8142 continue;
8143 }
8144
8145 SDValue To = Use.getResNo() ? TF : Perm;
8146 ++UI;
8147
8148 SmallVector<SDValue, 8> Ops;
8149 for (SDNode::op_iterator O = User->op_begin(),
8150 OE = User->op_end(); O != OE; ++O) {
8151 if (*O == Use)
8152 Ops.push_back(To);
8153 else
8154 Ops.push_back(*O);
8155 }
8156
Craig Topper8c0b4d02014-04-28 05:57:50 +00008157 DAG.UpdateNodeOperands(User, Ops);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008158 }
8159
8160 return SDValue(N, 0);
8161 }
8162 }
8163 break;
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008164 case ISD::INTRINSIC_WO_CHAIN:
8165 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() ==
8166 Intrinsic::ppc_altivec_lvsl &&
8167 N->getOperand(1)->getOpcode() == ISD::ADD) {
8168 SDValue Add = N->getOperand(1);
8169
8170 if (DAG.MaskedValueIsZero(Add->getOperand(1),
8171 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
8172 Add.getValueType().getScalarType().getSizeInBits()))) {
8173 SDNode *BasePtr = Add->getOperand(0).getNode();
8174 for (SDNode::use_iterator UI = BasePtr->use_begin(),
8175 UE = BasePtr->use_end(); UI != UE; ++UI) {
8176 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8177 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
8178 Intrinsic::ppc_altivec_lvsl) {
8179 // We've found another LVSL, and this address if an aligned
8180 // multiple of that one. The results will be the same, so use the
8181 // one we've just found instead.
8182
8183 return SDValue(*UI, 0);
8184 }
8185 }
8186 }
8187 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +00008188
8189 break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008190 case ISD::BSWAP:
8191 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008192 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +00008193 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008194 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
8195 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008196 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008197 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008198 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00008199 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +00008200 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008201 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +00008202 LD->getChain(), // Chain
8203 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008204 DAG.getValueType(N->getValueType(0)) // VT
8205 };
Dan Gohman48b185d2009-09-25 20:36:54 +00008206 SDValue BSLoad =
8207 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +00008208 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
8209 MVT::i64 : MVT::i32, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00008210 Ops, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008211
Scott Michelcf0da6c2009-02-17 22:15:04 +00008212 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008213 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +00008214 if (N->getValueType(0) == MVT::i16)
8215 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008216
Chris Lattnera7976d32006-07-10 20:56:58 +00008217 // First, combine the bswap away. This makes the value produced by the
8218 // load dead.
8219 DCI.CombineTo(N, ResVal);
8220
8221 // Next, combine the load away, we give it a bogus result value but a real
8222 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008223 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00008224
Chris Lattnera7976d32006-07-10 20:56:58 +00008225 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008226 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008227 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008228
Chris Lattner27f53452006-03-01 05:50:56 +00008229 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008230 case PPCISD::VCMP: {
8231 // If a VCMPo node already exists with exactly the same operands as this
8232 // node, use its result instead of this node (VCMPo computes both a CR6 and
8233 // a normal output).
8234 //
8235 if (!N->getOperand(0).hasOneUse() &&
8236 !N->getOperand(1).hasOneUse() &&
8237 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00008238
Chris Lattnerd4058a52006-03-31 06:02:07 +00008239 // Scan all of the users of the LHS, looking for VCMPo's that match.
Craig Topper062a2ba2014-04-25 05:30:21 +00008240 SDNode *VCMPoNode = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008241
Gabor Greiff304a7a2008-08-28 21:40:38 +00008242 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +00008243 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
8244 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008245 if (UI->getOpcode() == PPCISD::VCMPo &&
8246 UI->getOperand(1) == N->getOperand(1) &&
8247 UI->getOperand(2) == N->getOperand(2) &&
8248 UI->getOperand(0) == N->getOperand(0)) {
8249 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008250 break;
8251 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008252
Chris Lattner518834c2006-04-18 18:28:22 +00008253 // If there is no VCMPo node, or if the flag value has a single use, don't
8254 // transform this.
8255 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
8256 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008257
8258 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +00008259 // chain, this transformation is more complex. Note that multiple things
8260 // could use the value result, which we should ignore.
Craig Topper062a2ba2014-04-25 05:30:21 +00008261 SDNode *FlagUser = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008262 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Craig Topper062a2ba2014-04-25 05:30:21 +00008263 FlagUser == nullptr; ++UI) {
Chris Lattner518834c2006-04-18 18:28:22 +00008264 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008265 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +00008266 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008267 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +00008268 FlagUser = User;
8269 break;
8270 }
8271 }
8272 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008273
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008274 // If the user is a MFOCRF instruction, we know this is safe.
8275 // Otherwise we give up for right now.
8276 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008277 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +00008278 }
8279 break;
8280 }
Hal Finkel940ab932014-02-28 00:27:01 +00008281 case ISD::BRCOND: {
8282 SDValue Cond = N->getOperand(1);
8283 SDValue Target = N->getOperand(2);
8284
8285 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8286 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
8287 Intrinsic::ppc_is_decremented_ctr_nonzero) {
8288
8289 // We now need to make the intrinsic dead (it cannot be instruction
8290 // selected).
8291 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
8292 assert(Cond.getNode()->hasOneUse() &&
8293 "Counter decrement has more than one use");
8294
8295 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
8296 N->getOperand(0), Target);
8297 }
8298 }
8299 break;
Chris Lattner9754d142006-04-18 17:59:36 +00008300 case ISD::BR_CC: {
8301 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008302 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +00008303 // lowering is done pre-legalize, because the legalizer lowers the predicate
8304 // compare down to code that is difficult to reassemble.
8305 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008306 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +00008307
8308 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
8309 // value. If so, pass-through the AND to get to the intrinsic.
8310 if (LHS.getOpcode() == ISD::AND &&
8311 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8312 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
8313 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8314 isa<ConstantSDNode>(LHS.getOperand(1)) &&
8315 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
8316 isZero())
8317 LHS = LHS.getOperand(0);
8318
8319 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8320 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
8321 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8322 isa<ConstantSDNode>(RHS)) {
8323 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
8324 "Counter decrement comparison is not EQ or NE");
8325
8326 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
8327 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
8328 (CC == ISD::SETNE && !Val);
8329
8330 // We now need to make the intrinsic dead (it cannot be instruction
8331 // selected).
8332 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
8333 assert(LHS.getNode()->hasOneUse() &&
8334 "Counter decrement has more than one use");
8335
8336 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
8337 N->getOperand(0), N->getOperand(4));
8338 }
8339
Chris Lattner9754d142006-04-18 17:59:36 +00008340 int CompareOpc;
8341 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008342
Chris Lattner9754d142006-04-18 17:59:36 +00008343 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8344 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
8345 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
8346 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00008347
Chris Lattner9754d142006-04-18 17:59:36 +00008348 // If this is a comparison against something other than 0/1, then we know
8349 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008350 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00008351 if (Val != 0 && Val != 1) {
8352 if (CC == ISD::SETEQ) // Cond never true, remove branch.
8353 return N->getOperand(0);
8354 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +00008355 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +00008356 N->getOperand(0), N->getOperand(4));
8357 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008358
Chris Lattner9754d142006-04-18 17:59:36 +00008359 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008360
Chris Lattner9754d142006-04-18 17:59:36 +00008361 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008362 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008363 LHS.getOperand(2), // LHS of compare
8364 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +00008365 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008366 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00008367 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00008368 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008369
Chris Lattner9754d142006-04-18 17:59:36 +00008370 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008371 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +00008372 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +00008373 default: // Can't happen, don't crash on invalid number though.
8374 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008375 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +00008376 break;
8377 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008378 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +00008379 break;
8380 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008381 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +00008382 break;
8383 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008384 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +00008385 break;
8386 }
8387
Owen Anderson9f944592009-08-11 20:47:22 +00008388 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
8389 DAG.getConstant(CompOpc, MVT::i32),
8390 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +00008391 N->getOperand(4), CompNode.getValue(1));
8392 }
8393 break;
8394 }
Chris Lattnerf4184352006-03-01 04:57:39 +00008395 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008396
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008397 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +00008398}
8399
Chris Lattner4211ca92006-04-14 06:01:58 +00008400//===----------------------------------------------------------------------===//
8401// Inline Assembly Support
8402//===----------------------------------------------------------------------===//
8403
Jay Foada0653a32014-05-14 21:14:37 +00008404void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
8405 APInt &KnownZero,
8406 APInt &KnownOne,
8407 const SelectionDAG &DAG,
8408 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00008409 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +00008410 switch (Op.getOpcode()) {
8411 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008412 case PPCISD::LBRX: {
8413 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +00008414 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +00008415 KnownZero = 0xFFFF0000;
8416 break;
8417 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008418 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00008419 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +00008420 default: break;
8421 case Intrinsic::ppc_altivec_vcmpbfp_p:
8422 case Intrinsic::ppc_altivec_vcmpeqfp_p:
8423 case Intrinsic::ppc_altivec_vcmpequb_p:
8424 case Intrinsic::ppc_altivec_vcmpequh_p:
8425 case Intrinsic::ppc_altivec_vcmpequw_p:
8426 case Intrinsic::ppc_altivec_vcmpgefp_p:
8427 case Intrinsic::ppc_altivec_vcmpgtfp_p:
8428 case Intrinsic::ppc_altivec_vcmpgtsb_p:
8429 case Intrinsic::ppc_altivec_vcmpgtsh_p:
8430 case Intrinsic::ppc_altivec_vcmpgtsw_p:
8431 case Intrinsic::ppc_altivec_vcmpgtub_p:
8432 case Intrinsic::ppc_altivec_vcmpgtuh_p:
8433 case Intrinsic::ppc_altivec_vcmpgtuw_p:
8434 KnownZero = ~1U; // All bits but the low one are known to be zero.
8435 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008436 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008437 }
8438 }
8439}
8440
8441
Chris Lattnerd6855142007-03-25 02:14:49 +00008442/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +00008443/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008444PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00008445PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
8446 if (Constraint.size() == 1) {
8447 switch (Constraint[0]) {
8448 default: break;
8449 case 'b':
8450 case 'r':
8451 case 'f':
8452 case 'v':
8453 case 'y':
8454 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +00008455 case 'Z':
8456 // FIXME: While Z does indicate a memory constraint, it specifically
8457 // indicates an r+r address (used in conjunction with the 'y' modifier
8458 // in the replacement string). Currently, we're forcing the base
8459 // register to be r0 in the asm printer (which is interpreted as zero)
8460 // and forming the complete address in the second register. This is
8461 // suboptimal.
8462 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +00008463 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008464 } else if (Constraint == "wc") { // individual CR bits.
8465 return C_RegisterClass;
Hal Finkel27774d92014-03-13 07:58:58 +00008466 } else if (Constraint == "wa" || Constraint == "wd" ||
8467 Constraint == "wf" || Constraint == "ws") {
8468 return C_RegisterClass; // VSX registers.
Chris Lattnerd6855142007-03-25 02:14:49 +00008469 }
8470 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +00008471}
8472
John Thompsone8360b72010-10-29 17:29:13 +00008473/// Examine constraint type and operand type and determine a weight value.
8474/// This object must already have been set up with the operand type
8475/// and the current alternative constraint selected.
8476TargetLowering::ConstraintWeight
8477PPCTargetLowering::getSingleConstraintMatchWeight(
8478 AsmOperandInfo &info, const char *constraint) const {
8479 ConstraintWeight weight = CW_Invalid;
8480 Value *CallOperandVal = info.CallOperandVal;
8481 // If we don't have a value, we can't do a match,
8482 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00008483 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00008484 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00008485 Type *type = CallOperandVal->getType();
Hal Finkel6aca2372014-03-02 18:23:39 +00008486
John Thompsone8360b72010-10-29 17:29:13 +00008487 // Look at the constraint type.
Hal Finkel6aca2372014-03-02 18:23:39 +00008488 if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
8489 return CW_Register; // an individual CR bit.
Hal Finkel27774d92014-03-13 07:58:58 +00008490 else if ((StringRef(constraint) == "wa" ||
8491 StringRef(constraint) == "wd" ||
8492 StringRef(constraint) == "wf") &&
8493 type->isVectorTy())
8494 return CW_Register;
8495 else if (StringRef(constraint) == "ws" && type->isDoubleTy())
8496 return CW_Register;
Hal Finkel6aca2372014-03-02 18:23:39 +00008497
John Thompsone8360b72010-10-29 17:29:13 +00008498 switch (*constraint) {
8499 default:
8500 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8501 break;
8502 case 'b':
8503 if (type->isIntegerTy())
8504 weight = CW_Register;
8505 break;
8506 case 'f':
8507 if (type->isFloatTy())
8508 weight = CW_Register;
8509 break;
8510 case 'd':
8511 if (type->isDoubleTy())
8512 weight = CW_Register;
8513 break;
8514 case 'v':
8515 if (type->isVectorTy())
8516 weight = CW_Register;
8517 break;
8518 case 'y':
8519 weight = CW_Register;
8520 break;
Hal Finkel4f24c622012-11-05 18:18:42 +00008521 case 'Z':
8522 weight = CW_Memory;
8523 break;
John Thompsone8360b72010-10-29 17:29:13 +00008524 }
8525 return weight;
8526}
8527
Scott Michelcf0da6c2009-02-17 22:15:04 +00008528std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner584a11a2006-11-02 01:44:04 +00008529PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00008530 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +00008531 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +00008532 // GCC RS6000 Constraint Letters
8533 switch (Constraint[0]) {
8534 case 'b': // R1-R31
Hal Finkel638a9fa2013-03-19 18:51:05 +00008535 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
8536 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
8537 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008538 case 'r': // R0-R31
Owen Anderson9f944592009-08-11 20:47:22 +00008539 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +00008540 return std::make_pair(0U, &PPC::G8RCRegClass);
8541 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008542 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008543 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +00008544 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008545 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +00008546 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008547 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008548 case 'v':
Craig Topperabadc662012-04-20 06:31:50 +00008549 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008550 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +00008551 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008552 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008553 } else if (Constraint == "wc") { // an individual CR bit.
8554 return std::make_pair(0U, &PPC::CRBITRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +00008555 } else if (Constraint == "wa" || Constraint == "wd" ||
Hal Finkel19be5062014-03-29 05:29:01 +00008556 Constraint == "wf") {
Hal Finkel27774d92014-03-13 07:58:58 +00008557 return std::make_pair(0U, &PPC::VSRCRegClass);
Hal Finkel19be5062014-03-29 05:29:01 +00008558 } else if (Constraint == "ws") {
8559 return std::make_pair(0U, &PPC::VSFRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008560 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008561
Hal Finkelb176acb2013-08-03 12:25:10 +00008562 std::pair<unsigned, const TargetRegisterClass*> R =
8563 TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8564
8565 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
8566 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
8567 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
8568 // register.
8569 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
8570 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
8571 if (R.first && VT == MVT::i64 && PPCSubTarget.isPPC64() &&
8572 PPC::GPRCRegClass.contains(R.first)) {
8573 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
8574 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +00008575 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +00008576 &PPC::G8RCRegClass);
8577 }
8578
8579 return R;
Chris Lattner01513612006-01-31 19:20:21 +00008580}
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008581
Chris Lattner584a11a2006-11-02 01:44:04 +00008582
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008583/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +00008584/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +00008585void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00008586 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008587 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00008588 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00008589 SDValue Result;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008590
Eric Christopherde9399b2011-06-02 23:16:42 +00008591 // Only support length 1 constraints.
8592 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008593
Eric Christopherde9399b2011-06-02 23:16:42 +00008594 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008595 switch (Letter) {
8596 default: break;
8597 case 'I':
8598 case 'J':
8599 case 'K':
8600 case 'L':
8601 case 'M':
8602 case 'N':
8603 case 'O':
8604 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +00008605 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008606 if (!CST) return; // Must be an immediate to match.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008607 unsigned Value = CST->getZExtValue();
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008608 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008609 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008610 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008611 if ((short)Value == (int)Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008612 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008613 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008614 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
8615 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008616 if ((short)Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008617 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008618 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008619 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008620 if ((Value >> 16) == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008621 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008622 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008623 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008624 if (Value > 31)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008625 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008626 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008627 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008628 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008629 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008630 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008631 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008632 if (Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008633 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008634 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008635 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008636 if ((short)-Value == (int)-Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008637 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008638 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008639 }
8640 break;
8641 }
8642 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008643
Gabor Greiff304a7a2008-08-28 21:40:38 +00008644 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008645 Ops.push_back(Result);
8646 return;
8647 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008648
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008649 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +00008650 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008651}
Evan Cheng2dd2c652006-03-13 23:20:37 +00008652
Chris Lattner1eb94d92007-03-30 23:15:24 +00008653// isLegalAddressingMode - Return true if the addressing mode represented
8654// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008655bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00008656 Type *Ty) const {
Chris Lattner1eb94d92007-03-30 23:15:24 +00008657 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelcf0da6c2009-02-17 22:15:04 +00008658
Chris Lattner1eb94d92007-03-30 23:15:24 +00008659 // PPC allows a sign-extended 16-bit immediate field.
8660 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
8661 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008662
Chris Lattner1eb94d92007-03-30 23:15:24 +00008663 // No global is ever allowed as a base.
8664 if (AM.BaseGV)
8665 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008666
8667 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +00008668 switch (AM.Scale) {
8669 case 0: // "r+i" or just "i", depending on HasBaseReg.
8670 break;
8671 case 1:
8672 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
8673 return false;
8674 // Otherwise we have r+r or r+i.
8675 break;
8676 case 2:
8677 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
8678 return false;
8679 // Allow 2*r as r+r.
8680 break;
Chris Lattner19ccd622007-04-09 22:10:05 +00008681 default:
8682 // No other scales are supported.
8683 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +00008684 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008685
Chris Lattner1eb94d92007-03-30 23:15:24 +00008686 return true;
8687}
8688
Dan Gohman21cea8a2010-04-17 15:26:15 +00008689SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
8690 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00008691 MachineFunction &MF = DAG.getMachineFunction();
8692 MachineFrameInfo *MFI = MF.getFrameInfo();
8693 MFI->setReturnAddressIsTaken(true);
8694
Bill Wendling908bf812014-01-06 00:43:20 +00008695 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008696 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008697
Andrew Trickef9de2a2013-05-25 02:42:55 +00008698 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008699 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +00008700
Dale Johannesen81bfca72010-05-03 22:59:34 +00008701 // Make sure the function does not optimize away the store of the RA to
8702 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +00008703 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008704 FuncInfo->setLRStoreRequired();
8705 bool isPPC64 = PPCSubTarget.isPPC64();
8706 bool isDarwinABI = PPCSubTarget.isDarwinABI();
8707
8708 if (Depth > 0) {
8709 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8710 SDValue Offset =
Wesley Peck527da1b2010-11-23 03:31:01 +00008711
Anton Korobeynikov2f931282011-01-10 12:39:04 +00008712 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen81bfca72010-05-03 22:59:34 +00008713 isPPC64? MVT::i64 : MVT::i32);
8714 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
8715 DAG.getNode(ISD::ADD, dl, getPointerTy(),
8716 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008717 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008718 }
Chris Lattnerf6a81562007-12-08 06:59:59 +00008719
Chris Lattnerf6a81562007-12-08 06:59:59 +00008720 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008721 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008722 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008723 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +00008724}
8725
Dan Gohman21cea8a2010-04-17 15:26:15 +00008726SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
8727 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008728 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008729 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00008730
Owen Anderson53aa7a92009-08-10 22:56:29 +00008731 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00008732 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008733
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008734 MachineFunction &MF = DAG.getMachineFunction();
8735 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008736 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +00008737
8738 // Naked functions never have a frame pointer, and so we use r1. For all
8739 // other functions, this decision must be delayed until during PEI.
8740 unsigned FrameReg;
8741 if (MF.getFunction()->getAttributes().hasAttribute(
8742 AttributeSet::FunctionIndex, Attribute::Naked))
8743 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
8744 else
8745 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
8746
Dale Johannesen81bfca72010-05-03 22:59:34 +00008747 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
8748 PtrVT);
8749 while (Depth--)
8750 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008751 FrameAddr, MachinePointerInfo(), false, false,
8752 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008753 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008754}
Dan Gohmanc14e5222008-10-21 03:41:46 +00008755
Hal Finkel0d8db462014-05-11 19:29:11 +00008756// FIXME? Maybe this could be a TableGen attribute on some registers and
8757// this table could be generated automatically from RegInfo.
8758unsigned PPCTargetLowering::getRegisterByName(const char* RegName,
8759 EVT VT) const {
8760 bool isPPC64 = PPCSubTarget.isPPC64();
8761 bool isDarwinABI = PPCSubTarget.isDarwinABI();
8762
8763 if ((isPPC64 && VT != MVT::i64 && VT != MVT::i32) ||
8764 (!isPPC64 && VT != MVT::i32))
8765 report_fatal_error("Invalid register global variable type");
8766
8767 bool is64Bit = isPPC64 && VT == MVT::i64;
8768 unsigned Reg = StringSwitch<unsigned>(RegName)
8769 .Case("r1", is64Bit ? PPC::X1 : PPC::R1)
8770 .Case("r2", isDarwinABI ? 0 : (is64Bit ? PPC::X2 : PPC::R2))
8771 .Case("r13", (!isPPC64 && isDarwinABI) ? 0 :
8772 (is64Bit ? PPC::X13 : PPC::R13))
8773 .Default(0);
8774
8775 if (Reg)
8776 return Reg;
8777 report_fatal_error("Invalid register name global variable");
8778}
8779
Dan Gohmanc14e5222008-10-21 03:41:46 +00008780bool
8781PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8782 // The PowerPC target isn't yet aware of offsets.
8783 return false;
8784}
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008785
Evan Chengd9929f02010-04-01 20:10:42 +00008786/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +00008787/// and store operations as a result of memset, memcpy, and memmove
8788/// lowering. If DstAlign is zero that means it's safe to destination
8789/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
8790/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +00008791/// probably because the source does not need to be loaded. If 'IsMemset' is
8792/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
8793/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
8794/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +00008795/// It returns EVT::Other if the type should be determined using generic
8796/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +00008797EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
8798 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00008799 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00008800 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +00008801 MachineFunction &MF) const {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008802 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +00008803 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008804 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00008805 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008806 }
8807}
Hal Finkel88ed4e32012-04-01 19:23:08 +00008808
Hal Finkel34974ed2014-04-12 21:52:38 +00008809/// \brief Returns true if it is beneficial to convert a load of a constant
8810/// to just the constant itself.
8811bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
8812 Type *Ty) const {
8813 assert(Ty->isIntegerTy());
8814
8815 unsigned BitSize = Ty->getPrimitiveSizeInBits();
8816 if (BitSize == 0 || BitSize > 64)
8817 return false;
8818 return true;
8819}
8820
8821bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
8822 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
8823 return false;
8824 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
8825 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
8826 return NumBits1 == 64 && NumBits2 == 32;
8827}
8828
8829bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
8830 if (!VT1.isInteger() || !VT2.isInteger())
8831 return false;
8832 unsigned NumBits1 = VT1.getSizeInBits();
8833 unsigned NumBits2 = VT2.getSizeInBits();
8834 return NumBits1 == 64 && NumBits2 == 32;
8835}
8836
8837bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
8838 return isInt<16>(Imm) || isUInt<16>(Imm);
8839}
8840
8841bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8842 return isInt<16>(Imm) || isUInt<16>(Imm);
8843}
8844
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008845bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +00008846 unsigned,
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008847 bool *Fast) const {
8848 if (DisablePPCUnaligned)
8849 return false;
8850
8851 // PowerPC supports unaligned memory access for simple non-vector types.
8852 // Although accessing unaligned addresses is not as efficient as accessing
8853 // aligned addresses, it is generally more efficient than manual expansion,
8854 // and generally only traps for software emulation when crossing page
8855 // boundaries.
8856
8857 if (!VT.isSimple())
8858 return false;
8859
Hal Finkel6e28e6a2014-03-26 19:39:09 +00008860 if (VT.getSimpleVT().isVector()) {
8861 if (PPCSubTarget.hasVSX()) {
8862 if (VT != MVT::v2f64 && VT != MVT::v2i64)
8863 return false;
8864 } else {
8865 return false;
8866 }
8867 }
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008868
8869 if (VT == MVT::ppcf128)
8870 return false;
8871
8872 if (Fast)
8873 *Fast = true;
8874
8875 return true;
8876}
8877
Stephen Lin73de7bf2013-07-09 18:16:56 +00008878bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
8879 VT = VT.getScalarType();
8880
Hal Finkel0a479ae2012-06-22 00:49:52 +00008881 if (!VT.isSimple())
8882 return false;
8883
8884 switch (VT.getSimpleVT().SimpleTy) {
8885 case MVT::f32:
8886 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +00008887 return true;
8888 default:
8889 break;
8890 }
8891
8892 return false;
8893}
8894
Hal Finkelb4240ca2014-03-31 17:48:16 +00008895bool
8896PPCTargetLowering::shouldExpandBuildVectorWithShuffles(
8897 EVT VT , unsigned DefinedValues) const {
8898 if (VT == MVT::v2i64)
8899 return false;
8900
8901 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
8902}
8903
Hal Finkel88ed4e32012-04-01 19:23:08 +00008904Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel21442b22013-09-11 23:05:25 +00008905 if (DisableILPPref || PPCSubTarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008906 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +00008907
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008908 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +00008909}
8910
Bill Schmidt0cf702f2013-07-30 00:50:39 +00008911// Create a fast isel object.
8912FastISel *
8913PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
8914 const TargetLibraryInfo *LibInfo) const {
8915 return PPC::createFastISel(FuncInfo, LibInfo);
8916}