blob: f56f0cf84ccf1c76427da784b5fe16f5f45eaca6 [file] [log] [blame]
Nate Begeman0b71e002005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattnerf22556d2005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf22556d2005-08-16 17:14:42 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattnerf22556d2005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6f3b9542005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Jim Laskey48850c12006-11-16 22:43:37 +000016#include "PPCMachineFunctionInfo.h"
Bill Wendlingdd3fe942010-03-12 02:00:43 +000017#include "PPCPerfectShuffle.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000018#include "PPCTargetMachine.h"
Bill Schmidt22d40dc2013-05-13 19:34:37 +000019#include "PPCTargetObjectFile.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000020#include "llvm/ADT/STLExtras.h"
Chris Lattner4f2e4e02007-03-06 00:59:59 +000021#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner9b577f12005-08-26 21:23:58 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000026#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000027#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/Constants.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/Intrinsics.h"
Chris Lattnerce645542006-11-10 02:08:47 +000033#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
Craig Topperb25fda92012-03-17 18:46:09 +000035#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000036#include "llvm/Support/raw_ostream.h"
Craig Topperb25fda92012-03-17 18:46:09 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000038using namespace llvm;
39
Hal Finkel595817e2012-06-04 02:21:00 +000040static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
41cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000042
Hal Finkel4e9f1a82012-06-10 19:32:29 +000043static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
44cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
45
Hal Finkel8d7fbc92013-03-15 15:27:13 +000046static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
47cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
48
Hal Finkel940ab932014-02-28 00:27:01 +000049// FIXME: Remove this once the bug has been fixed!
50extern cl::opt<bool> ANDIGlueBug;
51
Chris Lattner5e693ed2009-07-28 03:13:23 +000052static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) {
53 if (TM.getSubtargetImpl()->isDarwin())
Bill Wendlingbbcaa402010-03-15 21:09:38 +000054 return new TargetLoweringObjectFileMachO();
Bill Wendlingdd3fe942010-03-12 02:00:43 +000055
Bill Schmidt22d40dc2013-05-13 19:34:37 +000056 if (TM.getSubtargetImpl()->isSVR4ABI())
57 return new PPC64LinuxTargetObjectFile();
58
Bruno Cardoso Lopes62e6a8b2009-08-13 23:30:21 +000059 return new TargetLoweringObjectFileELF();
Chris Lattner5e693ed2009-07-28 03:13:23 +000060}
61
Chris Lattner584a11a2006-11-02 01:44:04 +000062PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Chris Lattner5e693ed2009-07-28 03:13:23 +000063 : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) {
Evan Cheng39e90022012-07-02 22:39:56 +000064 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
Scott Michelcf0da6c2009-02-17 22:15:04 +000065
Nate Begeman4dd38312005-10-21 00:02:42 +000066 setPow2DivIsCheap();
Dale Johannesenc31eb202008-07-31 18:13:12 +000067
Chris Lattnera028e7a2005-09-27 22:18:25 +000068 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000069 setUseUnderscoreSetJmp(true);
70 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000071
Chris Lattnerd10babf2010-10-10 18:34:00 +000072 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
73 // arguments are at least 4/8 bytes aligned.
Evan Cheng39e90022012-07-02 22:39:56 +000074 bool isPPC64 = Subtarget->isPPC64();
75 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000076
Chris Lattnerf22556d2005-08-16 17:14:42 +000077 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000078 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
79 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
80 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +000081
Evan Cheng5d9fd972006-10-04 00:56:09 +000082 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson9f944592009-08-11 20:47:22 +000083 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
84 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sands95d46ef2008-01-23 20:39:46 +000085
Owen Anderson9f944592009-08-11 20:47:22 +000086 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000087
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000088 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000089 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
90 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
91 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
92 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
93 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
94 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
95 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
96 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
97 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
98 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +000099
Hal Finkel940ab932014-02-28 00:27:01 +0000100 if (Subtarget->useCRBits()) {
101 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
102
103 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
104 AddPromotedToType (ISD::SINT_TO_FP, MVT::i1,
105 isPPC64 ? MVT::i64 : MVT::i32);
106 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
107 AddPromotedToType (ISD::UINT_TO_FP, MVT::i1,
108 isPPC64 ? MVT::i64 : MVT::i32);
109
110 // PowerPC does not support direct load / store of condition registers
111 setOperationAction(ISD::LOAD, MVT::i1, Custom);
112 setOperationAction(ISD::STORE, MVT::i1, Custom);
113
114 // FIXME: Remove this once the ANDI glue bug is fixed:
115 if (ANDIGlueBug)
116 setOperationAction(ISD::TRUNCATE, MVT::i1, Custom);
117
118 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
119 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
120 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
121 setTruncStoreAction(MVT::i32, MVT::i1, Expand);
122 setTruncStoreAction(MVT::i16, MVT::i1, Expand);
123 setTruncStoreAction(MVT::i8, MVT::i1, Expand);
124
125 addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass);
126 }
127
Dale Johannesen666323e2007-10-10 01:01:31 +0000128 // This is used in the ppcf128->int sequence. Note it has different semantics
129 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +0000130 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000131
Roman Divacky1faf5b02012-08-16 18:19:29 +0000132 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000133 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
134 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
135 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
136 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
137 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000138 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000139
Chris Lattnerf22556d2005-08-16 17:14:42 +0000140 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000141 setOperationAction(ISD::SREM, MVT::i32, Expand);
142 setOperationAction(ISD::UREM, MVT::i32, Expand);
143 setOperationAction(ISD::SREM, MVT::i64, Expand);
144 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000145
146 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000147 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
148 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
149 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
150 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
151 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
152 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
153 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
154 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000155
Dan Gohman482732a2007-10-11 23:21:31 +0000156 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000157 setOperationAction(ISD::FSIN , MVT::f64, Expand);
158 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000159 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000160 setOperationAction(ISD::FREM , MVT::f64, Expand);
161 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000162 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000163 setOperationAction(ISD::FSIN , MVT::f32, Expand);
164 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000165 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000166 setOperationAction(ISD::FREM , MVT::f32, Expand);
167 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000168 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000169
Owen Anderson9f944592009-08-11 20:47:22 +0000170 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000171
Chris Lattnerf22556d2005-08-16 17:14:42 +0000172 // If we're enabling GP optimizations, use hardware square root
Hal Finkel2e103312013-04-03 04:01:11 +0000173 if (!Subtarget->hasFSQRT() &&
174 !(TM.Options.UnsafeFPMath &&
175 Subtarget->hasFRSQRTE() && Subtarget->hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000176 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000177
178 if (!Subtarget->hasFSQRT() &&
179 !(TM.Options.UnsafeFPMath &&
180 Subtarget->hasFRSQRTES() && Subtarget->hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000181 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000182
Hal Finkeldbc78e12013-08-19 05:01:02 +0000183 if (Subtarget->hasFCPSGN()) {
184 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
185 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
186 } else {
187 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
188 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
189 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000190
Hal Finkelc20a08d2013-03-29 08:57:48 +0000191 if (Subtarget->hasFPRND()) {
192 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
193 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
194 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000195 setOperationAction(ISD::FROUND, MVT::f64, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000196
197 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
198 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
199 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000200 setOperationAction(ISD::FROUND, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000201 }
202
Nate Begeman2fba8a32006-01-14 03:14:10 +0000203 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000204 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000205 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000206 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
207 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000208 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000209 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000210 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
211 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000212
Hal Finkela4d07482013-03-28 13:29:47 +0000213 if (Subtarget->hasPOPCNTD()) {
Hal Finkel290376d2013-04-01 15:58:15 +0000214 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000215 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
216 } else {
217 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
218 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
219 }
220
Nate Begeman1b8121b2006-01-11 21:21:00 +0000221 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000222 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
223 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000224
Hal Finkel940ab932014-02-28 00:27:01 +0000225 if (!Subtarget->useCRBits()) {
226 // PowerPC does not have Select
227 setOperationAction(ISD::SELECT, MVT::i32, Expand);
228 setOperationAction(ISD::SELECT, MVT::i64, Expand);
229 setOperationAction(ISD::SELECT, MVT::f32, Expand);
230 setOperationAction(ISD::SELECT, MVT::f64, Expand);
231 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000232
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000233 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000234 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
235 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000236
Nate Begeman7e7f4392006-02-01 07:19:44 +0000237 // PowerPC wants to optimize integer setcc a bit
Hal Finkel940ab932014-02-28 00:27:01 +0000238 if (!Subtarget->useCRBits())
239 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000240
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000241 // PowerPC does not have BRCOND which requires SetCC
Hal Finkel940ab932014-02-28 00:27:01 +0000242 if (!Subtarget->useCRBits())
243 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000244
Owen Anderson9f944592009-08-11 20:47:22 +0000245 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000246
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000247 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000248 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000249
Jim Laskey6267b2c2005-08-17 00:40:22 +0000250 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000251 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
252 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000253
Wesley Peck527da1b2010-11-23 03:31:01 +0000254 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
255 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
256 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
257 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattnerc46fc242005-12-23 05:13:35 +0000258
Chris Lattner84b49d52006-04-28 21:56:10 +0000259 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000260 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000261
Hal Finkel1996f3d2013-03-27 19:10:42 +0000262 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000263 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
264 // support continuation, user-level threading, and etc.. As a result, no
265 // other SjLj exception interfaces are implemented and please don't build
266 // your own exception handling based on them.
267 // LLVM/Clang supports zero-cost DWARF exception handling.
268 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
269 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000270
271 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000272 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000273 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
274 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000275 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000276 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
277 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
278 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
279 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000280 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000281 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
282 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000283
Nate Begemanf69d13b2008-08-11 17:36:31 +0000284 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000285 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000286
287 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000288 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
289 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000290
Nate Begemane74795c2006-01-25 18:21:52 +0000291 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000292 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000293
Evan Cheng39e90022012-07-02 22:39:56 +0000294 if (Subtarget->isSVR4ABI()) {
295 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000296 // VAARG always uses double-word chunks, so promote anything smaller.
297 setOperationAction(ISD::VAARG, MVT::i1, Promote);
298 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
299 setOperationAction(ISD::VAARG, MVT::i8, Promote);
300 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
301 setOperationAction(ISD::VAARG, MVT::i16, Promote);
302 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
303 setOperationAction(ISD::VAARG, MVT::i32, Promote);
304 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
305 setOperationAction(ISD::VAARG, MVT::Other, Expand);
306 } else {
307 // VAARG is custom lowered with the 32-bit SVR4 ABI.
308 setOperationAction(ISD::VAARG, MVT::Other, Custom);
309 setOperationAction(ISD::VAARG, MVT::i64, Custom);
310 }
Roman Divacky4394e682011-06-28 15:30:42 +0000311 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000312 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000313
Roman Divackyc3825df2013-07-25 21:36:47 +0000314 if (Subtarget->isSVR4ABI() && !isPPC64)
315 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
316 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
317 else
318 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
319
Chris Lattner5bd514d2006-01-15 09:02:48 +0000320 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000321 setOperationAction(ISD::VAEND , MVT::Other, Expand);
322 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
323 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
324 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
325 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000326
Chris Lattner6961fc72006-03-26 10:06:40 +0000327 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000328 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000329
Hal Finkel25c19922013-05-15 21:37:41 +0000330 // To handle counter-based loop conditions.
331 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
332
Dale Johannesen160be0f2008-11-07 22:54:33 +0000333 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000334 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
335 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
336 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
337 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
338 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
339 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
340 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
341 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
342 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
343 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
344 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
345 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000346
Evan Cheng39e90022012-07-02 22:39:56 +0000347 if (Subtarget->has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000348 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000349 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
350 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
351 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
352 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000353 // This is just the low 32 bits of a (signed) fp->i64 conversion.
354 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000355 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000356
Hal Finkelf6d45f22013-04-01 17:52:07 +0000357 if (PPCSubTarget.hasLFIWAX() || Subtarget->isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000358 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000359 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000360 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000361 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000362 }
363
Hal Finkelf6d45f22013-04-01 17:52:07 +0000364 // With the instructions enabled under FPCVT, we can do everything.
365 if (PPCSubTarget.hasFPCVT()) {
366 if (Subtarget->has64BitSupport()) {
367 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
368 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
369 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
370 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
371 }
372
373 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
374 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
375 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
376 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
377 }
378
Evan Cheng39e90022012-07-02 22:39:56 +0000379 if (Subtarget->use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000380 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000381 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000382 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000383 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000384 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000385 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
386 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
387 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000388 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000389 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000390 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
391 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
392 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000393 }
Evan Cheng19264272006-03-01 01:11:20 +0000394
Evan Cheng39e90022012-07-02 22:39:56 +0000395 if (Subtarget->hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000396 // First set operation action for all vector types to expand. Then we
397 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson9f944592009-08-11 20:47:22 +0000398 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
399 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
400 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands13237ac2008-06-06 12:08:01 +0000401
Chris Lattner06a21ba2006-04-16 01:37:57 +0000402 // add/sub are legal for all supported vector VT's.
Duncan Sands13237ac2008-06-06 12:08:01 +0000403 setOperationAction(ISD::ADD , VT, Legal);
404 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000405
Chris Lattner95c7adc2006-04-04 17:25:31 +0000406 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000407 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000408 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000409
410 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000411 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000412 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000413 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000414 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000415 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000416 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000417 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000418 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000419 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000420 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000421 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000422 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000423
Chris Lattner06a21ba2006-04-16 01:37:57 +0000424 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000425 setOperationAction(ISD::MUL , VT, Expand);
426 setOperationAction(ISD::SDIV, VT, Expand);
427 setOperationAction(ISD::SREM, VT, Expand);
428 setOperationAction(ISD::UDIV, VT, Expand);
429 setOperationAction(ISD::UREM, VT, Expand);
430 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000431 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000432 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000433 setOperationAction(ISD::FSQRT, VT, Expand);
434 setOperationAction(ISD::FLOG, VT, Expand);
435 setOperationAction(ISD::FLOG10, VT, Expand);
436 setOperationAction(ISD::FLOG2, VT, Expand);
437 setOperationAction(ISD::FEXP, VT, Expand);
438 setOperationAction(ISD::FEXP2, VT, Expand);
439 setOperationAction(ISD::FSIN, VT, Expand);
440 setOperationAction(ISD::FCOS, VT, Expand);
441 setOperationAction(ISD::FABS, VT, Expand);
442 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000443 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000444 setOperationAction(ISD::FCEIL, VT, Expand);
445 setOperationAction(ISD::FTRUNC, VT, Expand);
446 setOperationAction(ISD::FRINT, VT, Expand);
447 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000448 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
449 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
450 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
451 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
452 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
453 setOperationAction(ISD::UDIVREM, VT, Expand);
454 setOperationAction(ISD::SDIVREM, VT, Expand);
455 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
456 setOperationAction(ISD::FPOW, VT, Expand);
457 setOperationAction(ISD::CTPOP, VT, Expand);
458 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000459 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000460 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000461 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000462 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000463 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
464
465 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
466 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
467 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
468 setTruncStoreAction(VT, InnerVT, Expand);
469 }
470 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
471 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
472 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000473 }
474
Chris Lattner95c7adc2006-04-04 17:25:31 +0000475 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
476 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000477 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000478
Owen Anderson9f944592009-08-11 20:47:22 +0000479 setOperationAction(ISD::AND , MVT::v4i32, Legal);
480 setOperationAction(ISD::OR , MVT::v4i32, Legal);
481 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
482 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Hal Finkel940ab932014-02-28 00:27:01 +0000483 setOperationAction(ISD::SELECT, MVT::v4i32,
484 Subtarget->useCRBits() ? Legal : Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000485 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000486 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
487 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
488 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
489 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000490 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
491 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
492 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
493 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000494
Craig Topperabadc662012-04-20 06:31:50 +0000495 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
496 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
497 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
498 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000499
Owen Anderson9f944592009-08-11 20:47:22 +0000500 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000501 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000502
503 if (TM.Options.UnsafeFPMath) {
504 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
505 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
506 }
507
Owen Anderson9f944592009-08-11 20:47:22 +0000508 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
509 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
510 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000511
Owen Anderson9f944592009-08-11 20:47:22 +0000512 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
513 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000514
Owen Anderson9f944592009-08-11 20:47:22 +0000515 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
516 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
517 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
518 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000519
520 // Altivec does not contain unordered floating-point compare instructions
521 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
522 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
523 setCondCodeAction(ISD::SETUGT, MVT::v4f32, Expand);
524 setCondCodeAction(ISD::SETUGE, MVT::v4f32, Expand);
525 setCondCodeAction(ISD::SETULT, MVT::v4f32, Expand);
526 setCondCodeAction(ISD::SETULE, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000527
528 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
529 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000530 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000531
Hal Finkel70381a72012-08-04 14:10:46 +0000532 if (Subtarget->has64BitSupport()) {
Hal Finkel322e41a2012-04-01 20:08:17 +0000533 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel70381a72012-08-04 14:10:46 +0000534 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
535 }
Hal Finkel322e41a2012-04-01 20:08:17 +0000536
Eli Friedman7dfa7912011-08-29 18:23:02 +0000537 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
538 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkel1b5ff082012-12-25 17:22:53 +0000539 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
540 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000541
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000542 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidta76bf5a2013-04-23 18:49:44 +0000543 // Altivec instructions set fields to all zeros or all ones.
544 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000545
Evan Cheng39e90022012-07-02 22:39:56 +0000546 if (isPPC64) {
Chris Lattner454436d2006-10-18 01:20:43 +0000547 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000548 setExceptionPointerRegister(PPC::X3);
549 setExceptionSelectorRegister(PPC::X4);
550 } else {
Chris Lattner454436d2006-10-18 01:20:43 +0000551 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000552 setExceptionPointerRegister(PPC::R3);
553 setExceptionSelectorRegister(PPC::R4);
554 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000555
Chris Lattnerf4184352006-03-01 04:57:39 +0000556 // We have target-specific dag combine patterns for the following nodes:
557 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000558 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000559 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000560 setTargetDAGCombine(ISD::BR_CC);
Hal Finkel940ab932014-02-28 00:27:01 +0000561 if (Subtarget->useCRBits())
562 setTargetDAGCombine(ISD::BRCOND);
Chris Lattnera7976d32006-07-10 20:56:58 +0000563 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000564 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000565
Hal Finkel940ab932014-02-28 00:27:01 +0000566 if (Subtarget->useCRBits()) {
567 setTargetDAGCombine(ISD::SIGN_EXTEND);
568 setTargetDAGCombine(ISD::ZERO_EXTEND);
569 setTargetDAGCombine(ISD::ANY_EXTEND);
570 setTargetDAGCombine(ISD::TRUNCATE);
571 setTargetDAGCombine(ISD::SETCC);
572 setTargetDAGCombine(ISD::SELECT_CC);
573 }
574
Hal Finkel2e103312013-04-03 04:01:11 +0000575 // Use reciprocal estimates.
576 if (TM.Options.UnsafeFPMath) {
577 setTargetDAGCombine(ISD::FDIV);
578 setTargetDAGCombine(ISD::FSQRT);
579 }
580
Dale Johannesen10432e52007-10-19 00:59:18 +0000581 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng39e90022012-07-02 22:39:56 +0000582 if (Subtarget->isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000583 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000584 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
585 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000586 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
587 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000588 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
589 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
590 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
591 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
592 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000593 }
594
Hal Finkel940ab932014-02-28 00:27:01 +0000595 // With 32 condition bits, we don't need to sink (and duplicate) compares
596 // aggressively in CodeGenPrep.
597 if (Subtarget->useCRBits())
598 setHasMultipleConditionRegisters();
599
Hal Finkel65298572011-10-17 18:53:03 +0000600 setMinFunctionAlignment(2);
601 if (PPCSubTarget.isDarwin())
602 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000603
Evan Cheng39e90022012-07-02 22:39:56 +0000604 if (isPPC64 && Subtarget->isJITCodeModel())
605 // Temporary workaround for the inability of PPC64 JIT to handle jump
606 // tables.
607 setSupportJumpTables(false);
608
Eli Friedman30a49e92011-08-03 21:06:02 +0000609 setInsertFencesForAtomic(true);
610
Hal Finkel21442b22013-09-11 23:05:25 +0000611 if (Subtarget->enableMachineScheduler())
612 setSchedulingPreference(Sched::Source);
613 else
614 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000615
Chris Lattnerf22556d2005-08-16 17:14:42 +0000616 computeRegisterProperties();
Hal Finkel742b5352012-08-28 16:12:39 +0000617
618 // The Freescale cores does better with aggressive inlining of memcpy and
619 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
620 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
621 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000622 MaxStoresPerMemset = 32;
623 MaxStoresPerMemsetOptSize = 16;
624 MaxStoresPerMemcpy = 32;
625 MaxStoresPerMemcpyOptSize = 8;
626 MaxStoresPerMemmove = 32;
627 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel742b5352012-08-28 16:12:39 +0000628
629 setPrefFunctionAlignment(4);
Hal Finkel742b5352012-08-28 16:12:39 +0000630 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000631}
632
Hal Finkel262a2242013-09-12 23:20:06 +0000633/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
634/// the desired ByVal argument alignment.
635static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
636 unsigned MaxMaxAlign) {
637 if (MaxAlign == MaxMaxAlign)
638 return;
639 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
640 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
641 MaxAlign = 32;
642 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
643 MaxAlign = 16;
644 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
645 unsigned EltAlign = 0;
646 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
647 if (EltAlign > MaxAlign)
648 MaxAlign = EltAlign;
649 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
650 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
651 unsigned EltAlign = 0;
652 getMaxByValAlign(STy->getElementType(i), EltAlign, MaxMaxAlign);
653 if (EltAlign > MaxAlign)
654 MaxAlign = EltAlign;
655 if (MaxAlign == MaxMaxAlign)
656 break;
657 }
658 }
659}
660
Dale Johannesencbde4c22008-02-28 22:31:51 +0000661/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
662/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000663unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000664 // Darwin passes everything on 4 byte boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000665 if (PPCSubTarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000666 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000667
668 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000669 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000670 unsigned Align = PPCSubTarget.isPPC64() ? 8 : 4;
671 if (PPCSubTarget.hasAltivec() || PPCSubTarget.hasQPX())
672 getMaxByValAlign(Ty, Align, PPCSubTarget.hasQPX() ? 32 : 16);
673 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000674}
675
Chris Lattner347ed8a2006-01-09 23:52:17 +0000676const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
677 switch (Opcode) {
678 default: return 0;
Evan Cheng32e376f2008-07-12 02:23:19 +0000679 case PPCISD::FSEL: return "PPCISD::FSEL";
680 case PPCISD::FCFID: return "PPCISD::FCFID";
681 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
682 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000683 case PPCISD::FRE: return "PPCISD::FRE";
684 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000685 case PPCISD::STFIWX: return "PPCISD::STFIWX";
686 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
687 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
688 case PPCISD::VPERM: return "PPCISD::VPERM";
689 case PPCISD::Hi: return "PPCISD::Hi";
690 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000691 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller79fef932009-12-18 13:00:15 +0000692 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
693 case PPCISD::LOAD: return "PPCISD::LOAD";
694 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000695 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
696 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
697 case PPCISD::SRL: return "PPCISD::SRL";
698 case PPCISD::SRA: return "PPCISD::SRA";
699 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000700 case PPCISD::CALL: return "PPCISD::CALL";
701 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000702 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000703 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng32e376f2008-07-12 02:23:19 +0000704 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel756810f2013-03-21 21:37:52 +0000705 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
706 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000707 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng32e376f2008-07-12 02:23:19 +0000708 case PPCISD::VCMP: return "PPCISD::VCMP";
709 case PPCISD::VCMPo: return "PPCISD::VCMPo";
710 case PPCISD::LBRX: return "PPCISD::LBRX";
711 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng32e376f2008-07-12 02:23:19 +0000712 case PPCISD::LARX: return "PPCISD::LARX";
713 case PPCISD::STCX: return "PPCISD::STCX";
714 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +0000715 case PPCISD::BDNZ: return "PPCISD::BDNZ";
716 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000717 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000718 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000719 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +0000720 case PPCISD::CR6SET: return "PPCISD::CR6SET";
721 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34627e32012-11-27 17:35:46 +0000722 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
723 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
724 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Roman Divacky32143e22013-12-20 18:08:54 +0000725 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000726 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
727 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000728 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000729 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
730 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
731 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000732 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
733 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
734 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
735 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
736 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +0000737 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +0000738 case PPCISD::SC: return "PPCISD::SC";
Chris Lattner347ed8a2006-01-09 23:52:17 +0000739 }
740}
741
Matt Arsenault758659232013-05-18 00:21:46 +0000742EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000743 if (!VT.isVector())
Hal Finkel940ab932014-02-28 00:27:01 +0000744 return PPCSubTarget.useCRBits() ? MVT::i1 : MVT::i32;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000745 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000746}
747
Chris Lattner4211ca92006-04-14 06:01:58 +0000748//===----------------------------------------------------------------------===//
749// Node matching predicates, for use by the tblgen matching code.
750//===----------------------------------------------------------------------===//
751
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000752/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000753static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000754 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000755 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000756 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000757 // Maybe this has already been legalized into the constant pool?
758 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000759 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000760 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000761 }
762 return false;
763}
764
Chris Lattnere8b83b42006-04-06 17:23:16 +0000765/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
766/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000767static bool isConstantOrUndef(int Op, int Val) {
768 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000769}
770
771/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
772/// VPKUHUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000773bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000774 if (!isUnary) {
775 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000776 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000777 return false;
778 } else {
779 for (unsigned i = 0; i != 8; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000780 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
781 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000782 return false;
783 }
Chris Lattner1d338192006-04-06 18:26:28 +0000784 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000785}
786
787/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
788/// VPKUWUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000789bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000790 if (!isUnary) {
791 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000792 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
793 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000794 return false;
795 } else {
796 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000797 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
798 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
799 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
800 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000801 return false;
802 }
Chris Lattner1d338192006-04-06 18:26:28 +0000803 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000804}
805
Chris Lattnerf38e0332006-04-06 22:02:42 +0000806/// isVMerge - Common function, used to match vmrg* shuffles.
807///
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000808static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +0000809 unsigned LHSStart, unsigned RHSStart) {
Owen Anderson9f944592009-08-11 20:47:22 +0000810 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000811 "PPC only supports shuffles by bytes!");
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000812 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
813 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000814
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000815 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
816 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000817 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000818 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000819 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000820 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000821 return false;
822 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000823 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +0000824}
825
826/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
827/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000828bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000829 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000830 if (!isUnary)
831 return isVMerge(N, UnitSize, 8, 24);
832 return isVMerge(N, UnitSize, 8, 8);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000833}
834
835/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
836/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000837bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000838 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000839 if (!isUnary)
840 return isVMerge(N, UnitSize, 0, 16);
841 return isVMerge(N, UnitSize, 0, 0);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000842}
843
844
Chris Lattner1d338192006-04-06 18:26:28 +0000845/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
846/// amount, otherwise return -1.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000847int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Owen Anderson9f944592009-08-11 20:47:22 +0000848 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000849 "PPC only supports shuffles by bytes!");
850
851 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +0000852
Chris Lattner1d338192006-04-06 18:26:28 +0000853 // Find the first non-undef value in the shuffle mask.
854 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000855 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +0000856 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000857
Chris Lattner1d338192006-04-06 18:26:28 +0000858 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000859
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000860 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +0000861 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000862 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +0000863 if (ShiftAmt < i) return -1;
864 ShiftAmt -= i;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000865
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000866 if (!isUnary) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000867 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000868 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000869 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000870 return -1;
871 } else {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000872 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000873 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000874 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000875 return -1;
876 }
Chris Lattner1d338192006-04-06 18:26:28 +0000877 return ShiftAmt;
878}
Chris Lattnerffc47562006-03-20 06:33:01 +0000879
880/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
881/// specifies a splat of a single element that is suitable for input to
882/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000883bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +0000884 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +0000885 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000886
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000887 // This is a splat operation if each element of the permute is the same, and
888 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000889 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +0000890
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000891 // FIXME: Handle UNDEF elements too!
892 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +0000893 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000894
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000895 // Check that the indices are consecutive, in the case of a multi-byte element
896 // splatted with a v16i8 mask.
897 for (unsigned i = 1; i != EltSize; ++i)
898 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000899 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000900
Chris Lattner95c7adc2006-04-04 17:25:31 +0000901 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000902 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +0000903 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000904 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000905 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000906 }
Chris Lattner95c7adc2006-04-04 17:25:31 +0000907 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +0000908}
909
Evan Cheng581d2792007-07-30 07:51:22 +0000910/// isAllNegativeZeroVector - Returns true if all elements of build_vector
911/// are -0.0.
912bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000913 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
914
915 APInt APVal, APUndef;
916 unsigned BitSize;
917 bool HasAnyUndefs;
Wesley Peck527da1b2010-11-23 03:31:01 +0000918
Dale Johannesen5f4eecf2009-11-13 01:45:18 +0000919 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000920 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000921 return CFP->getValueAPF().isNegZero();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000922
Evan Cheng581d2792007-07-30 07:51:22 +0000923 return false;
924}
925
Chris Lattnerffc47562006-03-20 06:33:01 +0000926/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
927/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner95c7adc2006-04-04 17:25:31 +0000928unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000929 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
930 assert(isSplatShuffleMask(SVOp, EltSize));
931 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +0000932}
933
Chris Lattner74cf9ff2006-04-12 17:37:20 +0000934/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +0000935/// by using a vspltis[bhw] instruction of the specified element size, return
936/// the constant being splatted. The ByteSize field indicates the number of
937/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000938SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
939 SDValue OpVal(0, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000940
941 // If ByteSize of the splat is bigger than the element size of the
942 // build_vector, then we have a case where we are checking for a splat where
943 // multiple elements of the buildvector are folded together into a single
944 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
945 unsigned EltSize = 16/N->getNumOperands();
946 if (EltSize < ByteSize) {
947 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000948 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000949 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000950
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000951 // See if all of the elements in the buildvector agree across.
952 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
953 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
954 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000955 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000956
Scott Michelcf0da6c2009-02-17 22:15:04 +0000957
Gabor Greiff304a7a2008-08-28 21:40:38 +0000958 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000959 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
960 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000961 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000962 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000963
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000964 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
965 // either constant or undef values that are identical for each chunk. See
966 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000967
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000968 // Check to see if all of the leading entries are either 0 or -1. If
969 // neither, then this won't fit into the immediate field.
970 bool LeadingZero = true;
971 bool LeadingOnes = true;
972 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000973 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000974
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000975 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
976 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
977 }
978 // Finally, check the least significant entry.
979 if (LeadingZero) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000980 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +0000981 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +0000982 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000983 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +0000984 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000985 }
986 if (LeadingOnes) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000987 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +0000988 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +0000989 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000990 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +0000991 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000992 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000993
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000994 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000995 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000996
Chris Lattner2771e2c2006-03-25 06:12:06 +0000997 // Check to see if this buildvec has a single non-undef value in its elements.
998 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
999 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001000 if (OpVal.getNode() == 0)
Chris Lattner2771e2c2006-03-25 06:12:06 +00001001 OpVal = N->getOperand(i);
1002 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001003 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001004 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001005
Gabor Greiff304a7a2008-08-28 21:40:38 +00001006 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001007
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001008 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001009 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001010 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001011 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001012 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001013 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001014 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001015 }
1016
1017 // If the splat value is larger than the element value, then we can never do
1018 // this splat. The only case that we could fit the replicated bits into our
1019 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001020 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001021
Chris Lattner2771e2c2006-03-25 06:12:06 +00001022 // If the element value is larger than the splat value, cut it in half and
1023 // check to see if the two halves are equal. Continue doing this until we
1024 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
1025 while (ValSizeInBytes > ByteSize) {
1026 ValSizeInBytes >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001027
Chris Lattner2771e2c2006-03-25 06:12:06 +00001028 // If the top half equals the bottom half, we're still ok.
Chris Lattner39cc7172006-04-05 17:39:25 +00001029 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
1030 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001031 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001032 }
1033
1034 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001035 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001036
Evan Chengb1ddc982006-03-26 09:52:32 +00001037 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001038 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001039
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001040 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001041 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +00001042 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001043 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001044}
1045
Chris Lattner4211ca92006-04-14 06:01:58 +00001046//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001047// Addressing Mode Selection
1048//===----------------------------------------------------------------------===//
1049
1050/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1051/// or 64-bit immediate, and if the value can be accurately represented as a
1052/// sign extension from a 16-bit value. If so, this returns true and the
1053/// immediate.
1054static bool isIntS16Immediate(SDNode *N, short &Imm) {
1055 if (N->getOpcode() != ISD::Constant)
1056 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001057
Dan Gohmaneffb8942008-09-12 16:56:44 +00001058 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001059 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001060 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001061 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001062 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001063}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001064static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001065 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001066}
1067
1068
1069/// SelectAddressRegReg - Given the specified addressed, check to see if it
1070/// can be represented as an indexed [r+r] operation. Returns false if it
1071/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001072bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1073 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001074 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001075 short imm = 0;
1076 if (N.getOpcode() == ISD::ADD) {
1077 if (isIntS16Immediate(N.getOperand(1), imm))
1078 return false; // r+i
1079 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1080 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001081
Chris Lattnera801fced2006-11-08 02:15:41 +00001082 Base = N.getOperand(0);
1083 Index = N.getOperand(1);
1084 return true;
1085 } else if (N.getOpcode() == ISD::OR) {
1086 if (isIntS16Immediate(N.getOperand(1), imm))
1087 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001088
Chris Lattnera801fced2006-11-08 02:15:41 +00001089 // If this is an or of disjoint bitfields, we can codegen this as an add
1090 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1091 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001092 APInt LHSKnownZero, LHSKnownOne;
1093 APInt RHSKnownZero, RHSKnownOne;
1094 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001095 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001096
Dan Gohmanf19609a2008-02-27 01:23:58 +00001097 if (LHSKnownZero.getBoolValue()) {
1098 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001099 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001100 // If all of the bits are known zero on the LHS or RHS, the add won't
1101 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001102 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001103 Base = N.getOperand(0);
1104 Index = N.getOperand(1);
1105 return true;
1106 }
1107 }
1108 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001109
Chris Lattnera801fced2006-11-08 02:15:41 +00001110 return false;
1111}
1112
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001113// If we happen to be doing an i64 load or store into a stack slot that has
1114// less than a 4-byte alignment, then the frame-index elimination may need to
1115// use an indexed load or store instruction (because the offset may not be a
1116// multiple of 4). The extra register needed to hold the offset comes from the
1117// register scavenger, and it is possible that the scavenger will need to use
1118// an emergency spill slot. As a result, we need to make sure that a spill slot
1119// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1120// stack slot.
1121static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1122 // FIXME: This does not handle the LWA case.
1123 if (VT != MVT::i64)
1124 return;
1125
Hal Finkel7ab3db52013-07-10 15:29:01 +00001126 // NOTE: We'll exclude negative FIs here, which come from argument
1127 // lowering, because there are no known test cases triggering this problem
1128 // using packed structures (or similar). We can remove this exclusion if
1129 // we find such a test case. The reason why this is so test-case driven is
1130 // because this entire 'fixup' is only to prevent crashes (from the
1131 // register scavenger) on not-really-valid inputs. For example, if we have:
1132 // %a = alloca i1
1133 // %b = bitcast i1* %a to i64*
1134 // store i64* a, i64 b
1135 // then the store should really be marked as 'align 1', but is not. If it
1136 // were marked as 'align 1' then the indexed form would have been
1137 // instruction-selected initially, and the problem this 'fixup' is preventing
1138 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001139 if (FrameIdx < 0)
1140 return;
1141
1142 MachineFunction &MF = DAG.getMachineFunction();
1143 MachineFrameInfo *MFI = MF.getFrameInfo();
1144
1145 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1146 if (Align >= 4)
1147 return;
1148
1149 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1150 FuncInfo->setHasNonRISpills();
1151}
1152
Chris Lattnera801fced2006-11-08 02:15:41 +00001153/// Returns true if the address N can be represented by a base register plus
1154/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001155/// represented as reg+reg. If Aligned is true, only accept displacements
1156/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001157bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001158 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001159 SelectionDAG &DAG,
1160 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001161 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001162 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001163 // If this can be more profitably realized as r+r, fail.
1164 if (SelectAddressRegReg(N, Disp, Base, DAG))
1165 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001166
Chris Lattnera801fced2006-11-08 02:15:41 +00001167 if (N.getOpcode() == ISD::ADD) {
1168 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001169 if (isIntS16Immediate(N.getOperand(1), imm) &&
1170 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001171 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001172 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1173 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001174 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001175 } else {
1176 Base = N.getOperand(0);
1177 }
1178 return true; // [r+i]
1179 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1180 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001181 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001182 && "Cannot handle constant offsets yet!");
1183 Disp = N.getOperand(1).getOperand(0); // The global address.
1184 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001185 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001186 Disp.getOpcode() == ISD::TargetConstantPool ||
1187 Disp.getOpcode() == ISD::TargetJumpTable);
1188 Base = N.getOperand(0);
1189 return true; // [&g+r]
1190 }
1191 } else if (N.getOpcode() == ISD::OR) {
1192 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001193 if (isIntS16Immediate(N.getOperand(1), imm) &&
1194 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001195 // If this is an or of disjoint bitfields, we can codegen this as an add
1196 // (for better address arithmetic) if the LHS and RHS of the OR are
1197 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001198 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001199 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001200
Dan Gohmanf19609a2008-02-27 01:23:58 +00001201 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001202 // If all of the bits are known zero on the LHS or RHS, the add won't
1203 // carry.
1204 Base = N.getOperand(0);
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001205 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001206 return true;
1207 }
1208 }
1209 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1210 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001211
Chris Lattnera801fced2006-11-08 02:15:41 +00001212 // If this address fits entirely in a 16-bit sext immediate field, codegen
1213 // this as "d, 0"
1214 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001215 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001216 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkelf70c41e2013-03-21 23:45:03 +00001217 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1218 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001219 return true;
1220 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001221
1222 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001223 if ((CN->getValueType(0) == MVT::i32 ||
1224 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1225 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001226 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001227
Chris Lattnera801fced2006-11-08 02:15:41 +00001228 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001229 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001230
Owen Anderson9f944592009-08-11 20:47:22 +00001231 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1232 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001233 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001234 return true;
1235 }
1236 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001237
Chris Lattnera801fced2006-11-08 02:15:41 +00001238 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001239 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001240 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001241 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1242 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001243 Base = N;
1244 return true; // [r+0]
1245}
1246
1247/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1248/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001249bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1250 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001251 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001252 // Check to see if we can easily represent this as an [r+r] address. This
1253 // will fail if it thinks that the address is more profitably represented as
1254 // reg+imm, e.g. where imm = 0.
1255 if (SelectAddressRegReg(N, Base, Index, DAG))
1256 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001257
Chris Lattnera801fced2006-11-08 02:15:41 +00001258 // If the operand is an addition, always emit this as [r+r], since this is
1259 // better (for code size, and execution, as the memop does the add for free)
1260 // than emitting an explicit add.
1261 if (N.getOpcode() == ISD::ADD) {
1262 Base = N.getOperand(0);
1263 Index = N.getOperand(1);
1264 return true;
1265 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001266
Chris Lattnera801fced2006-11-08 02:15:41 +00001267 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkelf70c41e2013-03-21 23:45:03 +00001268 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1269 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001270 Index = N;
1271 return true;
1272}
1273
Chris Lattnera801fced2006-11-08 02:15:41 +00001274/// getPreIndexedAddressParts - returns true by value, base pointer and
1275/// offset pointer and addressing mode by reference if the node's address
1276/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001277bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1278 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001279 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001280 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001281 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001282
Ulrich Weigande90b0222013-03-22 14:58:48 +00001283 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001284 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001285 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001286 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001287 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1288 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001289 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001290 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001291 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001292 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001293 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001294 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001295 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001296 } else
1297 return false;
1298
Chris Lattner68371252006-11-14 01:38:31 +00001299 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands13237ac2008-06-06 12:08:01 +00001300 if (VT.isVector())
Chris Lattner68371252006-11-14 01:38:31 +00001301 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001302
Ulrich Weigande90b0222013-03-22 14:58:48 +00001303 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1304
1305 // Common code will reject creating a pre-inc form if the base pointer
1306 // is a frame index, or if N is a store and the base pointer is either
1307 // the same as or a predecessor of the value being stored. Check for
1308 // those situations here, and try with swapped Base/Offset instead.
1309 bool Swap = false;
1310
1311 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1312 Swap = true;
1313 else if (!isLoad) {
1314 SDValue Val = cast<StoreSDNode>(N)->getValue();
1315 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1316 Swap = true;
1317 }
1318
1319 if (Swap)
1320 std::swap(Base, Offset);
1321
Hal Finkelca542be2012-06-20 15:43:03 +00001322 AM = ISD::PRE_INC;
1323 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001324 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001325
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001326 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001327 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001328 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001329 return false;
1330 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001331 // LDU/STU need an address with at least 4-byte alignment.
1332 if (Alignment < 4)
1333 return false;
1334
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001335 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001336 return false;
1337 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001338
Chris Lattnerb314b152006-11-11 00:08:42 +00001339 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001340 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1341 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001342 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001343 LD->getExtensionType() == ISD::SEXTLOAD &&
1344 isa<ConstantSDNode>(Offset))
1345 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001346 }
1347
Chris Lattnerce645542006-11-10 02:08:47 +00001348 AM = ISD::PRE_INC;
1349 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001350}
1351
1352//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001353// LowerOperation implementation
1354//===----------------------------------------------------------------------===//
1355
Chris Lattneredb9d842010-11-15 02:46:57 +00001356/// GetLabelAccessInfo - Return true if we should reference labels using a
1357/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1358static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Chris Lattnerdd6df842010-11-15 03:13:19 +00001359 unsigned &LoOpFlags, const GlobalValue *GV = 0) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001360 HiOpFlags = PPCII::MO_HA;
1361 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001362
Chris Lattneredb9d842010-11-15 02:46:57 +00001363 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1364 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peck527da1b2010-11-23 03:31:01 +00001365 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattneredb9d842010-11-15 02:46:57 +00001366 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattnerdd6df842010-11-15 03:13:19 +00001367 if (isPIC) {
1368 HiOpFlags |= PPCII::MO_PIC_FLAG;
1369 LoOpFlags |= PPCII::MO_PIC_FLAG;
1370 }
1371
1372 // If this is a reference to a global value that requires a non-lazy-ptr, make
1373 // sure that instruction lowering adds it.
1374 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1375 HiOpFlags |= PPCII::MO_NLP_FLAG;
1376 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001377
Chris Lattnerdd6df842010-11-15 03:13:19 +00001378 if (GV->hasHiddenVisibility()) {
1379 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1380 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1381 }
1382 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001383
Chris Lattneredb9d842010-11-15 02:46:57 +00001384 return isPIC;
1385}
1386
1387static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1388 SelectionDAG &DAG) {
1389 EVT PtrVT = HiPart.getValueType();
1390 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001391 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001392
1393 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1394 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001395
Chris Lattneredb9d842010-11-15 02:46:57 +00001396 // With PIC, the first instruction is actually "GR+hi(&G)".
1397 if (isPIC)
1398 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1399 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001400
Chris Lattneredb9d842010-11-15 02:46:57 +00001401 // Generate non-pic code that has direct accesses to the constant pool.
1402 // The address of the global is just (hi(&g)+lo(&g)).
1403 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1404}
1405
Scott Michelcf0da6c2009-02-17 22:15:04 +00001406SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001407 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001408 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001409 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001410 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001411
Roman Divackyace47072012-08-24 16:26:02 +00001412 // 64-bit SVR4 ABI code is always position-independent.
1413 // The actual address of the GlobalValue is stored in the TOC.
1414 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1415 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001416 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001417 DAG.getRegister(PPC::X2, MVT::i64));
1418 }
1419
Chris Lattneredb9d842010-11-15 02:46:57 +00001420 unsigned MOHiFlag, MOLoFlag;
1421 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1422 SDValue CPIHi =
1423 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1424 SDValue CPILo =
1425 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1426 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001427}
1428
Dan Gohman21cea8a2010-04-17 15:26:15 +00001429SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001430 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001431 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001432
Roman Divackyace47072012-08-24 16:26:02 +00001433 // 64-bit SVR4 ABI code is always position-independent.
1434 // The actual address of the GlobalValue is stored in the TOC.
1435 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1436 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001437 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001438 DAG.getRegister(PPC::X2, MVT::i64));
1439 }
1440
Chris Lattneredb9d842010-11-15 02:46:57 +00001441 unsigned MOHiFlag, MOLoFlag;
1442 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1443 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1444 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1445 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001446}
1447
Dan Gohman21cea8a2010-04-17 15:26:15 +00001448SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1449 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001450 EVT PtrVT = Op.getValueType();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001451
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001452 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peck527da1b2010-11-23 03:31:01 +00001453
Chris Lattneredb9d842010-11-15 02:46:57 +00001454 unsigned MOHiFlag, MOLoFlag;
1455 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001456 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1457 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001458 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1459}
1460
Roman Divackye3f15c982012-06-04 17:36:38 +00001461SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1462 SelectionDAG &DAG) const {
1463
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001464 // FIXME: TLS addresses currently use medium model code sequences,
1465 // which is the most useful form. Eventually support for small and
1466 // large models could be added if users need it, at the cost of
1467 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001468 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001469 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001470 const GlobalValue *GV = GA->getGlobal();
1471 EVT PtrVT = getPointerTy();
1472 bool is64bit = PPCSubTarget.isPPC64();
1473
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001474 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001475
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001476 if (Model == TLSModel::LocalExec) {
1477 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001478 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001479 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001480 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001481 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1482 is64bit ? MVT::i64 : MVT::i32);
1483 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1484 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1485 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001486
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001487 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001488 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001489 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1490 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00001491 SDValue GOTPtr;
1492 if (is64bit) {
1493 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1494 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1495 PtrVT, GOTReg, TGA);
1496 } else
1497 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001498 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00001499 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001500 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001501 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001502
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001503 if (Model == TLSModel::GeneralDynamic) {
1504 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1505 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1506 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1507 GOTReg, TGA);
1508 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1509 GOTEntryHi, TGA);
1510
1511 // We need a chain node, and don't have one handy. The underlying
1512 // call has no side effects, so using the function entry node
1513 // suffices.
1514 SDValue Chain = DAG.getEntryNode();
1515 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1516 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1517 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1518 PtrVT, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001519 // The return value from GET_TLS_ADDR really is in X3 already, but
1520 // some hacks are needed here to tie everything together. The extra
1521 // copies dissolve during subsequent transforms.
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001522 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1523 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1524 }
1525
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001526 if (Model == TLSModel::LocalDynamic) {
1527 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1528 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1529 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1530 GOTReg, TGA);
1531 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1532 GOTEntryHi, TGA);
1533
1534 // We need a chain node, and don't have one handy. The underlying
1535 // call has no side effects, so using the function entry node
1536 // suffices.
1537 SDValue Chain = DAG.getEntryNode();
1538 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1539 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1540 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1541 PtrVT, ParmReg, TGA);
1542 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1543 // some hacks are needed here to tie everything together. The extra
1544 // copies dissolve during subsequent transforms.
1545 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1546 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt9ed4dbc2012-12-13 20:57:10 +00001547 Chain, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001548 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1549 }
1550
1551 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00001552}
1553
Chris Lattneredb9d842010-11-15 02:46:57 +00001554SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1555 SelectionDAG &DAG) const {
1556 EVT PtrVT = Op.getValueType();
1557 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001558 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00001559 const GlobalValue *GV = GSDN->getGlobal();
1560
Chris Lattneredb9d842010-11-15 02:46:57 +00001561 // 64-bit SVR4 ABI code is always position-independent.
1562 // The actual address of the GlobalValue is stored in the TOC.
1563 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1564 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1565 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1566 DAG.getRegister(PPC::X2, MVT::i64));
1567 }
1568
Chris Lattnerdd6df842010-11-15 03:13:19 +00001569 unsigned MOHiFlag, MOLoFlag;
1570 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00001571
Chris Lattnerdd6df842010-11-15 03:13:19 +00001572 SDValue GAHi =
1573 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1574 SDValue GALo =
1575 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00001576
Chris Lattnerdd6df842010-11-15 03:13:19 +00001577 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00001578
Chris Lattnerdd6df842010-11-15 03:13:19 +00001579 // If the global reference is actually to a non-lazy-pointer, we have to do an
1580 // extra load to get the address of the global.
1581 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1582 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001583 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00001584 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00001585}
1586
Dan Gohman21cea8a2010-04-17 15:26:15 +00001587SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00001588 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001589 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001590
Chris Lattner4211ca92006-04-14 06:01:58 +00001591 // If we're comparing for equality to zero, expose the fact that this is
1592 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1593 // fold the new nodes.
1594 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1595 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001596 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001597 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001598 if (VT.bitsLT(MVT::i32)) {
1599 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001600 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001601 }
Duncan Sands13237ac2008-06-06 12:08:01 +00001602 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001603 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1604 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00001605 DAG.getConstant(Log2b, MVT::i32));
1606 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00001607 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001608 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00001609 // optimized. FIXME: revisit this when we can custom lower all setcc
1610 // optimizations.
1611 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001612 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001613 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001614
Chris Lattner4211ca92006-04-14 06:01:58 +00001615 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00001616 // by xor'ing the rhs with the lhs, which is faster than setting a
1617 // condition register, reading it back out, and masking the correct bit. The
1618 // normal approach here uses sub to do this instead of xor. Using xor exposes
1619 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001620 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00001621 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001622 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001623 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00001624 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001625 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00001626 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001627 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001628}
1629
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001630SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001631 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00001632 SDNode *Node = Op.getNode();
1633 EVT VT = Node->getValueType(0);
1634 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1635 SDValue InChain = Node->getOperand(0);
1636 SDValue VAListPtr = Node->getOperand(1);
1637 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001638 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001639
Roman Divacky4394e682011-06-28 15:30:42 +00001640 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1641
1642 // gpr_index
1643 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1644 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1645 false, false, 0);
1646 InChain = GprIndex.getValue(1);
1647
1648 if (VT == MVT::i64) {
1649 // Check if GprIndex is even
1650 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1651 DAG.getConstant(1, MVT::i32));
1652 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1653 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1654 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1655 DAG.getConstant(1, MVT::i32));
1656 // Align GprIndex to be even if it isn't
1657 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1658 GprIndex);
1659 }
1660
1661 // fpr index is 1 byte after gpr
1662 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1663 DAG.getConstant(1, MVT::i32));
1664
1665 // fpr
1666 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1667 FprPtr, MachinePointerInfo(SV), MVT::i8,
1668 false, false, 0);
1669 InChain = FprIndex.getValue(1);
1670
1671 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1672 DAG.getConstant(8, MVT::i32));
1673
1674 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1675 DAG.getConstant(4, MVT::i32));
1676
1677 // areas
1678 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001679 MachinePointerInfo(), false, false,
1680 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001681 InChain = OverflowArea.getValue(1);
1682
1683 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001684 MachinePointerInfo(), false, false,
1685 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001686 InChain = RegSaveArea.getValue(1);
1687
1688 // select overflow_area if index > 8
1689 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1690 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1691
Roman Divacky4394e682011-06-28 15:30:42 +00001692 // adjustment constant gpr_index * 4/8
1693 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1694 VT.isInteger() ? GprIndex : FprIndex,
1695 DAG.getConstant(VT.isInteger() ? 4 : 8,
1696 MVT::i32));
1697
1698 // OurReg = RegSaveArea + RegConstant
1699 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1700 RegConstant);
1701
1702 // Floating types are 32 bytes into RegSaveArea
1703 if (VT.isFloatingPoint())
1704 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1705 DAG.getConstant(32, MVT::i32));
1706
1707 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1708 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1709 VT.isInteger() ? GprIndex : FprIndex,
1710 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1711 MVT::i32));
1712
1713 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1714 VT.isInteger() ? VAListPtr : FprPtr,
1715 MachinePointerInfo(SV),
1716 MVT::i8, false, false, 0);
1717
1718 // determine if we should load from reg_save_area or overflow_area
1719 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1720
1721 // increase overflow_area by 4/8 if gpr/fpr > 8
1722 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1723 DAG.getConstant(VT.isInteger() ? 4 : 8,
1724 MVT::i32));
1725
1726 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1727 OverflowAreaPlusN);
1728
1729 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1730 OverflowAreaPtr,
1731 MachinePointerInfo(),
1732 MVT::i32, false, false, 0);
1733
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00001734 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001735 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001736}
1737
Roman Divackyc3825df2013-07-25 21:36:47 +00001738SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
1739 const PPCSubtarget &Subtarget) const {
1740 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
1741
1742 // We have to copy the entire va_list struct:
1743 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
1744 return DAG.getMemcpy(Op.getOperand(0), Op,
1745 Op.getOperand(1), Op.getOperand(2),
1746 DAG.getConstant(12, MVT::i32), 8, false, true,
1747 MachinePointerInfo(), MachinePointerInfo());
1748}
1749
Duncan Sandsa0984362011-09-06 13:37:06 +00001750SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1751 SelectionDAG &DAG) const {
1752 return Op.getOperand(0);
1753}
1754
1755SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1756 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00001757 SDValue Chain = Op.getOperand(0);
1758 SDValue Trmp = Op.getOperand(1); // trampoline
1759 SDValue FPtr = Op.getOperand(2); // nested function
1760 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00001761 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00001762
Owen Anderson53aa7a92009-08-10 22:56:29 +00001763 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00001764 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00001765 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001766 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00001767 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00001768
Scott Michelcf0da6c2009-02-17 22:15:04 +00001769 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00001770 TargetLowering::ArgListEntry Entry;
1771
1772 Entry.Ty = IntPtrTy;
1773 Entry.Node = Trmp; Args.push_back(Entry);
1774
1775 // TrampSize == (isPPC64 ? 48 : 40);
1776 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00001777 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00001778 Args.push_back(Entry);
1779
1780 Entry.Node = FPtr; Args.push_back(Entry);
1781 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001782
Bill Wendling95e1af22008-09-17 00:30:57 +00001783 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Justin Holewinskiaa583972012-05-25 16:35:28 +00001784 TargetLowering::CallLoweringInfo CLI(Chain,
1785 Type::getVoidTy(*DAG.getContext()),
1786 false, false, false, false, 0,
1787 CallingConv::C,
Evan Cheng65f9d192012-02-28 18:51:51 +00001788 /*isTailCall=*/false,
Justin Holewinskiaa583972012-05-25 16:35:28 +00001789 /*doesNotRet=*/false,
1790 /*isReturnValueUsed=*/true,
Bill Wendling95e1af22008-09-17 00:30:57 +00001791 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00001792 Args, DAG, dl);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001793 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bill Wendling95e1af22008-09-17 00:30:57 +00001794
Duncan Sandsa0984362011-09-06 13:37:06 +00001795 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00001796}
1797
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001798SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001799 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001800 MachineFunction &MF = DAG.getMachineFunction();
1801 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1802
Andrew Trickef9de2a2013-05-25 02:42:55 +00001803 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001804
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001805 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001806 // vastart just stores the address of the VarArgsFrameIndex slot into the
1807 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001808 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00001809 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001810 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00001811 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1812 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00001813 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001814 }
1815
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001816 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001817 // We suppose the given va_list is already allocated.
1818 //
1819 // typedef struct {
1820 // char gpr; /* index into the array of 8 GPRs
1821 // * stored in the register save area
1822 // * gpr=0 corresponds to r3,
1823 // * gpr=1 to r4, etc.
1824 // */
1825 // char fpr; /* index into the array of 8 FPRs
1826 // * stored in the register save area
1827 // * fpr=0 corresponds to f1,
1828 // * fpr=1 to f2, etc.
1829 // */
1830 // char *overflow_arg_area;
1831 // /* location on stack that holds
1832 // * the next overflow argument
1833 // */
1834 // char *reg_save_area;
1835 // /* where r3:r10 and f1:f8 (if saved)
1836 // * are stored
1837 // */
1838 // } va_list[1];
1839
1840
Dan Gohman31ae5862010-04-17 14:41:14 +00001841 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1842 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001843
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001844
Owen Anderson53aa7a92009-08-10 22:56:29 +00001845 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001846
Dan Gohman31ae5862010-04-17 14:41:14 +00001847 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1848 PtrVT);
1849 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1850 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001851
Duncan Sands13237ac2008-06-06 12:08:01 +00001852 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001853 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001854
Duncan Sands13237ac2008-06-06 12:08:01 +00001855 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001856 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001857
1858 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001859 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001860
Dan Gohman2d489b52008-02-06 22:27:42 +00001861 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001862
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001863 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001864 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00001865 Op.getOperand(1),
1866 MachinePointerInfo(SV),
1867 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001868 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001869 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001870 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001871
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001872 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001873 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00001874 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1875 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00001876 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001877 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001878 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001879
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001880 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001881 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00001882 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1883 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001884 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001885 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001886 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001887
1888 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00001889 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1890 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001891 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001892
Chris Lattner4211ca92006-04-14 06:01:58 +00001893}
1894
Chris Lattner4f2e4e02007-03-06 00:59:59 +00001895#include "PPCGenCallingConv.inc"
1896
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001897// Function whose sole purpose is to kill compiler warnings
1898// stemming from unused functions included from PPCGenCallingConv.inc.
1899CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001900 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001901}
1902
Bill Schmidt230b4512013-06-12 16:39:22 +00001903bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1904 CCValAssign::LocInfo &LocInfo,
1905 ISD::ArgFlagsTy &ArgFlags,
1906 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001907 return true;
1908}
1909
Bill Schmidt230b4512013-06-12 16:39:22 +00001910bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
1911 MVT &LocVT,
1912 CCValAssign::LocInfo &LocInfo,
1913 ISD::ArgFlagsTy &ArgFlags,
1914 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00001915 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001916 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1917 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1918 };
1919 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00001920
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001921 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1922
1923 // Skip one register if the first unallocated register has an even register
1924 // number and there are still argument registers available which have not been
1925 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1926 // need to skip a register if RegNum is odd.
1927 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1928 State.AllocateReg(ArgRegs[RegNum]);
1929 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001930
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001931 // Always return false here, as this function only makes sure that the first
1932 // unallocated register has an odd register number and does not actually
1933 // allocate a register for the current argument.
1934 return false;
1935}
1936
Bill Schmidt230b4512013-06-12 16:39:22 +00001937bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
1938 MVT &LocVT,
1939 CCValAssign::LocInfo &LocInfo,
1940 ISD::ArgFlagsTy &ArgFlags,
1941 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00001942 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001943 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1944 PPC::F8
1945 };
1946
1947 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00001948
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001949 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1950
1951 // If there is only one Floating-point register left we need to put both f64
1952 // values of a split ppc_fp128 value on the stack.
1953 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1954 State.AllocateReg(ArgRegs[RegNum]);
1955 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001956
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001957 // Always return false here, as this function only makes sure that the two f64
1958 // values a ppc_fp128 value is split into are both passed in registers or both
1959 // passed on the stack and does not actually allocate a register for the
1960 // current argument.
1961 return false;
1962}
1963
Chris Lattner43df5b32007-02-25 05:34:32 +00001964/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001965/// on Darwin.
Craig Topperca658c22012-03-11 07:16:55 +00001966static const uint16_t *GetFPR() {
1967 static const uint16_t FPR[] = {
Chris Lattner43df5b32007-02-25 05:34:32 +00001968 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001969 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner43df5b32007-02-25 05:34:32 +00001970 };
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001971
Chris Lattner43df5b32007-02-25 05:34:32 +00001972 return FPR;
1973}
1974
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00001975/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1976/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001977static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00001978 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00001979 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00001980 if (Flags.isByVal())
1981 ArgSize = Flags.getByValSize();
1982 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1983
1984 return ArgSize;
1985}
1986
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001987SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001988PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001989 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001990 const SmallVectorImpl<ISD::InputArg>
1991 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001992 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001993 SmallVectorImpl<SDValue> &InVals)
1994 const {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00001995 if (PPCSubTarget.isSVR4ABI()) {
1996 if (PPCSubTarget.isPPC64())
1997 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
1998 dl, DAG, InVals);
1999 else
2000 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2001 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002002 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002003 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2004 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002005 }
2006}
2007
2008SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002009PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002010 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002011 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002012 const SmallVectorImpl<ISD::InputArg>
2013 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002014 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002015 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002016
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002017 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002018 // +-----------------------------------+
2019 // +--> | Back chain |
2020 // | +-----------------------------------+
2021 // | | Floating-point register save area |
2022 // | +-----------------------------------+
2023 // | | General register save area |
2024 // | +-----------------------------------+
2025 // | | CR save word |
2026 // | +-----------------------------------+
2027 // | | VRSAVE save word |
2028 // | +-----------------------------------+
2029 // | | Alignment padding |
2030 // | +-----------------------------------+
2031 // | | Vector register save area |
2032 // | +-----------------------------------+
2033 // | | Local variable space |
2034 // | +-----------------------------------+
2035 // | | Parameter list area |
2036 // | +-----------------------------------+
2037 // | | LR save word |
2038 // | +-----------------------------------+
2039 // SP--> +--- | Back chain |
2040 // +-----------------------------------+
2041 //
2042 // Specifications:
2043 // System V Application Binary Interface PowerPC Processor Supplement
2044 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002045
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002046 MachineFunction &MF = DAG.getMachineFunction();
2047 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002048 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002049
Owen Anderson53aa7a92009-08-10 22:56:29 +00002050 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002051 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002052 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2053 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002054 unsigned PtrByteSize = 4;
2055
2056 // Assign locations to all of the incoming arguments.
2057 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002058 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002059 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002060
2061 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002062 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002063
Bill Schmidtef17c142013-02-06 17:33:58 +00002064 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002065
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002066 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2067 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002068
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002069 // Arguments stored in registers.
2070 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002071 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002072 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002073
Owen Anderson9f944592009-08-11 20:47:22 +00002074 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002075 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002076 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002077 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002078 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002079 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002080 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002081 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00002082 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002083 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002084 case MVT::f64:
Craig Topperabadc662012-04-20 06:31:50 +00002085 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002086 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002087 case MVT::v16i8:
2088 case MVT::v8i16:
2089 case MVT::v4i32:
2090 case MVT::v4f32:
Craig Topperabadc662012-04-20 06:31:50 +00002091 RC = &PPC::VRRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002092 break;
2093 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002094
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002095 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002096 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002097 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2098 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2099
2100 if (ValVT == MVT::i1)
2101 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002102
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002103 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002104 } else {
2105 // Argument stored in memory.
2106 assert(VA.isMemLoc());
2107
Hal Finkel940ab932014-02-28 00:27:01 +00002108 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002109 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002110 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002111
2112 // Create load nodes to retrieve arguments from the stack.
2113 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002114 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2115 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002116 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002117 }
2118 }
2119
2120 // Assign locations to all of the incoming aggregate by value arguments.
2121 // Aggregates passed by value are stored in the local variable space of the
2122 // caller's stack frame, right above the parameter list area.
2123 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002124 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002125 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002126
2127 // Reserve stack space for the allocations in CCInfo.
2128 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2129
Bill Schmidtef17c142013-02-06 17:33:58 +00002130 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002131
2132 // Area that is at least reserved in the caller of this function.
2133 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00002134
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002135 // Set the size that is at least reserved in caller of this function. Tail
2136 // call optimized function's reserved stack space needs to be aligned so that
2137 // taking the difference between two stack areas will result in an aligned
2138 // stack.
2139 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2140
2141 MinReservedArea =
2142 std::max(MinReservedArea,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002143 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peck527da1b2010-11-23 03:31:01 +00002144
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002145 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002146 getStackAlignment();
2147 unsigned AlignMask = TargetAlign-1;
2148 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peck527da1b2010-11-23 03:31:01 +00002149
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002150 FI->setMinReservedArea(MinReservedArea);
2151
2152 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002153
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002154 // If the function takes variable number of arguments, make a frame index for
2155 // the start of the first vararg value... for expansion of llvm.va_start.
2156 if (isVarArg) {
Craig Topperbef78fc2012-03-11 07:57:25 +00002157 static const uint16_t GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002158 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2159 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2160 };
2161 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2162
Craig Topperbef78fc2012-03-11 07:57:25 +00002163 static const uint16_t FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002164 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2165 PPC::F8
2166 };
2167 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2168
Dan Gohman31ae5862010-04-17 14:41:14 +00002169 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2170 NumGPArgRegs));
2171 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2172 NumFPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002173
2174 // Make room for NumGPArgRegs and NumFPArgRegs.
2175 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson9f944592009-08-11 20:47:22 +00002176 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002177
Dan Gohman31ae5862010-04-17 14:41:14 +00002178 FuncInfo->setVarArgsStackOffset(
2179 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002180 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002181
Dan Gohman31ae5862010-04-17 14:41:14 +00002182 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2183 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002184
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002185 // The fixed integer arguments of a variadic function are stored to the
2186 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2187 // the result of va_next.
2188 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2189 // Get an existing live-in vreg, or add a new one.
2190 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2191 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002192 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002193
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002194 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002195 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2196 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002197 MemOps.push_back(Store);
2198 // Increment the address by four for the next argument to store
2199 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2200 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2201 }
2202
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002203 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2204 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002205 // The double arguments are stored to the VarArgsFrameIndex
2206 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002207 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2208 // Get an existing live-in vreg, or add a new one.
2209 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2210 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002211 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002212
Owen Anderson9f944592009-08-11 20:47:22 +00002213 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002214 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2215 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002216 MemOps.push_back(Store);
2217 // Increment the address by eight for the next argument to store
Owen Anderson9f944592009-08-11 20:47:22 +00002218 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002219 PtrVT);
2220 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2221 }
2222 }
2223
2224 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002225 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002226 MVT::Other, &MemOps[0], MemOps.size());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002227
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002228 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002229}
2230
Bill Schmidt57d6de52012-10-23 15:51:16 +00002231// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2232// value to MVT::i64 and then truncate to the correct register size.
2233SDValue
2234PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2235 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002236 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002237 if (Flags.isSExt())
2238 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2239 DAG.getValueType(ObjectVT));
2240 else if (Flags.isZExt())
2241 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2242 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002243
Hal Finkel940ab932014-02-28 00:27:01 +00002244 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002245}
2246
2247// Set the size that is at least reserved in caller of this function. Tail
2248// call optimized functions' reserved stack space needs to be aligned so that
2249// taking the difference between two stack areas will result in an aligned
2250// stack.
2251void
2252PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2253 unsigned nAltivecParamsAtEnd,
2254 unsigned MinReservedArea,
2255 bool isPPC64) const {
2256 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2257 // Add the Altivec parameters at the end, if needed.
2258 if (nAltivecParamsAtEnd) {
2259 MinReservedArea = ((MinReservedArea+15)/16)*16;
2260 MinReservedArea += 16*nAltivecParamsAtEnd;
2261 }
2262 MinReservedArea =
2263 std::max(MinReservedArea,
2264 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2265 unsigned TargetAlign
2266 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2267 getStackAlignment();
2268 unsigned AlignMask = TargetAlign-1;
2269 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2270 FI->setMinReservedArea(MinReservedArea);
2271}
2272
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002273SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002274PPCTargetLowering::LowerFormalArguments_64SVR4(
2275 SDValue Chain,
2276 CallingConv::ID CallConv, bool isVarArg,
2277 const SmallVectorImpl<ISD::InputArg>
2278 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002279 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002280 SmallVectorImpl<SDValue> &InVals) const {
2281 // TODO: add description of PPC stack frame format, or at least some docs.
2282 //
2283 MachineFunction &MF = DAG.getMachineFunction();
2284 MachineFrameInfo *MFI = MF.getFrameInfo();
2285 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2286
2287 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2288 // Potential tail calls could cause overwriting of argument stack slots.
2289 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2290 (CallConv == CallingConv::Fast));
2291 unsigned PtrByteSize = 8;
2292
2293 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2294 // Area that is at least reserved in caller of this function.
2295 unsigned MinReservedArea = ArgOffset;
2296
2297 static const uint16_t GPR[] = {
2298 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2299 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2300 };
2301
2302 static const uint16_t *FPR = GetFPR();
2303
2304 static const uint16_t VR[] = {
2305 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2306 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2307 };
2308
2309 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2310 const unsigned Num_FPR_Regs = 13;
2311 const unsigned Num_VR_Regs = array_lengthof(VR);
2312
2313 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2314
2315 // Add DAG nodes to load the arguments or copy them out of registers. On
2316 // entry to a function on PPC, the arguments start after the linkage area,
2317 // although the first ones are often in registers.
2318
2319 SmallVector<SDValue, 8> MemOps;
2320 unsigned nAltivecParamsAtEnd = 0;
2321 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002322 unsigned CurArgIdx = 0;
2323 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002324 SDValue ArgVal;
2325 bool needsLoad = false;
2326 EVT ObjectVT = Ins[ArgNo].VT;
Hal Finkel940ab932014-02-28 00:27:01 +00002327 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002328 unsigned ArgSize = ObjSize;
2329 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt6631e942013-02-20 17:31:41 +00002330 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2331 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002332
2333 unsigned CurArgOffset = ArgOffset;
2334
2335 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2336 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2337 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
2338 if (isVarArg) {
2339 MinReservedArea = ((MinReservedArea+15)/16)*16;
2340 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2341 Flags,
2342 PtrByteSize);
2343 } else
2344 nAltivecParamsAtEnd++;
2345 } else
2346 // Calculate min reserved area.
2347 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2348 Flags,
2349 PtrByteSize);
2350
2351 // FIXME the codegen can be much improved in some cases.
2352 // We do not have to keep everything in memory.
2353 if (Flags.isByVal()) {
2354 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2355 ObjSize = Flags.getByValSize();
2356 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002357 // Empty aggregate parameters do not take up registers. Examples:
2358 // struct { } a;
2359 // union { } b;
2360 // int c[0];
2361 // etc. However, we have to provide a place-holder in InVals, so
2362 // pretend we have an 8-byte item at the current address for that
2363 // purpose.
2364 if (!ObjSize) {
2365 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2366 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2367 InVals.push_back(FIN);
2368 continue;
2369 }
Hal Finkel262a2242013-09-12 23:20:06 +00002370
2371 unsigned BVAlign = Flags.getByValAlign();
2372 if (BVAlign > 8) {
2373 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
2374 CurArgOffset = ArgOffset;
2375 }
2376
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002377 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt48081ca2012-10-16 13:30:53 +00002378 if (ObjSize < PtrByteSize)
2379 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002380 // The value of the object is its address.
2381 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2382 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2383 InVals.push_back(FIN);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002384
2385 if (ObjSize < 8) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002386 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002387 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002388 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002389 SDValue Store;
2390
2391 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2392 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2393 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2394 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002395 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002396 ObjType, false, false, 0);
2397 } else {
2398 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2399 // store the whole register as-is to the parameter save area
2400 // slot. The address of the parameter was already calculated
2401 // above (InVals.push_back(FIN)) to be the right-justified
2402 // offset within the slot. For this store, we need a new
2403 // frame index that points at the beginning of the slot.
2404 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2405 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2406 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002407 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002408 false, false, 0);
2409 }
2410
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002411 MemOps.push_back(Store);
2412 ++GPR_idx;
2413 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002414 // Whether we copied from a register or not, advance the offset
2415 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002416 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002417 continue;
2418 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002419
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002420 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2421 // Store whatever pieces of the object are in registers
2422 // to memory. ArgOffset will be the address of the beginning
2423 // of the object.
2424 if (GPR_idx != Num_GPR_Regs) {
2425 unsigned VReg;
2426 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2427 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2428 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2429 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002430 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002431 MachinePointerInfo(FuncArg, j),
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002432 false, false, 0);
2433 MemOps.push_back(Store);
2434 ++GPR_idx;
2435 ArgOffset += PtrByteSize;
2436 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00002437 ArgOffset += ArgSize - j;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002438 break;
2439 }
2440 }
2441 continue;
2442 }
2443
2444 switch (ObjectVT.getSimpleVT().SimpleTy) {
2445 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00002446 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002447 case MVT::i32:
2448 case MVT::i64:
2449 if (GPR_idx != Num_GPR_Regs) {
2450 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2451 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2452
Hal Finkel940ab932014-02-28 00:27:01 +00002453 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002454 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2455 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002456 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002457
2458 ++GPR_idx;
2459 } else {
2460 needsLoad = true;
2461 ArgSize = PtrByteSize;
2462 }
2463 ArgOffset += 8;
2464 break;
2465
2466 case MVT::f32:
2467 case MVT::f64:
2468 // Every 8 bytes of argument space consumes one of the GPRs available for
2469 // argument passing.
2470 if (GPR_idx != Num_GPR_Regs) {
2471 ++GPR_idx;
2472 }
2473 if (FPR_idx != Num_FPR_Regs) {
2474 unsigned VReg;
2475
2476 if (ObjectVT == MVT::f32)
2477 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2478 else
2479 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
2480
2481 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2482 ++FPR_idx;
2483 } else {
2484 needsLoad = true;
Bill Schmidt22162472012-10-11 15:38:20 +00002485 ArgSize = PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002486 }
2487
2488 ArgOffset += 8;
2489 break;
2490 case MVT::v4f32:
2491 case MVT::v4i32:
2492 case MVT::v8i16:
2493 case MVT::v16i8:
2494 // Note that vector arguments in registers don't reserve stack space,
2495 // except in varargs functions.
2496 if (VR_idx != Num_VR_Regs) {
2497 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
2498 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2499 if (isVarArg) {
2500 while ((ArgOffset % 16) != 0) {
2501 ArgOffset += PtrByteSize;
2502 if (GPR_idx != Num_GPR_Regs)
2503 GPR_idx++;
2504 }
2505 ArgOffset += 16;
2506 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2507 }
2508 ++VR_idx;
2509 } else {
2510 // Vectors are aligned.
2511 ArgOffset = ((ArgOffset+15)/16)*16;
2512 CurArgOffset = ArgOffset;
2513 ArgOffset += 16;
2514 needsLoad = true;
2515 }
2516 break;
2517 }
2518
2519 // We need to load the argument to a virtual register if we determined
2520 // above that we ran out of physical registers of the appropriate type.
2521 if (needsLoad) {
2522 int FI = MFI->CreateFixedObject(ObjSize,
2523 CurArgOffset + (ArgSize - ObjSize),
2524 isImmutable);
2525 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2526 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2527 false, false, false, 0);
2528 }
2529
2530 InVals.push_back(ArgVal);
2531 }
2532
2533 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002534 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002535 // taking the difference between two stack areas will result in an aligned
2536 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002537 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002538
2539 // If the function takes variable number of arguments, make a frame index for
2540 // the start of the first vararg value... for expansion of llvm.va_start.
2541 if (isVarArg) {
2542 int Depth = ArgOffset;
2543
2544 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00002545 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002546 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2547
2548 // If this function is vararg, store any remaining integer argument regs
2549 // to their spots on the stack so that they may be loaded by deferencing the
2550 // result of va_next.
2551 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2552 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2553 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2554 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2555 MachinePointerInfo(), false, false, 0);
2556 MemOps.push_back(Store);
2557 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00002558 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002559 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2560 }
2561 }
2562
2563 if (!MemOps.empty())
2564 Chain = DAG.getNode(ISD::TokenFactor, dl,
2565 MVT::Other, &MemOps[0], MemOps.size());
2566
2567 return Chain;
2568}
2569
2570SDValue
2571PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002572 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002573 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002574 const SmallVectorImpl<ISD::InputArg>
2575 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002576 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002577 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002578 // TODO: add description of PPC stack frame format, or at least some docs.
2579 //
2580 MachineFunction &MF = DAG.getMachineFunction();
2581 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002582 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002583
Owen Anderson53aa7a92009-08-10 22:56:29 +00002584 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002585 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002586 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002587 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2588 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00002589 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey48850c12006-11-16 22:43:37 +00002590
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002591 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002592 // Area that is at least reserved in caller of this function.
2593 unsigned MinReservedArea = ArgOffset;
2594
Craig Topperca658c22012-03-11 07:16:55 +00002595 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002596 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2597 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2598 };
Craig Topperca658c22012-03-11 07:16:55 +00002599 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00002600 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2601 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2602 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00002603
Craig Topperca658c22012-03-11 07:16:55 +00002604 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002605
Craig Topperca658c22012-03-11 07:16:55 +00002606 static const uint16_t VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002607 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2608 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2609 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00002610
Owen Andersone2f23a32007-09-07 04:06:50 +00002611 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002612 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00002613 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00002614
2615 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002616
Craig Topperca658c22012-03-11 07:16:55 +00002617 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002618
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002619 // In 32-bit non-varargs functions, the stack space for vectors is after the
2620 // stack space for non-vectors. We do not use this space unless we have
2621 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00002622 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002623 // that out...for the pathological case, compute VecArgOffset as the
2624 // start of the vector parameter area. Computing VecArgOffset is the
2625 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002626 unsigned VecArgOffset = ArgOffset;
2627 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002628 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002629 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002630 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002631 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002632
Duncan Sandsd97eea32008-03-21 09:14:45 +00002633 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002634 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00002635 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002636 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002637 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2638 VecArgOffset += ArgSize;
2639 continue;
2640 }
2641
Owen Anderson9f944592009-08-11 20:47:22 +00002642 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002643 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002644 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002645 case MVT::i32:
2646 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002647 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002648 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002649 case MVT::i64: // PPC64
2650 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002651 // FIXME: We are guaranteed to be !isPPC64 at this point.
2652 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002653 VecArgOffset += 8;
2654 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002655 case MVT::v4f32:
2656 case MVT::v4i32:
2657 case MVT::v8i16:
2658 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002659 // Nothing to do, we're only looking at Nonvector args here.
2660 break;
2661 }
2662 }
2663 }
2664 // We've found where the vector parameter area in memory is. Skip the
2665 // first 12 parameters; these don't use that memory.
2666 VecArgOffset = ((VecArgOffset+15)/16)*16;
2667 VecArgOffset += 12*16;
2668
Chris Lattner4302e8f2006-05-16 18:18:50 +00002669 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00002670 // entry to a function on PPC, the arguments start after the linkage area,
2671 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00002672
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002673 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002674 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00002675 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002676 unsigned CurArgIdx = 0;
2677 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002678 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002679 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002680 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00002681 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00002682 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002683 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002684 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2685 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002686
Chris Lattner318f0d22006-05-16 18:51:52 +00002687 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002688
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002689 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002690 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2691 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002692 if (isVarArg || isPPC64) {
2693 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002694 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002695 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002696 PtrByteSize);
2697 } else nAltivecParamsAtEnd++;
2698 } else
2699 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002700 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002701 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002702 PtrByteSize);
2703
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002704 // FIXME the codegen can be much improved in some cases.
2705 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002706 if (Flags.isByVal()) {
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002707 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002708 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002709 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002710 // Objects of size 1 and 2 are right justified, everything else is
2711 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00002712 if (ObjSize==1 || ObjSize==2) {
2713 CurArgOffset = CurArgOffset + (4 - ObjSize);
2714 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002715 // The value of the object is its address.
Evan Cheng0664a672010-07-03 00:40:23 +00002716 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002717 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002718 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002719 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00002720 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002721 unsigned VReg;
2722 if (isPPC64)
2723 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2724 else
2725 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002726 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002727 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002728 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002729 MachinePointerInfo(FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002730 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00002731 MemOps.push_back(Store);
2732 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00002733 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002734
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002735 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00002736
Dale Johannesen21a8f142008-03-08 01:41:42 +00002737 continue;
2738 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002739 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2740 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002741 // to memory. ArgOffset will be the address of the beginning
2742 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002743 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002744 unsigned VReg;
2745 if (isPPC64)
2746 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2747 else
2748 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00002749 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002750 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002751 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002752 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002753 MachinePointerInfo(FuncArg, j),
David Greene87a5abe2010-02-15 16:56:53 +00002754 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002755 MemOps.push_back(Store);
2756 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002757 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002758 } else {
2759 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2760 break;
2761 }
2762 }
2763 continue;
2764 }
2765
Owen Anderson9f944592009-08-11 20:47:22 +00002766 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002767 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002768 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002769 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00002770 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00002771 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002772 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002773 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Bill Wendling968f32c2008-03-07 20:49:02 +00002774 ++GPR_idx;
2775 } else {
2776 needsLoad = true;
2777 ArgSize = PtrByteSize;
2778 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002779 // All int arguments reserve stack space in the Darwin ABI.
2780 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00002781 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002782 }
Bill Wendling968f32c2008-03-07 20:49:02 +00002783 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00002784 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00002785 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002786 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002787 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00002788
Hal Finkel940ab932014-02-28 00:27:01 +00002789 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00002790 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00002791 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002792 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00002793
Chris Lattnerec78cad2006-06-26 22:48:35 +00002794 ++GPR_idx;
2795 } else {
2796 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00002797 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002798 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002799 // All int arguments reserve stack space in the Darwin ABI.
2800 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002801 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002802
Owen Anderson9f944592009-08-11 20:47:22 +00002803 case MVT::f32:
2804 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00002805 // Every 4 bytes of argument space consumes one of the GPRs available for
2806 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002807 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002808 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00002809 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002810 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00002811 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002812 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002813 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002814
Owen Anderson9f944592009-08-11 20:47:22 +00002815 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00002816 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002817 else
Devang Patelf3292b22011-02-21 23:21:26 +00002818 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002819
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002820 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002821 ++FPR_idx;
2822 } else {
2823 needsLoad = true;
2824 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002825
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002826 // All FP arguments reserve stack space in the Darwin ABI.
2827 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002828 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002829 case MVT::v4f32:
2830 case MVT::v4i32:
2831 case MVT::v8i16:
2832 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00002833 // Note that vector arguments in registers don't reserve stack space,
2834 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002835 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002836 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002837 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00002838 if (isVarArg) {
2839 while ((ArgOffset % 16) != 0) {
2840 ArgOffset += PtrByteSize;
2841 if (GPR_idx != Num_GPR_Regs)
2842 GPR_idx++;
2843 }
2844 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002845 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00002846 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002847 ++VR_idx;
2848 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002849 if (!isVarArg && !isPPC64) {
2850 // Vectors go after all the nonvectors.
2851 CurArgOffset = VecArgOffset;
2852 VecArgOffset += 16;
2853 } else {
2854 // Vectors are aligned.
2855 ArgOffset = ((ArgOffset+15)/16)*16;
2856 CurArgOffset = ArgOffset;
2857 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00002858 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002859 needsLoad = true;
2860 }
2861 break;
2862 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002863
Chris Lattner4302e8f2006-05-16 18:18:50 +00002864 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002865 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002866 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002867 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002868 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00002869 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002870 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002871 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002872 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002873 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002874
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002875 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002876 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002877
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002878 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002879 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002880 // taking the difference between two stack areas will result in an aligned
2881 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002882 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002883
Chris Lattner4302e8f2006-05-16 18:18:50 +00002884 // If the function takes variable number of arguments, make a frame index for
2885 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002886 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002887 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002888
Dan Gohman31ae5862010-04-17 14:41:14 +00002889 FuncInfo->setVarArgsFrameIndex(
2890 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002891 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00002892 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002893
Chris Lattner4302e8f2006-05-16 18:18:50 +00002894 // If this function is vararg, store any remaining integer argument regs
2895 // to their spots on the stack so that they may be loaded by deferencing the
2896 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002897 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00002898 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00002899
Chris Lattner2cca3852006-11-18 01:57:19 +00002900 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00002901 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00002902 else
Devang Patelf3292b22011-02-21 23:21:26 +00002903 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00002904
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002905 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002906 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2907 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002908 MemOps.push_back(Store);
2909 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002910 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00002911 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002912 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002913 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002914
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002915 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002916 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002917 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002918
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002919 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002920}
2921
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002922/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
2923/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002924static unsigned
2925CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2926 bool isPPC64,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002927 bool isVarArg,
2928 unsigned CC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002929 const SmallVectorImpl<ISD::OutputArg>
2930 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002931 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002932 unsigned &nAltivecParamsAtEnd) {
2933 // Count how many bytes are to be pushed on the stack, including the linkage
2934 // area, and parameter passing area. We start with 24/48 bytes, which is
2935 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002936 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002937 unsigned NumOps = Outs.size();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002938 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2939
2940 // Add up all the space actually used.
2941 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2942 // they all go in registers, but we must reserve stack space for them for
2943 // possible use by the caller. In varargs or 64-bit calls, parameters are
2944 // assigned stack space in order, with padding so Altivec parameters are
2945 // 16-byte aligned.
2946 nAltivecParamsAtEnd = 0;
2947 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002948 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002949 EVT ArgVT = Outs[i].VT;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002950 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002951 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2952 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002953 if (!isVarArg && !isPPC64) {
2954 // Non-varargs Altivec parameters go after all the non-Altivec
2955 // parameters; handle those later so we know how much padding we need.
2956 nAltivecParamsAtEnd++;
2957 continue;
2958 }
2959 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2960 NumBytes = ((NumBytes+15)/16)*16;
2961 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002962 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002963 }
2964
2965 // Allow for Altivec parameters at the end, if needed.
2966 if (nAltivecParamsAtEnd) {
2967 NumBytes = ((NumBytes+15)/16)*16;
2968 NumBytes += 16*nAltivecParamsAtEnd;
2969 }
2970
2971 // The prolog code of the callee may store up to 8 GPR argument registers to
2972 // the stack, allowing va_start to index over them in memory if its varargs.
2973 // Because we cannot tell if this is needed on the caller side, we have to
2974 // conservatively assume that it is needed. As such, make sure we have at
2975 // least enough stack space for the caller to store the 8 GPRs.
2976 NumBytes = std::max(NumBytes,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002977 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002978
2979 // Tail call needs the stack to be aligned.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002980 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
2981 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
2982 getFrameLowering()->getStackAlignment();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002983 unsigned AlignMask = TargetAlign-1;
2984 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2985 }
2986
2987 return NumBytes;
2988}
2989
2990/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00002991/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00002992static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002993 unsigned ParamSize) {
2994
Dale Johannesen86dcae12009-11-24 01:09:07 +00002995 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002996
2997 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
2998 unsigned CallerMinReservedArea = FI->getMinReservedArea();
2999 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3000 // Remember only if the new adjustement is bigger.
3001 if (SPDiff < FI->getTailCallSPDelta())
3002 FI->setTailCallSPDelta(SPDiff);
3003
3004 return SPDiff;
3005}
3006
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003007/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3008/// for tail call optimization. Targets which want to do tail call
3009/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003010bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003011PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003012 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003013 bool isVarArg,
3014 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003015 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003016 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00003017 return false;
3018
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003019 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003020 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003021 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003022
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003023 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00003024 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003025 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
3026 // Functions containing by val parameters are not supported.
3027 for (unsigned i = 0; i != Ins.size(); i++) {
3028 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3029 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003030 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003031
Alp Tokerf907b892013-12-05 05:44:44 +00003032 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003033 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
3034 return true;
3035
3036 // At the moment we can only do local tail calls (in same module, hidden
3037 // or protected) if we are generating PIC.
3038 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
3039 return G->getGlobal()->hasHiddenVisibility()
3040 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003041 }
3042
3043 return false;
3044}
3045
Chris Lattnereb755fc2006-05-17 19:00:46 +00003046/// isCallCompatibleAddress - Return the immediate to use if the specified
3047/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003048static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00003049 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3050 if (!C) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003051
Dan Gohmaneffb8942008-09-12 16:56:44 +00003052 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003053 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00003054 SignExtend32<26>(Addr) != Addr)
Chris Lattnereb755fc2006-05-17 19:00:46 +00003055 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003056
Dan Gohmaneffb8942008-09-12 16:56:44 +00003057 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00003058 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003059}
3060
Dan Gohmand78c4002008-05-13 00:00:25 +00003061namespace {
3062
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003063struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003064 SDValue Arg;
3065 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003066 int FrameIdx;
3067
3068 TailCallArgumentInfo() : FrameIdx(0) {}
3069};
3070
Dan Gohmand78c4002008-05-13 00:00:25 +00003071}
3072
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003073/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3074static void
3075StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003076 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003077 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3078 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003079 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003080 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003081 SDValue Arg = TailCallArgs[i].Arg;
3082 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003083 int FI = TailCallArgs[i].FrameIdx;
3084 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003085 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003086 MachinePointerInfo::getFixedStack(FI),
3087 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003088 }
3089}
3090
3091/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3092/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003093static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003094 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003095 SDValue Chain,
3096 SDValue OldRetAddr,
3097 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003098 int SPDiff,
3099 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003100 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003101 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003102 if (SPDiff) {
3103 // Calculate the new stack slot for the return address.
3104 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003105 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003106 isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003107 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003108 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003109 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003110 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003111 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003112 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003113 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003114
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003115 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3116 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003117 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003118 int NewFPLoc =
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003119 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene1fbe0542009-11-12 20:49:22 +00003120 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003121 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003122 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3123 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003124 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003125 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003126 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003127 }
3128 return Chain;
3129}
3130
3131/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3132/// the position of the argument.
3133static void
3134CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003135 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003136 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003137 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003138 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003139 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003140 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003141 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003142 TailCallArgumentInfo Info;
3143 Info.Arg = Arg;
3144 Info.FrameIdxOp = FIN;
3145 Info.FrameIdx = FI;
3146 TailCallArguments.push_back(Info);
3147}
3148
3149/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3150/// stack slot. Returns the chain as result and the loaded frame pointers in
3151/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003152SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003153 int SPDiff,
3154 SDValue Chain,
3155 SDValue &LROpOut,
3156 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003157 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003158 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003159 if (SPDiff) {
3160 // Load the LR and FP stack slot for later adjusting.
Owen Anderson9f944592009-08-11 20:47:22 +00003161 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003162 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003163 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003164 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003165 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003166
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003167 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3168 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003169 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003170 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003171 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003172 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003173 Chain = SDValue(FPOpOut.getNode(), 1);
3174 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003175 }
3176 return Chain;
3177}
3178
Dale Johannesen85d41a12008-03-04 23:17:14 +00003179/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003180/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003181/// specified by the specific parameter attribute. The copy will be passed as
3182/// a byval function parameter.
3183/// Sometimes what we are copying is the end of a larger object, the part that
3184/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003185static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003186CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003187 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003188 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003189 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003190 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Chris Lattner2510de22010-09-21 05:40:29 +00003191 false, false, MachinePointerInfo(0),
3192 MachinePointerInfo(0));
Dale Johannesen85d41a12008-03-04 23:17:14 +00003193}
Chris Lattner43df5b32007-02-25 05:34:32 +00003194
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003195/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3196/// tail calls.
3197static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003198LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3199 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003200 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003201 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3202 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003203 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003204 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003205 if (!isTailCall) {
3206 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003207 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003208 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003209 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003210 else
Owen Anderson9f944592009-08-11 20:47:22 +00003211 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003212 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003213 DAG.getConstant(ArgOffset, PtrVT));
3214 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003215 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3216 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003217 // Calculate and remember argument location.
3218 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3219 TailCallArguments);
3220}
3221
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003222static
3223void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003224 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003225 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003226 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003227 MachineFunction &MF = DAG.getMachineFunction();
3228
3229 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3230 // might overwrite each other in case of tail call optimization.
3231 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003232 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003233 InFlag = SDValue();
3234 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3235 MemOpChains2, dl);
3236 if (!MemOpChains2.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003237 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003238 &MemOpChains2[0], MemOpChains2.size());
3239
3240 // Store the return address to the appropriate stack slot.
3241 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3242 isPPC64, isDarwinABI, dl);
3243
3244 // Emit callseq_end just before tailcall node.
3245 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003246 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003247 InFlag = Chain.getValue(1);
3248}
3249
3250static
3251unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003252 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003253 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3254 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003255 const PPCSubtarget &PPCSubTarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003256
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003257 bool isPPC64 = PPCSubTarget.isPPC64();
3258 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3259
Owen Anderson53aa7a92009-08-10 22:56:29 +00003260 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003261 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003262 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003263
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003264 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003265
Torok Edwin31e90d22010-08-04 20:47:44 +00003266 bool needIndirectCall = true;
3267 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003268 // If this is an absolute destination address, use the munged value.
3269 Callee = SDValue(Dest, 0);
Torok Edwin31e90d22010-08-04 20:47:44 +00003270 needIndirectCall = false;
3271 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003272
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003273 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3274 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3275 // Use indirect calls for ALL functions calls in JIT mode, since the
3276 // far-call stubs may be outside relocation limits for a BL instruction.
3277 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3278 unsigned OpFlags = 0;
3279 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003280 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003281 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003282 (G->getGlobal()->isDeclaration() ||
3283 G->getGlobal()->isWeakForLinker())) {
3284 // PC-relative references to external symbols should go through $stub,
3285 // unless we're building with the leopard linker or later, which
3286 // automatically synthesizes these stubs.
3287 OpFlags = PPCII::MO_DARWIN_STUB;
3288 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003289
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003290 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3291 // every direct call is) turn it into a TargetGlobalAddress /
3292 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin31e90d22010-08-04 20:47:44 +00003293 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003294 Callee.getValueType(),
3295 0, OpFlags);
Torok Edwin31e90d22010-08-04 20:47:44 +00003296 needIndirectCall = false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003297 }
Torok Edwin31e90d22010-08-04 20:47:44 +00003298 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003299
Torok Edwin31e90d22010-08-04 20:47:44 +00003300 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003301 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003302
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003303 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003304 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003305 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003306 // PC-relative references to external symbols should go through $stub,
3307 // unless we're building with the leopard linker or later, which
3308 // automatically synthesizes these stubs.
3309 OpFlags = PPCII::MO_DARWIN_STUB;
3310 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003311
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003312 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3313 OpFlags);
3314 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003315 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003316
Torok Edwin31e90d22010-08-04 20:47:44 +00003317 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003318 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3319 // to do the call, we can't use PPCISD::CALL.
3320 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003321
3322 if (isSVR4ABI && isPPC64) {
3323 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3324 // entry point, but to the function descriptor (the function entry point
3325 // address is part of the function descriptor though).
3326 // The function descriptor is a three doubleword structure with the
3327 // following fields: function entry point, TOC base address and
3328 // environment pointer.
3329 // Thus for a call through a function pointer, the following actions need
3330 // to be performed:
3331 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003332 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003333 // 2. Load the address of the function entry point from the function
3334 // descriptor.
3335 // 3. Load the TOC of the callee from the function descriptor into r2.
3336 // 4. Load the environment pointer from the function descriptor into
3337 // r11.
3338 // 5. Branch to the function entry point address.
3339 // 6. On return of the callee, the TOC of the caller needs to be
3340 // restored (this is done in FinishCall()).
3341 //
3342 // All those operations are flagged together to ensure that no other
3343 // operations can be scheduled in between. E.g. without flagging the
3344 // operations together, a TOC access in the caller could be scheduled
3345 // between the load of the callee TOC and the branch to the callee, which
3346 // results in the TOC access going through the TOC of the callee instead
3347 // of going through the TOC of the caller, which leads to incorrect code.
3348
3349 // Load the address of the function entry point from the function
3350 // descriptor.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003351 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003352 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, MTCTROps,
3353 InFlag.getNode() ? 3 : 2);
3354 Chain = LoadFuncPtr.getValue(1);
3355 InFlag = LoadFuncPtr.getValue(2);
3356
3357 // Load environment pointer into r11.
3358 // Offset of the environment pointer within the function descriptor.
3359 SDValue PtrOff = DAG.getIntPtrConstant(16);
3360
3361 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3362 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3363 InFlag);
3364 Chain = LoadEnvPtr.getValue(1);
3365 InFlag = LoadEnvPtr.getValue(2);
3366
3367 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3368 InFlag);
3369 Chain = EnvVal.getValue(0);
3370 InFlag = EnvVal.getValue(1);
3371
3372 // Load TOC of the callee into r2. We are using a target-specific load
3373 // with r2 hard coded, because the result of a target-independent load
3374 // would never go directly into r2, since r2 is a reserved register (which
3375 // prevents the register allocator from allocating it), resulting in an
3376 // additional register being allocated and an unnecessary move instruction
3377 // being generated.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003378 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003379 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3380 Callee, InFlag);
3381 Chain = LoadTOCPtr.getValue(0);
3382 InFlag = LoadTOCPtr.getValue(1);
3383
3384 MTCTROps[0] = Chain;
3385 MTCTROps[1] = LoadFuncPtr;
3386 MTCTROps[2] = InFlag;
3387 }
3388
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003389 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
3390 2 + (InFlag.getNode() != 0));
3391 InFlag = Chain.getValue(1);
3392
3393 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00003394 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003395 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003396 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003397 CallOpc = PPCISD::BCTRL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003398 Callee.setNode(0);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003399 // Add use of X11 (holding environment pointer)
3400 if (isSVR4ABI && isPPC64)
3401 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003402 // Add CTR register as callee so a bctr can be emitted later.
3403 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00003404 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003405 }
3406
3407 // If this is a direct call, pass the chain and the callee.
3408 if (Callee.getNode()) {
3409 Ops.push_back(Chain);
3410 Ops.push_back(Callee);
3411 }
3412 // If this is a tail call add stack pointer delta.
3413 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00003414 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003415
3416 // Add argument registers to the end of the list so that they are known live
3417 // into the call.
3418 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3419 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3420 RegsToPass[i].second.getValueType()));
3421
3422 return CallOpc;
3423}
3424
Roman Divacky76293062012-09-18 16:47:58 +00003425static
3426bool isLocalCall(const SDValue &Callee)
3427{
3428 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00003429 return !G->getGlobal()->isDeclaration() &&
3430 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00003431 return false;
3432}
3433
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003434SDValue
3435PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003436 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003437 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003438 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003439 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003440
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003441 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003442 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003443 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003444 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003445
3446 // Copy all of the result registers out of their specified physreg.
3447 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3448 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003449 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00003450
3451 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3452 VA.getLocReg(), VA.getLocVT(), InFlag);
3453 Chain = Val.getValue(1);
3454 InFlag = Val.getValue(2);
3455
3456 switch (VA.getLocInfo()) {
3457 default: llvm_unreachable("Unknown loc info!");
3458 case CCValAssign::Full: break;
3459 case CCValAssign::AExt:
3460 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3461 break;
3462 case CCValAssign::ZExt:
3463 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3464 DAG.getValueType(VA.getValVT()));
3465 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3466 break;
3467 case CCValAssign::SExt:
3468 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3469 DAG.getValueType(VA.getValVT()));
3470 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3471 break;
3472 }
3473
3474 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003475 }
3476
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003477 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003478}
3479
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003480SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00003481PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003482 bool isTailCall, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003483 SelectionDAG &DAG,
3484 SmallVector<std::pair<unsigned, SDValue>, 8>
3485 &RegsToPass,
3486 SDValue InFlag, SDValue Chain,
3487 SDValue &Callee,
3488 int SPDiff, unsigned NumBytes,
3489 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003490 SmallVectorImpl<SDValue> &InVals) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003491 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003492 SmallVector<SDValue, 8> Ops;
3493 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3494 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003495 PPCSubTarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003496
Hal Finkel5ab37802012-08-28 02:10:27 +00003497 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3498 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3499 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3500
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003501 // When performing tail call optimization the callee pops its arguments off
3502 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00003503 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003504 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003505 (CallConv == CallingConv::Fast &&
3506 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003507
Roman Divackyef21be22012-03-06 16:41:49 +00003508 // Add a register mask operand representing the call-preserved registers.
3509 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3510 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3511 assert(Mask && "Missing call preserved mask for calling convention");
3512 Ops.push_back(DAG.getRegisterMask(Mask));
3513
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003514 if (InFlag.getNode())
3515 Ops.push_back(InFlag);
3516
3517 // Emit tail call.
3518 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003519 assert(((Callee.getOpcode() == ISD::Register &&
3520 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3521 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3522 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3523 isa<ConstantSDNode>(Callee)) &&
3524 "Expecting an global address, external symbol, absolute value or register");
3525
Owen Anderson9f944592009-08-11 20:47:22 +00003526 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003527 }
3528
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003529 // Add a NOP immediately after the branch instruction when using the 64-bit
3530 // SVR4 ABI. At link time, if caller and callee are in a different module and
3531 // thus have a different TOC, the call will be replaced with a call to a stub
3532 // function which saves the current TOC, loads the TOC of the callee and
3533 // branches to the callee. The NOP will be replaced with a load instruction
3534 // which restores the TOC of the caller from the TOC save slot of the current
3535 // stack frame. If caller and callee belong to the same module (and have the
3536 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00003537
3538 bool needsTOCRestore = false;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003539 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003540 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003541 // This is a call through a function pointer.
3542 // Restore the caller TOC from the save area into R2.
3543 // See PrepareCall() for more information about calls through function
3544 // pointers in the 64-bit SVR4 ABI.
3545 // We are using a target-specific load with r2 hard coded, because the
3546 // result of a target-independent load would never go directly into r2,
3547 // since r2 is a reserved register (which prevents the register allocator
3548 // from allocating it), resulting in an additional register being
3549 // allocated and an unnecessary move instruction being generated.
Hal Finkel51861b42012-03-31 14:45:15 +00003550 needsTOCRestore = true;
Bill Schmidtcea15962013-09-26 17:09:28 +00003551 } else if ((CallOpc == PPCISD::CALL) &&
3552 (!isLocalCall(Callee) ||
3553 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Roman Divacky76293062012-09-18 16:47:58 +00003554 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003555 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller79fef932009-12-18 13:00:15 +00003556 }
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003557 }
3558
Hal Finkel51861b42012-03-31 14:45:15 +00003559 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
3560 InFlag = Chain.getValue(1);
3561
3562 if (needsTOCRestore) {
3563 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3564 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3565 InFlag = Chain.getValue(1);
3566 }
3567
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003568 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3569 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003570 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003571 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003572 InFlag = Chain.getValue(1);
3573
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003574 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3575 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003576}
3577
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003578SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00003579PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003580 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00003581 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00003582 SDLoc &dl = CLI.DL;
3583 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3584 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3585 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00003586 SDValue Chain = CLI.Chain;
3587 SDValue Callee = CLI.Callee;
3588 bool &isTailCall = CLI.IsTailCall;
3589 CallingConv::ID CallConv = CLI.CallConv;
3590 bool isVarArg = CLI.IsVarArg;
3591
Evan Cheng67a69dd2010-01-27 00:07:07 +00003592 if (isTailCall)
3593 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3594 Ins, DAG);
3595
Bill Schmidt57d6de52012-10-23 15:51:16 +00003596 if (PPCSubTarget.isSVR4ABI()) {
3597 if (PPCSubTarget.isPPC64())
3598 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3599 isTailCall, Outs, OutVals, Ins,
3600 dl, DAG, InVals);
3601 else
3602 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3603 isTailCall, Outs, OutVals, Ins,
3604 dl, DAG, InVals);
3605 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003606
Bill Schmidt57d6de52012-10-23 15:51:16 +00003607 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3608 isTailCall, Outs, OutVals, Ins,
3609 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003610}
3611
3612SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003613PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3614 CallingConv::ID CallConv, bool isVarArg,
3615 bool isTailCall,
3616 const SmallVectorImpl<ISD::OutputArg> &Outs,
3617 const SmallVectorImpl<SDValue> &OutVals,
3618 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003619 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003620 SmallVectorImpl<SDValue> &InVals) const {
3621 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003622 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003623
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003624 assert((CallConv == CallingConv::C ||
3625 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003626
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003627 unsigned PtrByteSize = 4;
3628
3629 MachineFunction &MF = DAG.getMachineFunction();
3630
3631 // Mark this function as potentially containing a function that contains a
3632 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3633 // and restoring the callers stack pointer in this functions epilog. This is
3634 // done because by tail calling the called function might overwrite the value
3635 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003636 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3637 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003638 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00003639
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003640 // Count how many bytes are to be pushed on the stack, including the linkage
3641 // area, parameter list area and the part of the local variable space which
3642 // contains copies of aggregates which are passed by value.
3643
3644 // Assign locations to all of the outgoing arguments.
3645 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003646 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003647 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003648
3649 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003650 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003651
3652 if (isVarArg) {
3653 // Handle fixed and variable vector arguments differently.
3654 // Fixed vector arguments go into registers as long as registers are
3655 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003656 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00003657
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003658 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003659 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003660 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003661 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00003662
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003663 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00003664 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3665 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003666 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00003667 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3668 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003669 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003670
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003671 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003672#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00003673 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00003674 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003675#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00003676 llvm_unreachable(0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003677 }
3678 }
3679 } else {
3680 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00003681 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003682 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003683
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003684 // Assign locations to all of the outgoing aggregate by value arguments.
3685 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003686 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003687 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003688
3689 // Reserve stack space for the allocations in CCInfo.
3690 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3691
Bill Schmidtef17c142013-02-06 17:33:58 +00003692 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003693
3694 // Size of the linkage area, parameter list area and the part of the local
3695 // space variable where copies of aggregates which are passed by value are
3696 // stored.
3697 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003698
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003699 // Calculate by how many bytes the stack has to be adjusted in case of tail
3700 // call optimization.
3701 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3702
3703 // Adjust the stack pointer for the new arguments...
3704 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003705 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3706 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003707 SDValue CallSeqStart = Chain;
3708
3709 // Load the return address and frame pointer so it can be moved somewhere else
3710 // later.
3711 SDValue LROp, FPOp;
3712 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3713 dl);
3714
3715 // Set up a copy of the stack pointer for use loading and storing any
3716 // arguments that may not fit in the registers available for argument
3717 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00003718 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00003719
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003720 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3721 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3722 SmallVector<SDValue, 8> MemOpChains;
3723
Roman Divacky71038e72011-08-30 17:04:16 +00003724 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003725 // Walk the register/memloc assignments, inserting copies/loads.
3726 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3727 i != e;
3728 ++i) {
3729 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003730 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003731 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00003732
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003733 if (Flags.isByVal()) {
3734 // Argument is an aggregate which is passed by value, thus we need to
3735 // create a copy of it in the local variable space of the current stack
3736 // frame (which is the stack frame of the caller) and pass the address of
3737 // this copy to the callee.
3738 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3739 CCValAssign &ByValVA = ByValArgLocs[j++];
3740 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00003741
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003742 // Memory reserved in the local variable space of the callers stack frame.
3743 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003744
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003745 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3746 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00003747
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003748 // Create a copy of the argument in the local area of the current
3749 // stack frame.
3750 SDValue MemcpyCall =
3751 CreateCopyOfByValArgument(Arg, PtrOff,
3752 CallSeqStart.getNode()->getOperand(0),
3753 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00003754
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003755 // This must go outside the CALLSEQ_START..END.
3756 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003757 CallSeqStart.getNode()->getOperand(1),
3758 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003759 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3760 NewCallSeqStart.getNode());
3761 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00003762
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003763 // Pass the address of the aggregate copy on the stack either in a
3764 // physical register or in the parameter list area of the current stack
3765 // frame to the callee.
3766 Arg = PtrOff;
3767 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003768
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003769 if (VA.isRegLoc()) {
Roman Divacky71038e72011-08-30 17:04:16 +00003770 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003771 // Put argument in a physical register.
3772 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3773 } else {
3774 // Put argument in the parameter list area of the current stack frame.
3775 assert(VA.isMemLoc());
3776 unsigned LocMemOffset = VA.getLocMemOffset();
3777
3778 if (!isTailCall) {
3779 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3780 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3781
3782 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00003783 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00003784 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003785 } else {
3786 // Calculate and remember argument location.
3787 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3788 TailCallArguments);
3789 }
3790 }
3791 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003792
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003793 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003794 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003795 &MemOpChains[0], MemOpChains.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00003796
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003797 // Build a sequence of copy-to-reg nodes chained together with token chain
3798 // and flag operands which copy the outgoing args into the appropriate regs.
3799 SDValue InFlag;
3800 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3801 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3802 RegsToPass[i].second, InFlag);
3803 InFlag = Chain.getValue(1);
3804 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003805
Hal Finkel5ab37802012-08-28 02:10:27 +00003806 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3807 // registers.
3808 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003809 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3810 SDValue Ops[] = { Chain, InFlag };
3811
Hal Finkel5ab37802012-08-28 02:10:27 +00003812 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003813 dl, VTs, Ops, InFlag.getNode() ? 2 : 1);
3814
Hal Finkel5ab37802012-08-28 02:10:27 +00003815 InFlag = Chain.getValue(1);
3816 }
3817
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003818 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003819 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3820 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003821
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003822 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3823 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3824 Ins, InVals);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003825}
3826
Bill Schmidt57d6de52012-10-23 15:51:16 +00003827// Copy an argument into memory, being careful to do this outside the
3828// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003829SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00003830PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3831 SDValue CallSeqStart,
3832 ISD::ArgFlagsTy Flags,
3833 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003834 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003835 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3836 CallSeqStart.getNode()->getOperand(0),
3837 Flags, DAG, dl);
3838 // The MEMCPY must go outside the CALLSEQ_START..END.
3839 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003840 CallSeqStart.getNode()->getOperand(1),
3841 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00003842 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3843 NewCallSeqStart.getNode());
3844 return NewCallSeqStart;
3845}
3846
3847SDValue
3848PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003849 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003850 bool isTailCall,
3851 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003852 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003853 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003854 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003855 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003856
Bill Schmidt57d6de52012-10-23 15:51:16 +00003857 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003858
Bill Schmidt57d6de52012-10-23 15:51:16 +00003859 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3860 unsigned PtrByteSize = 8;
3861
3862 MachineFunction &MF = DAG.getMachineFunction();
3863
3864 // Mark this function as potentially containing a function that contains a
3865 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3866 // and restoring the callers stack pointer in this functions epilog. This is
3867 // done because by tail calling the called function might overwrite the value
3868 // in this function's (MF) stack pointer stack slot 0(SP).
3869 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3870 CallConv == CallingConv::Fast)
3871 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3872
3873 unsigned nAltivecParamsAtEnd = 0;
3874
3875 // Count how many bytes are to be pushed on the stack, including the linkage
3876 // area, and parameter passing area. We start with at least 48 bytes, which
3877 // is reserved space for [SP][CR][LR][3 x unused].
3878 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
3879 // of this call.
3880 unsigned NumBytes =
3881 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
3882 Outs, OutVals, nAltivecParamsAtEnd);
3883
3884 // Calculate by how many bytes the stack has to be adjusted in case of tail
3885 // call optimization.
3886 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3887
3888 // To protect arguments on the stack from being clobbered in a tail call,
3889 // force all the loads to happen before doing any other lowering.
3890 if (isTailCall)
3891 Chain = DAG.getStackArgumentTokenFactor(Chain);
3892
3893 // Adjust the stack pointer for the new arguments...
3894 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003895 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3896 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003897 SDValue CallSeqStart = Chain;
3898
3899 // Load the return address and frame pointer so it can be move somewhere else
3900 // later.
3901 SDValue LROp, FPOp;
3902 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
3903 dl);
3904
3905 // Set up a copy of the stack pointer for use loading and storing any
3906 // arguments that may not fit in the registers available for argument
3907 // passing.
3908 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
3909
3910 // Figure out which arguments are going to go in registers, and which in
3911 // memory. Also, if this is a vararg function, floating point operations
3912 // must be stored to our stack, and loaded into integer regs as well, if
3913 // any integer regs are available for argument passing.
3914 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
3915 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
3916
3917 static const uint16_t GPR[] = {
3918 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3919 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3920 };
3921 static const uint16_t *FPR = GetFPR();
3922
3923 static const uint16_t VR[] = {
3924 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3925 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3926 };
3927 const unsigned NumGPRs = array_lengthof(GPR);
3928 const unsigned NumFPRs = 13;
3929 const unsigned NumVRs = array_lengthof(VR);
3930
3931 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3932 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3933
3934 SmallVector<SDValue, 8> MemOpChains;
3935 for (unsigned i = 0; i != NumOps; ++i) {
3936 SDValue Arg = OutVals[i];
3937 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3938
3939 // PtrOff will be used to store the current argument to the stack if a
3940 // register cannot be found for it.
3941 SDValue PtrOff;
3942
3943 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
3944
3945 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
3946
3947 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00003948 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003949 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
3950 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3951 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
3952 }
3953
3954 // FIXME memcpy is used way more than necessary. Correctness first.
3955 // Note: "by value" is code for passing a structure by value, not
3956 // basic types.
3957 if (Flags.isByVal()) {
3958 // Note: Size includes alignment padding, so
3959 // struct x { short a; char b; }
3960 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
3961 // These are the proper values we need for right-justifying the
3962 // aggregate in a parameter register.
3963 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00003964
3965 // An empty aggregate parameter takes up no storage and no
3966 // registers.
3967 if (Size == 0)
3968 continue;
3969
Hal Finkel262a2242013-09-12 23:20:06 +00003970 unsigned BVAlign = Flags.getByValAlign();
3971 if (BVAlign > 8) {
3972 if (BVAlign % PtrByteSize != 0)
3973 llvm_unreachable(
3974 "ByVal alignment is not a multiple of the pointer size");
3975
3976 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
3977 }
3978
Bill Schmidt57d6de52012-10-23 15:51:16 +00003979 // All aggregates smaller than 8 bytes must be passed right-justified.
3980 if (Size==1 || Size==2 || Size==4) {
3981 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
3982 if (GPR_idx != NumGPRs) {
3983 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
3984 MachinePointerInfo(), VT,
3985 false, false, 0);
3986 MemOpChains.push_back(Load.getValue(1));
3987 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3988
3989 ArgOffset += PtrByteSize;
3990 continue;
3991 }
3992 }
3993
3994 if (GPR_idx == NumGPRs && Size < 8) {
3995 SDValue Const = DAG.getConstant(PtrByteSize - Size,
3996 PtrOff.getValueType());
3997 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3998 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3999 CallSeqStart,
4000 Flags, DAG, dl);
4001 ArgOffset += PtrByteSize;
4002 continue;
4003 }
4004 // Copy entire object into memory. There are cases where gcc-generated
4005 // code assumes it is there, even if it could be put entirely into
4006 // registers. (This is not what the doc says.)
4007
4008 // FIXME: The above statement is likely due to a misunderstanding of the
4009 // documents. All arguments must be copied into the parameter area BY
4010 // THE CALLEE in the event that the callee takes the address of any
4011 // formal argument. That has not yet been implemented. However, it is
4012 // reasonable to use the stack area as a staging area for the register
4013 // load.
4014
4015 // Skip this for small aggregates, as we will use the same slot for a
4016 // right-justified copy, below.
4017 if (Size >= 8)
4018 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4019 CallSeqStart,
4020 Flags, DAG, dl);
4021
4022 // When a register is available, pass a small aggregate right-justified.
4023 if (Size < 8 && GPR_idx != NumGPRs) {
4024 // The easiest way to get this right-justified in a register
4025 // is to copy the structure into the rightmost portion of a
4026 // local variable slot, then load the whole slot into the
4027 // register.
4028 // FIXME: The memcpy seems to produce pretty awful code for
4029 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00004030 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00004031 // parameter save area instead of a new local variable.
4032 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
4033 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4034 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4035 CallSeqStart,
4036 Flags, DAG, dl);
4037
4038 // Load the slot into the register.
4039 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
4040 MachinePointerInfo(),
4041 false, false, false, 0);
4042 MemOpChains.push_back(Load.getValue(1));
4043 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4044
4045 // Done with this argument.
4046 ArgOffset += PtrByteSize;
4047 continue;
4048 }
4049
4050 // For aggregates larger than PtrByteSize, copy the pieces of the
4051 // object that fit into registers from the parameter save area.
4052 for (unsigned j=0; j<Size; j+=PtrByteSize) {
4053 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
4054 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
4055 if (GPR_idx != NumGPRs) {
4056 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4057 MachinePointerInfo(),
4058 false, false, false, 0);
4059 MemOpChains.push_back(Load.getValue(1));
4060 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4061 ArgOffset += PtrByteSize;
4062 } else {
4063 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4064 break;
4065 }
4066 }
4067 continue;
4068 }
4069
Craig Topper56710102013-08-15 02:33:50 +00004070 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004071 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00004072 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004073 case MVT::i32:
4074 case MVT::i64:
4075 if (GPR_idx != NumGPRs) {
4076 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
4077 } else {
4078 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4079 true, isTailCall, false, MemOpChains,
4080 TailCallArguments, dl);
4081 }
4082 ArgOffset += PtrByteSize;
4083 break;
4084 case MVT::f32:
4085 case MVT::f64:
4086 if (FPR_idx != NumFPRs) {
4087 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4088
4089 if (isVarArg) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004090 // A single float or an aggregate containing only a single float
4091 // must be passed right-justified in the stack doubleword, and
4092 // in the GPR, if one is available.
4093 SDValue StoreOff;
Craig Topper56710102013-08-15 02:33:50 +00004094 if (Arg.getSimpleValueType().SimpleTy == MVT::f32) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004095 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4096 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4097 } else
4098 StoreOff = PtrOff;
4099
4100 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004101 MachinePointerInfo(), false, false, 0);
4102 MemOpChains.push_back(Store);
4103
4104 // Float varargs are always shadowed in available integer registers
4105 if (GPR_idx != NumGPRs) {
4106 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4107 MachinePointerInfo(), false, false,
4108 false, 0);
4109 MemOpChains.push_back(Load.getValue(1));
4110 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4111 }
4112 } else if (GPR_idx != NumGPRs)
4113 // If we have any FPRs remaining, we may also have GPRs remaining.
4114 ++GPR_idx;
4115 } else {
4116 // Single-precision floating-point values are mapped to the
4117 // second (rightmost) word of the stack doubleword.
4118 if (Arg.getValueType() == MVT::f32) {
4119 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4120 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4121 }
4122
4123 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4124 true, isTailCall, false, MemOpChains,
4125 TailCallArguments, dl);
4126 }
4127 ArgOffset += 8;
4128 break;
4129 case MVT::v4f32:
4130 case MVT::v4i32:
4131 case MVT::v8i16:
4132 case MVT::v16i8:
4133 if (isVarArg) {
4134 // These go aligned on the stack, or in the corresponding R registers
4135 // when within range. The Darwin PPC ABI doc claims they also go in
4136 // V registers; in fact gcc does this only for arguments that are
4137 // prototyped, not for those that match the ... We do it for all
4138 // arguments, seems to work.
4139 while (ArgOffset % 16 !=0) {
4140 ArgOffset += PtrByteSize;
4141 if (GPR_idx != NumGPRs)
4142 GPR_idx++;
4143 }
4144 // We could elide this store in the case where the object fits
4145 // entirely in R registers. Maybe later.
4146 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
4147 DAG.getConstant(ArgOffset, PtrVT));
4148 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4149 MachinePointerInfo(), false, false, 0);
4150 MemOpChains.push_back(Store);
4151 if (VR_idx != NumVRs) {
4152 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4153 MachinePointerInfo(),
4154 false, false, false, 0);
4155 MemOpChains.push_back(Load.getValue(1));
4156 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4157 }
4158 ArgOffset += 16;
4159 for (unsigned i=0; i<16; i+=PtrByteSize) {
4160 if (GPR_idx == NumGPRs)
4161 break;
4162 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4163 DAG.getConstant(i, PtrVT));
4164 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4165 false, false, false, 0);
4166 MemOpChains.push_back(Load.getValue(1));
4167 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4168 }
4169 break;
4170 }
4171
4172 // Non-varargs Altivec params generally go in registers, but have
4173 // stack space allocated at the end.
4174 if (VR_idx != NumVRs) {
4175 // Doesn't have GPR space allocated.
4176 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4177 } else {
4178 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4179 true, isTailCall, true, MemOpChains,
4180 TailCallArguments, dl);
4181 ArgOffset += 16;
4182 }
4183 break;
4184 }
4185 }
4186
4187 if (!MemOpChains.empty())
4188 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4189 &MemOpChains[0], MemOpChains.size());
4190
4191 // Check if this is an indirect call (MTCTR/BCTRL).
4192 // See PrepareCall() for more information about calls through function
4193 // pointers in the 64-bit SVR4 ABI.
4194 if (!isTailCall &&
4195 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4196 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4197 !isBLACompatibleAddress(Callee, DAG)) {
4198 // Load r2 into a virtual register and store it to the TOC save area.
4199 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4200 // TOC save area offset.
4201 SDValue PtrOff = DAG.getIntPtrConstant(40);
4202 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4203 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4204 false, false, 0);
4205 // R12 must contain the address of an indirect callee. This does not
4206 // mean the MTCTR instruction must use R12; it's easier to model this
4207 // as an extra parameter, so do that.
4208 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4209 }
4210
4211 // Build a sequence of copy-to-reg nodes chained together with token chain
4212 // and flag operands which copy the outgoing args into the appropriate regs.
4213 SDValue InFlag;
4214 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4215 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4216 RegsToPass[i].second, InFlag);
4217 InFlag = Chain.getValue(1);
4218 }
4219
4220 if (isTailCall)
4221 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4222 FPOp, true, TailCallArguments);
4223
4224 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4225 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4226 Ins, InVals);
4227}
4228
4229SDValue
4230PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4231 CallingConv::ID CallConv, bool isVarArg,
4232 bool isTailCall,
4233 const SmallVectorImpl<ISD::OutputArg> &Outs,
4234 const SmallVectorImpl<SDValue> &OutVals,
4235 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004236 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004237 SmallVectorImpl<SDValue> &InVals) const {
4238
4239 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004240
Owen Anderson53aa7a92009-08-10 22:56:29 +00004241 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00004242 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004243 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004244
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004245 MachineFunction &MF = DAG.getMachineFunction();
4246
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004247 // Mark this function as potentially containing a function that contains a
4248 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4249 // and restoring the callers stack pointer in this functions epilog. This is
4250 // done because by tail calling the called function might overwrite the value
4251 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004252 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4253 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004254 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4255
4256 unsigned nAltivecParamsAtEnd = 0;
4257
Chris Lattneraa40ec12006-05-16 22:56:08 +00004258 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00004259 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00004260 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004261 unsigned NumBytes =
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004262 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004263 Outs, OutVals,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004264 nAltivecParamsAtEnd);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004265
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004266 // Calculate by how many bytes the stack has to be adjusted in case of tail
4267 // call optimization.
4268 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004269
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004270 // To protect arguments on the stack from being clobbered in a tail call,
4271 // force all the loads to happen before doing any other lowering.
4272 if (isTailCall)
4273 Chain = DAG.getStackArgumentTokenFactor(Chain);
4274
Chris Lattnerb7552a82006-05-17 00:15:40 +00004275 // Adjust the stack pointer for the new arguments...
4276 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004277 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4278 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004279 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004280
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004281 // Load the return address and frame pointer so it can be move somewhere else
4282 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004283 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004284 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4285 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004286
Chris Lattnerb7552a82006-05-17 00:15:40 +00004287 // Set up a copy of the stack pointer for use loading and storing any
4288 // arguments that may not fit in the registers available for argument
4289 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004290 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004291 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004292 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004293 else
Owen Anderson9f944592009-08-11 20:47:22 +00004294 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004295
Chris Lattnerb7552a82006-05-17 00:15:40 +00004296 // Figure out which arguments are going to go in registers, and which in
4297 // memory. Also, if this is a vararg function, floating point operations
4298 // must be stored to our stack, and loaded into integer regs as well, if
4299 // any integer regs are available for argument passing.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004300 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004301 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004302
Craig Topperca658c22012-03-11 07:16:55 +00004303 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004304 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4305 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4306 };
Craig Topperca658c22012-03-11 07:16:55 +00004307 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00004308 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4309 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4310 };
Craig Topperca658c22012-03-11 07:16:55 +00004311 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004312
Craig Topperca658c22012-03-11 07:16:55 +00004313 static const uint16_t VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004314 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4315 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4316 };
Owen Andersone2f23a32007-09-07 04:06:50 +00004317 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004318 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00004319 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004320
Craig Topperca658c22012-03-11 07:16:55 +00004321 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004322
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004323 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004324 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4325
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004326 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00004327 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004328 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004329 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004330
Chris Lattnerb7552a82006-05-17 00:15:40 +00004331 // PtrOff will be used to store the current argument to the stack if a
4332 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004333 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004334
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004335 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004336
Dale Johannesen679073b2009-02-04 02:34:38 +00004337 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004338
4339 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00004340 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00004341 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4342 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00004343 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004344 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00004345
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004346 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004347 // Note: "by value" is code for passing a structure by value, not
4348 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00004349 if (Flags.isByVal()) {
4350 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004351 // Very small objects are passed right-justified. Everything else is
4352 // passed left-justified.
4353 if (Size==1 || Size==2) {
4354 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004355 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00004356 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00004357 MachinePointerInfo(), VT,
4358 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004359 MemOpChains.push_back(Load.getValue(1));
4360 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004361
4362 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004363 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00004364 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4365 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004366 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004367 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4368 CallSeqStart,
4369 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004370 ArgOffset += PtrByteSize;
4371 }
4372 continue;
4373 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004374 // Copy entire object into memory. There are cases where gcc-generated
4375 // code assumes it is there, even if it could be put entirely into
4376 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004377 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4378 CallSeqStart,
4379 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004380
4381 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4382 // copy the pieces of the object that fit into registers from the
4383 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00004384 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004385 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004386 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00004387 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004388 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4389 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004390 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00004391 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00004392 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004393 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004394 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004395 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004396 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004397 }
4398 }
4399 continue;
4400 }
4401
Craig Topper56710102013-08-15 02:33:50 +00004402 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004403 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00004404 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00004405 case MVT::i32:
4406 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004407 if (GPR_idx != NumGPRs) {
4408 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004409 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004410 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4411 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004412 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00004413 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004414 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004415 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004416 case MVT::f32:
4417 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004418 if (FPR_idx != NumFPRs) {
4419 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4420
Chris Lattnerb7552a82006-05-17 00:15:40 +00004421 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00004422 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4423 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004424 MemOpChains.push_back(Store);
4425
Chris Lattnerb7552a82006-05-17 00:15:40 +00004426 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004427 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004428 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004429 MachinePointerInfo(), false, false,
4430 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004431 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004432 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004433 }
Owen Anderson9f944592009-08-11 20:47:22 +00004434 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004435 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004436 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00004437 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4438 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004439 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004440 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004441 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00004442 }
4443 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00004444 // If we have any FPRs remaining, we may also have GPRs remaining.
4445 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4446 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004447 if (GPR_idx != NumGPRs)
4448 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00004449 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004450 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4451 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004452 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004453 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004454 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4455 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004456 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004457 if (isPPC64)
4458 ArgOffset += 8;
4459 else
Owen Anderson9f944592009-08-11 20:47:22 +00004460 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004461 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004462 case MVT::v4f32:
4463 case MVT::v4i32:
4464 case MVT::v8i16:
4465 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00004466 if (isVarArg) {
4467 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00004468 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00004469 // V registers; in fact gcc does this only for arguments that are
4470 // prototyped, not for those that match the ... We do it for all
4471 // arguments, seems to work.
4472 while (ArgOffset % 16 !=0) {
4473 ArgOffset += PtrByteSize;
4474 if (GPR_idx != NumGPRs)
4475 GPR_idx++;
4476 }
4477 // We could elide this store in the case where the object fits
4478 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004479 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004480 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00004481 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4482 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004483 MemOpChains.push_back(Store);
4484 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004485 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00004486 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004487 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004488 MemOpChains.push_back(Load.getValue(1));
4489 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4490 }
4491 ArgOffset += 16;
4492 for (unsigned i=0; i<16; i+=PtrByteSize) {
4493 if (GPR_idx == NumGPRs)
4494 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00004495 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004496 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00004497 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004498 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004499 MemOpChains.push_back(Load.getValue(1));
4500 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4501 }
4502 break;
4503 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004504
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004505 // Non-varargs Altivec params generally go in registers, but have
4506 // stack space allocated at the end.
4507 if (VR_idx != NumVRs) {
4508 // Doesn't have GPR space allocated.
4509 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4510 } else if (nAltivecParamsAtEnd==0) {
4511 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004512 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4513 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004514 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004515 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00004516 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00004517 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004518 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00004519 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004520 // If all Altivec parameters fit in registers, as they usually do,
4521 // they get stack space following the non-Altivec parameters. We
4522 // don't track this here because nobody below needs it.
4523 // If there are more Altivec parameters than fit in registers emit
4524 // the stores here.
4525 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4526 unsigned j = 0;
4527 // Offset is aligned; skip 1st 12 params which go in V registers.
4528 ArgOffset = ((ArgOffset+15)/16)*16;
4529 ArgOffset += 12*16;
4530 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004531 SDValue Arg = OutVals[i];
4532 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00004533 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4534 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004535 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004536 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004537 // We are emitting Altivec params in order.
4538 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4539 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004540 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004541 ArgOffset += 16;
4542 }
4543 }
4544 }
4545 }
4546
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004547 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00004548 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnered728e82006-08-11 17:38:39 +00004549 &MemOpChains[0], MemOpChains.size());
Scott Michelcf0da6c2009-02-17 22:15:04 +00004550
Dale Johannesen90eab672010-03-09 20:15:42 +00004551 // On Darwin, R12 must contain the address of an indirect callee. This does
4552 // not mean the MTCTR instruction must use R12; it's easier to model this as
4553 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004554 if (!isTailCall &&
Dale Johannesen90eab672010-03-09 20:15:42 +00004555 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4556 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4557 !isBLACompatibleAddress(Callee, DAG))
4558 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4559 PPC::R12), Callee));
4560
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004561 // Build a sequence of copy-to-reg nodes chained together with token chain
4562 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004563 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004564 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00004565 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00004566 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004567 InFlag = Chain.getValue(1);
4568 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004569
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004570 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004571 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4572 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004573
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004574 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4575 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4576 Ins, InVals);
Chris Lattneraa40ec12006-05-16 22:56:08 +00004577}
4578
Hal Finkel450128a2011-10-14 19:51:36 +00004579bool
4580PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4581 MachineFunction &MF, bool isVarArg,
4582 const SmallVectorImpl<ISD::OutputArg> &Outs,
4583 LLVMContext &Context) const {
4584 SmallVector<CCValAssign, 16> RVLocs;
4585 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4586 RVLocs, Context);
4587 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4588}
4589
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004590SDValue
4591PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004592 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004593 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004594 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004595 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004596
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004597 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004598 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00004599 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004600 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004601
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004602 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004603 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004604
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004605 // Copy the result values into the output registers.
4606 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4607 CCValAssign &VA = RVLocs[i];
4608 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004609
4610 SDValue Arg = OutVals[i];
4611
4612 switch (VA.getLocInfo()) {
4613 default: llvm_unreachable("Unknown loc info!");
4614 case CCValAssign::Full: break;
4615 case CCValAssign::AExt:
4616 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4617 break;
4618 case CCValAssign::ZExt:
4619 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4620 break;
4621 case CCValAssign::SExt:
4622 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4623 break;
4624 }
4625
4626 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004627 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004628 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004629 }
4630
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004631 RetOps[0] = Chain; // Update chain.
4632
4633 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00004634 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004635 RetOps.push_back(Flag);
4636
4637 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other,
4638 &RetOps[0], RetOps.size());
Chris Lattner4211ca92006-04-14 06:01:58 +00004639}
4640
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004641SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004642 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00004643 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004644 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004645
Jim Laskeye4f4d042006-12-04 22:04:42 +00004646 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004647 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00004648
4649 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00004650 bool isPPC64 = Subtarget.isPPC64();
4651 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004652 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004653
4654 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004655 SDValue Chain = Op.getOperand(0);
4656 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004657
Jim Laskeye4f4d042006-12-04 22:04:42 +00004658 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00004659 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4660 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004661 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004662
Jim Laskeye4f4d042006-12-04 22:04:42 +00004663 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00004664 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004665
Jim Laskeye4f4d042006-12-04 22:04:42 +00004666 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00004667 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004668 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004669}
4670
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004671
4672
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004673SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004674PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004675 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004676 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004677 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004678 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004679
4680 // Get current frame pointer save index. The users of this index will be
4681 // primarily DYNALLOC instructions.
4682 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4683 int RASI = FI->getReturnAddrSaveIndex();
4684
4685 // If the frame pointer save index hasn't been defined yet.
4686 if (!RASI) {
4687 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004688 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004689 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004690 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004691 // Save the result.
4692 FI->setReturnAddrSaveIndex(RASI);
4693 }
4694 return DAG.getFrameIndex(RASI, PtrVT);
4695}
4696
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004697SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004698PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4699 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004700 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004701 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004702 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004703
4704 // Get current frame pointer save index. The users of this index will be
4705 // primarily DYNALLOC instructions.
4706 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4707 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004708
Jim Laskey48850c12006-11-16 22:43:37 +00004709 // If the frame pointer save index hasn't been defined yet.
4710 if (!FPSI) {
4711 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004712 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004713 isDarwinABI);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004714
Jim Laskey48850c12006-11-16 22:43:37 +00004715 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004716 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00004717 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004718 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00004719 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004720 return DAG.getFrameIndex(FPSI, PtrVT);
4721}
Jim Laskey48850c12006-11-16 22:43:37 +00004722
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004723SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004724 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004725 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004726 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004727 SDValue Chain = Op.getOperand(0);
4728 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004729 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004730
Jim Laskey48850c12006-11-16 22:43:37 +00004731 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004732 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004733 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004734 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00004735 DAG.getConstant(0, PtrVT), Size);
4736 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004737 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00004738 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004739 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00004740 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004741 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey48850c12006-11-16 22:43:37 +00004742}
4743
Hal Finkel756810f2013-03-21 21:37:52 +00004744SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
4745 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004746 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004747 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
4748 DAG.getVTList(MVT::i32, MVT::Other),
4749 Op.getOperand(0), Op.getOperand(1));
4750}
4751
4752SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
4753 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004754 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004755 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
4756 Op.getOperand(0), Op.getOperand(1));
4757}
4758
Hal Finkel940ab932014-02-28 00:27:01 +00004759SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
4760 assert(Op.getValueType() == MVT::i1 &&
4761 "Custom lowering only for i1 loads");
4762
4763 // First, load 8 bits into 32 bits, then truncate to 1 bit.
4764
4765 SDLoc dl(Op);
4766 LoadSDNode *LD = cast<LoadSDNode>(Op);
4767
4768 SDValue Chain = LD->getChain();
4769 SDValue BasePtr = LD->getBasePtr();
4770 MachineMemOperand *MMO = LD->getMemOperand();
4771
4772 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(), Chain,
4773 BasePtr, MVT::i8, MMO);
4774 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
4775
4776 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
4777 return DAG.getMergeValues(Ops, 2, dl);
4778}
4779
4780SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
4781 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
4782 "Custom lowering only for i1 stores");
4783
4784 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
4785
4786 SDLoc dl(Op);
4787 StoreSDNode *ST = cast<StoreSDNode>(Op);
4788
4789 SDValue Chain = ST->getChain();
4790 SDValue BasePtr = ST->getBasePtr();
4791 SDValue Value = ST->getValue();
4792 MachineMemOperand *MMO = ST->getMemOperand();
4793
4794 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(), Value);
4795 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
4796}
4797
4798// FIXME: Remove this once the ANDI glue bug is fixed:
4799SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
4800 assert(Op.getValueType() == MVT::i1 &&
4801 "Custom lowering only for i1 results");
4802
4803 SDLoc DL(Op);
4804 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
4805 Op.getOperand(0));
4806}
4807
Chris Lattner4211ca92006-04-14 06:01:58 +00004808/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4809/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004810SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00004811 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00004812 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4813 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00004814 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004815
Hal Finkel81f87992013-04-07 22:11:09 +00004816 // We might be able to do better than this under some circumstances, but in
4817 // general, fsel-based lowering of select is a finite-math-only optimization.
4818 // For more information, see section F.3 of the 2.06 ISA specification.
4819 if (!DAG.getTarget().Options.NoInfsFPMath ||
4820 !DAG.getTarget().Options.NoNaNsFPMath)
4821 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004822
Hal Finkel81f87992013-04-07 22:11:09 +00004823 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004824
Owen Anderson53aa7a92009-08-10 22:56:29 +00004825 EVT ResVT = Op.getValueType();
4826 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004827 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4828 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004829 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004830
Chris Lattner4211ca92006-04-14 06:01:58 +00004831 // If the RHS of the comparison is a 0.0, we don't need to do the
4832 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00004833 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00004834 if (isFloatingPointZero(RHS))
4835 switch (CC) {
4836 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004837 case ISD::SETNE:
4838 std::swap(TV, FV);
4839 case ISD::SETEQ:
4840 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4841 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
4842 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
4843 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4844 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4845 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4846 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004847 case ISD::SETULT:
4848 case ISD::SETLT:
4849 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004850 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004851 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00004852 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4853 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004854 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004855 case ISD::SETUGT:
4856 case ISD::SETGT:
4857 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004858 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004859 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00004860 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4861 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004862 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00004863 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004864 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004865
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004866 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00004867 switch (CC) {
4868 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004869 case ISD::SETNE:
4870 std::swap(TV, FV);
4871 case ISD::SETEQ:
4872 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
4873 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4874 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
4875 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
4876 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4877 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4878 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4879 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004880 case ISD::SETULT:
4881 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004882 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004883 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4884 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004885 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004886 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004887 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004888 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004889 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4890 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004891 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004892 case ISD::SETUGT:
4893 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004894 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004895 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4896 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004897 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004898 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004899 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004900 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004901 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4902 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004903 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004904 }
Eli Friedman5806e182009-05-28 04:31:08 +00004905 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00004906}
4907
Chris Lattner57ee7c62007-11-28 18:44:47 +00004908// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen37bc85f2009-06-04 20:53:52 +00004909SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004910 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00004911 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004912 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00004913 if (Src.getValueType() == MVT::f32)
4914 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00004915
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004916 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00004917 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004918 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00004919 case MVT::i32:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00004920 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Hal Finkelf6d45f22013-04-01 17:52:07 +00004921 (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ :
4922 PPCISD::FCTIDZ),
Owen Anderson9f944592009-08-11 20:47:22 +00004923 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00004924 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004925 case MVT::i64:
Hal Finkel3f88d082013-04-01 18:42:58 +00004926 assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) &&
4927 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00004928 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
4929 PPCISD::FCTIDUZ,
4930 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00004931 break;
4932 }
Duncan Sands2a287912008-07-19 16:26:02 +00004933
Chris Lattner4211ca92006-04-14 06:01:58 +00004934 // Convert the FP value to an int value through memory.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004935 bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() &&
4936 (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT());
4937 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
4938 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
4939 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00004940
Chris Lattner06a49542007-10-15 20:14:52 +00004941 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004942 SDValue Chain;
4943 if (i32Stack) {
4944 MachineFunction &MF = DAG.getMachineFunction();
4945 MachineMemOperand *MMO =
4946 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
4947 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
4948 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
4949 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
4950 MVT::i32, MMO);
4951 } else
4952 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
4953 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00004954
4955 // Result is a load from the stack slot. If loading 4 bytes, make sure to
4956 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004957 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00004958 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00004959 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkelf6d45f22013-04-01 17:52:07 +00004960 MPI = MachinePointerInfo();
4961 }
4962
4963 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004964 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00004965}
4966
Hal Finkelf6d45f22013-04-01 17:52:07 +00004967SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004968 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004969 SDLoc dl(Op);
Dan Gohmand6819da2008-03-11 01:59:03 +00004970 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00004971 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004972 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00004973
Hal Finkelf6d45f22013-04-01 17:52:07 +00004974 assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) &&
4975 "UINT_TO_FP is supported only with FPCVT");
4976
4977 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00004978 // Otherwise, convert to double-precision and then round.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004979 unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4980 (Op.getOpcode() == ISD::UINT_TO_FP ?
4981 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
4982 (Op.getOpcode() == ISD::UINT_TO_FP ?
4983 PPCISD::FCFIDU : PPCISD::FCFID);
4984 MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4985 MVT::f32 : MVT::f64;
4986
Owen Anderson9f944592009-08-11 20:47:22 +00004987 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00004988 SDValue SINT = Op.getOperand(0);
4989 // When converting to single-precision, we actually need to convert
4990 // to double-precision first and then round to single-precision.
4991 // To avoid double-rounding effects during that operation, we have
4992 // to prepare the input operand. Bits that might be truncated when
4993 // converting to double-precision are replaced by a bit that won't
4994 // be lost at this stage, but is below the single-precision rounding
4995 // position.
4996 //
4997 // However, if -enable-unsafe-fp-math is in effect, accept double
4998 // rounding to avoid the extra overhead.
4999 if (Op.getValueType() == MVT::f32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005000 !PPCSubTarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005001 !DAG.getTarget().Options.UnsafeFPMath) {
5002
5003 // Twiddle input to make sure the low 11 bits are zero. (If this
5004 // is the case, we are guaranteed the value will fit into the 53 bit
5005 // mantissa of an IEEE double-precision value without rounding.)
5006 // If any of those low 11 bits were not zero originally, make sure
5007 // bit 12 (value 2048) is set instead, so that the final rounding
5008 // to single-precision gets the correct result.
5009 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5010 SINT, DAG.getConstant(2047, MVT::i64));
5011 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
5012 Round, DAG.getConstant(2047, MVT::i64));
5013 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
5014 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5015 Round, DAG.getConstant(-2048, MVT::i64));
5016
5017 // However, we cannot use that value unconditionally: if the magnitude
5018 // of the input value is small, the bit-twiddling we did above might
5019 // end up visibly changing the output. Fortunately, in that case, we
5020 // don't need to twiddle bits since the original input will convert
5021 // exactly to double-precision floating-point already. Therefore,
5022 // construct a conditional to use the original value if the top 11
5023 // bits are all sign-bit copies, and use the rounded value computed
5024 // above otherwise.
5025 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
5026 SINT, DAG.getConstant(53, MVT::i32));
5027 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
5028 Cond, DAG.getConstant(1, MVT::i64));
5029 Cond = DAG.getSetCC(dl, MVT::i32,
5030 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
5031
5032 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
5033 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00005034
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005035 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005036 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
5037
5038 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00005039 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005040 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005041 return FP;
5042 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005043
Owen Anderson9f944592009-08-11 20:47:22 +00005044 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005045 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005046 // Since we only generate this in 64-bit mode, we can take advantage of
5047 // 64-bit registers. In particular, sign extend the input value into the
5048 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
5049 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00005050 MachineFunction &MF = DAG.getMachineFunction();
5051 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005052 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005053
Hal Finkelbeb296b2013-03-31 10:12:51 +00005054 SDValue Ld;
Hal Finkelf6d45f22013-04-01 17:52:07 +00005055 if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) {
Hal Finkelbeb296b2013-03-31 10:12:51 +00005056 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
5057 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005058
Hal Finkelbeb296b2013-03-31 10:12:51 +00005059 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
5060 MachinePointerInfo::getFixedStack(FrameIdx),
5061 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00005062
Hal Finkelbeb296b2013-03-31 10:12:51 +00005063 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
5064 "Expected an i32 store");
5065 MachineMemOperand *MMO =
5066 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
5067 MachineMemOperand::MOLoad, 4, 4);
5068 SDValue Ops[] = { Store, FIdx };
Hal Finkelf6d45f22013-04-01 17:52:07 +00005069 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
5070 PPCISD::LFIWZX : PPCISD::LFIWAX,
5071 dl, DAG.getVTList(MVT::f64, MVT::Other),
5072 Ops, 2, MVT::i32, MMO);
Hal Finkelbeb296b2013-03-31 10:12:51 +00005073 } else {
Hal Finkelf6d45f22013-04-01 17:52:07 +00005074 assert(PPCSubTarget.isPPC64() &&
5075 "i32->FP without LFIWAX supported only on PPC64");
5076
Hal Finkelbeb296b2013-03-31 10:12:51 +00005077 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
5078 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
5079
5080 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
5081 Op.getOperand(0));
5082
5083 // STD the extended value into the stack slot.
5084 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
5085 MachinePointerInfo::getFixedStack(FrameIdx),
5086 false, false, 0);
5087
5088 // Load the value as a double.
5089 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
5090 MachinePointerInfo::getFixedStack(FrameIdx),
5091 false, false, false, 0);
5092 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005093
Chris Lattner4211ca92006-04-14 06:01:58 +00005094 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005095 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
5096 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00005097 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005098 return FP;
5099}
5100
Dan Gohman21cea8a2010-04-17 15:26:15 +00005101SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
5102 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005103 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005104 /*
5105 The rounding mode is in bits 30:31 of FPSR, and has the following
5106 settings:
5107 00 Round to nearest
5108 01 Round to 0
5109 10 Round to +inf
5110 11 Round to -inf
5111
5112 FLT_ROUNDS, on the other hand, expects the following:
5113 -1 Undefined
5114 0 Round to 0
5115 1 Round to nearest
5116 2 Round to +inf
5117 3 Round to -inf
5118
5119 To perform the conversion, we do:
5120 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
5121 */
5122
5123 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005124 EVT VT = Op.getValueType();
5125 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005126 SDValue MFFSreg, InFlag;
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005127
5128 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005129 EVT NodeTys[] = {
5130 MVT::f64, // return register
5131 MVT::Glue // unused in this context
5132 };
Dale Johannesen021052a2009-02-04 20:06:27 +00005133 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005134
5135 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00005136 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005137 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005138 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00005139 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005140
5141 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005142 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005143 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00005144 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005145 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005146
5147 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005148 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00005149 DAG.getNode(ISD::AND, dl, MVT::i32,
5150 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005151 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00005152 DAG.getNode(ISD::SRL, dl, MVT::i32,
5153 DAG.getNode(ISD::AND, dl, MVT::i32,
5154 DAG.getNode(ISD::XOR, dl, MVT::i32,
5155 CWD, DAG.getConstant(3, MVT::i32)),
5156 DAG.getConstant(3, MVT::i32)),
5157 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005158
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005159 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00005160 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005161
Duncan Sands13237ac2008-06-06 12:08:01 +00005162 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00005163 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005164}
5165
Dan Gohman21cea8a2010-04-17 15:26:15 +00005166SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005167 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005168 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005169 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00005170 assert(Op.getNumOperands() == 3 &&
5171 VT == Op.getOperand(1).getValueType() &&
5172 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005173
Chris Lattner601b8652006-09-20 03:47:40 +00005174 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005175 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005176 SDValue Lo = Op.getOperand(0);
5177 SDValue Hi = Op.getOperand(1);
5178 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005179 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005180
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005181 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005182 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005183 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5184 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5185 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5186 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005187 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005188 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5189 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5190 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005191 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005192 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005193}
5194
Dan Gohman21cea8a2010-04-17 15:26:15 +00005195SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005196 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005197 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00005198 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005199 assert(Op.getNumOperands() == 3 &&
5200 VT == Op.getOperand(1).getValueType() &&
5201 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005202
Dan Gohman8d2ead22008-03-07 20:36:53 +00005203 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005204 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005205 SDValue Lo = Op.getOperand(0);
5206 SDValue Hi = Op.getOperand(1);
5207 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005208 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005209
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005210 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005211 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005212 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5213 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5214 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5215 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005216 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005217 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5218 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5219 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005220 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005221 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005222}
5223
Dan Gohman21cea8a2010-04-17 15:26:15 +00005224SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005225 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005226 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005227 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005228 assert(Op.getNumOperands() == 3 &&
5229 VT == Op.getOperand(1).getValueType() &&
5230 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005231
Dan Gohman8d2ead22008-03-07 20:36:53 +00005232 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005233 SDValue Lo = Op.getOperand(0);
5234 SDValue Hi = Op.getOperand(1);
5235 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005236 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005237
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005238 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005239 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005240 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5241 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5242 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5243 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005244 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005245 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5246 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5247 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00005248 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005249 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005250 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005251}
5252
5253//===----------------------------------------------------------------------===//
5254// Vector related lowering.
5255//
5256
Chris Lattner2a099c02006-04-17 06:00:21 +00005257/// BuildSplatI - Build a canonical splati of Val with an element size of
5258/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005259static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005260 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00005261 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005262
Owen Anderson53aa7a92009-08-10 22:56:29 +00005263 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00005264 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00005265 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005266
Owen Anderson9f944592009-08-11 20:47:22 +00005267 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005268
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005269 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5270 if (Val == -1)
5271 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005272
Owen Anderson53aa7a92009-08-10 22:56:29 +00005273 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005274
Chris Lattner2a099c02006-04-17 06:00:21 +00005275 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00005276 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005277 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00005278 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga49de9d2009-02-25 22:49:59 +00005279 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
5280 &Ops[0], Ops.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00005281 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005282}
5283
Hal Finkelcf2e9082013-05-24 23:00:14 +00005284/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5285/// specified intrinsic ID.
5286static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005287 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00005288 EVT DestVT = MVT::Other) {
5289 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5290 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5291 DAG.getConstant(IID, MVT::i32), Op);
5292}
5293
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005294/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00005295/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005296static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005297 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005298 EVT DestVT = MVT::Other) {
5299 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005300 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005301 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005302}
5303
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005304/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5305/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005306static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005307 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005308 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00005309 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005310 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005311 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005312}
5313
5314
Chris Lattner264c9082006-04-17 17:55:10 +00005315/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5316/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005317static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005318 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00005319 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00005320 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5321 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005322
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005323 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00005324 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005325 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00005326 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005327 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00005328}
5329
Chris Lattner19e90552006-04-14 05:19:18 +00005330// If this is a case we can't handle, return null and let the default
5331// expansion code take care of it. If we CAN select this case, and if it
5332// selects to a single instruction, return Op. Otherwise, if we can codegen
5333// this case more efficiently than a constant pool load, lower it to the
5334// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005335SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5336 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005337 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005338 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5339 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00005340
Bob Wilson85cefe82009-03-02 23:24:16 +00005341 // Check if this is a splat of a constant value.
5342 APInt APSplatBits, APSplatUndef;
5343 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005344 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00005345 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00005346 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00005347 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00005348
Bob Wilson530e0382009-03-03 19:26:27 +00005349 unsigned SplatBits = APSplatBits.getZExtValue();
5350 unsigned SplatUndef = APSplatUndef.getZExtValue();
5351 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005352
Bob Wilson530e0382009-03-03 19:26:27 +00005353 // First, handle single instruction cases.
5354
5355 // All zeros?
5356 if (SplatBits == 0) {
5357 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00005358 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5359 SDValue Z = DAG.getConstant(0, MVT::i32);
5360 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00005361 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00005362 }
Bob Wilson530e0382009-03-03 19:26:27 +00005363 return Op;
5364 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00005365
Bob Wilson530e0382009-03-03 19:26:27 +00005366 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5367 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5368 (32-SplatBitSize));
5369 if (SextVal >= -16 && SextVal <= 15)
5370 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005371
5372
Bob Wilson530e0382009-03-03 19:26:27 +00005373 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005374
Bob Wilson530e0382009-03-03 19:26:27 +00005375 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00005376 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5377 // If this value is in the range [17,31] and is odd, use:
5378 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5379 // If this value is in the range [-31,-17] and is odd, use:
5380 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5381 // Note the last two are three-instruction sequences.
5382 if (SextVal >= -32 && SextVal <= 31) {
5383 // To avoid having these optimizations undone by constant folding,
5384 // we convert to a pseudo that will be expanded later into one of
5385 // the above forms.
5386 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt51e79512013-02-20 15:50:31 +00005387 EVT VT = Op.getValueType();
5388 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5389 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5390 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilson530e0382009-03-03 19:26:27 +00005391 }
5392
5393 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5394 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5395 // for fneg/fabs.
5396 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5397 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00005398 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005399
5400 // Make the VSLW intrinsic, computing 0x8000_0000.
5401 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5402 OnesV, DAG, dl);
5403
5404 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00005405 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00005406 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005407 }
5408
5409 // Check to see if this is a wide variety of vsplti*, binop self cases.
5410 static const signed char SplatCsts[] = {
5411 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5412 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5413 };
5414
5415 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5416 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5417 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5418 int i = SplatCsts[idx];
5419
5420 // Figure out what shift amount will be used by altivec if shifted by i in
5421 // this splat size.
5422 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5423
5424 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00005425 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005426 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005427 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5428 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5429 Intrinsic::ppc_altivec_vslw
5430 };
5431 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005432 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005433 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005434
Bob Wilson530e0382009-03-03 19:26:27 +00005435 // vsplti + srl self.
5436 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005437 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005438 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5439 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5440 Intrinsic::ppc_altivec_vsrw
5441 };
5442 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005443 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005444 }
5445
Bob Wilson530e0382009-03-03 19:26:27 +00005446 // vsplti + sra self.
5447 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005448 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005449 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5450 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5451 Intrinsic::ppc_altivec_vsraw
5452 };
5453 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005454 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005455 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005456
Bob Wilson530e0382009-03-03 19:26:27 +00005457 // vsplti + rol self.
5458 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5459 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005460 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005461 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5462 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5463 Intrinsic::ppc_altivec_vrlw
5464 };
5465 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005466 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005467 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005468
Bob Wilson530e0382009-03-03 19:26:27 +00005469 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00005470 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005471 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005472 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00005473 }
Bob Wilson530e0382009-03-03 19:26:27 +00005474 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00005475 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005476 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005477 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00005478 }
Bob Wilson530e0382009-03-03 19:26:27 +00005479 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00005480 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005481 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005482 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5483 }
5484 }
5485
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005486 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00005487}
5488
Chris Lattner071ad012006-04-17 05:28:54 +00005489/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5490/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005491static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005492 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005493 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00005494 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00005495 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00005496 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005497
Chris Lattner071ad012006-04-17 05:28:54 +00005498 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00005499 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 +00005500 OP_VMRGHW,
5501 OP_VMRGLW,
5502 OP_VSPLTISW0,
5503 OP_VSPLTISW1,
5504 OP_VSPLTISW2,
5505 OP_VSPLTISW3,
5506 OP_VSLDOI4,
5507 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00005508 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00005509 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00005510
Chris Lattner071ad012006-04-17 05:28:54 +00005511 if (OpNum == OP_COPY) {
5512 if (LHSID == (1*9+2)*9+3) return LHS;
5513 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5514 return RHS;
5515 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005516
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005517 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005518 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5519 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005520
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005521 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00005522 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005523 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00005524 case OP_VMRGHW:
5525 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5526 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5527 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5528 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5529 break;
5530 case OP_VMRGLW:
5531 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5532 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5533 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5534 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5535 break;
5536 case OP_VSPLTISW0:
5537 for (unsigned i = 0; i != 16; ++i)
5538 ShufIdxs[i] = (i&3)+0;
5539 break;
5540 case OP_VSPLTISW1:
5541 for (unsigned i = 0; i != 16; ++i)
5542 ShufIdxs[i] = (i&3)+4;
5543 break;
5544 case OP_VSPLTISW2:
5545 for (unsigned i = 0; i != 16; ++i)
5546 ShufIdxs[i] = (i&3)+8;
5547 break;
5548 case OP_VSPLTISW3:
5549 for (unsigned i = 0; i != 16; ++i)
5550 ShufIdxs[i] = (i&3)+12;
5551 break;
5552 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005553 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005554 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005555 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005556 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005557 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005558 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00005559 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00005560 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5561 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005562 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00005563 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00005564}
5565
Chris Lattner19e90552006-04-14 05:19:18 +00005566/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5567/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5568/// return the code it can be lowered into. Worst case, it can always be
5569/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005570SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005571 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005572 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005573 SDValue V1 = Op.getOperand(0);
5574 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005575 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005576 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005577
Chris Lattner19e90552006-04-14 05:19:18 +00005578 // Cases that are handled by instructions that take permute immediates
5579 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5580 // selected by the instruction selector.
5581 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005582 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5583 PPC::isSplatShuffleMask(SVOp, 2) ||
5584 PPC::isSplatShuffleMask(SVOp, 4) ||
5585 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5586 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5587 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5588 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5589 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5590 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5591 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5592 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5593 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattner19e90552006-04-14 05:19:18 +00005594 return Op;
5595 }
5596 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005597
Chris Lattner19e90552006-04-14 05:19:18 +00005598 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5599 // and produce a fixed permutation. If any of these match, do not lower to
5600 // VPERM.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005601 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5602 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5603 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5604 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5605 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5606 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5607 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5608 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5609 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattner19e90552006-04-14 05:19:18 +00005610 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005611
Chris Lattner071ad012006-04-17 05:28:54 +00005612 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5613 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005614 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00005615
Chris Lattner071ad012006-04-17 05:28:54 +00005616 unsigned PFIndexes[4];
5617 bool isFourElementShuffle = true;
5618 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5619 unsigned EltNo = 8; // Start out undef.
5620 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005621 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00005622 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005623
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005624 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00005625 if ((ByteSource & 3) != j) {
5626 isFourElementShuffle = false;
5627 break;
5628 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005629
Chris Lattner071ad012006-04-17 05:28:54 +00005630 if (EltNo == 8) {
5631 EltNo = ByteSource/4;
5632 } else if (EltNo != ByteSource/4) {
5633 isFourElementShuffle = false;
5634 break;
5635 }
5636 }
5637 PFIndexes[i] = EltNo;
5638 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005639
5640 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00005641 // perfect shuffle vector to determine if it is cost effective to do this as
5642 // discrete instructions, or whether we should use a vperm.
5643 if (isFourElementShuffle) {
5644 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005645 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00005646 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005647
Chris Lattner071ad012006-04-17 05:28:54 +00005648 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5649 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005650
Chris Lattner071ad012006-04-17 05:28:54 +00005651 // Determining when to avoid vperm is tricky. Many things affect the cost
5652 // of vperm, particularly how many times the perm mask needs to be computed.
5653 // For example, if the perm mask can be hoisted out of a loop or is already
5654 // used (perhaps because there are multiple permutes with the same shuffle
5655 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5656 // the loop requires an extra register.
5657 //
5658 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00005659 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00005660 // available, if this block is within a loop, we should avoid using vperm
5661 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005662 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005663 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005664 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005665
Chris Lattner19e90552006-04-14 05:19:18 +00005666 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5667 // vector that will get spilled to the constant pool.
5668 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005669
Chris Lattner19e90552006-04-14 05:19:18 +00005670 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5671 // that it is in input element units, not in bytes. Convert now.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005672 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005673 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005674
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005675 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005676 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5677 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005678
Chris Lattner19e90552006-04-14 05:19:18 +00005679 for (unsigned j = 0; j != BytesPerElement; ++j)
5680 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson9f944592009-08-11 20:47:22 +00005681 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00005682 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005683
Owen Anderson9f944592009-08-11 20:47:22 +00005684 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga49de9d2009-02-25 22:49:59 +00005685 &ResultMask[0], ResultMask.size());
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005686 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00005687}
5688
Chris Lattner9754d142006-04-18 17:59:36 +00005689/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5690/// altivec comparison. If it is, return true and fill in Opc/isDot with
5691/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005692static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner9754d142006-04-18 17:59:36 +00005693 bool &isDot) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005694 unsigned IntrinsicID =
5695 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00005696 CompareOpc = -1;
5697 isDot = false;
5698 switch (IntrinsicID) {
5699 default: return false;
5700 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00005701 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5702 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5703 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5704 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5705 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5706 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5707 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5708 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5709 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5710 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5711 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5712 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5713 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005714
Chris Lattner4211ca92006-04-14 06:01:58 +00005715 // Normal Comparisons.
5716 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5717 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5718 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5719 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5720 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5721 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5722 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5723 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5724 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5725 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5726 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5727 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5728 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5729 }
Chris Lattner9754d142006-04-18 17:59:36 +00005730 return true;
5731}
5732
5733/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5734/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005735SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005736 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00005737 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5738 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005739 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00005740 int CompareOpc;
5741 bool isDot;
5742 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005743 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005744
Chris Lattner9754d142006-04-18 17:59:36 +00005745 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00005746 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00005747 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00005748 Op.getOperand(1), Op.getOperand(2),
5749 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00005750 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00005751 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005752
Chris Lattner4211ca92006-04-14 06:01:58 +00005753 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005754 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005755 Op.getOperand(2), // LHS
5756 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00005757 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005758 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005759 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00005760 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005761
Chris Lattner4211ca92006-04-14 06:01:58 +00005762 // Now that we have the comparison, emit a copy from the CR to a GPR.
5763 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00005764 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00005765 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00005766 CompNode.getValue(1));
5767
Chris Lattner4211ca92006-04-14 06:01:58 +00005768 // Unpack the result based on how the target uses it.
5769 unsigned BitNo; // Bit # of CR6.
5770 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00005771 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00005772 default: // Can't happen, don't crash on invalid number though.
5773 case 0: // Return the value of the EQ bit of CR6.
5774 BitNo = 0; InvertBit = false;
5775 break;
5776 case 1: // Return the inverted value of the EQ bit of CR6.
5777 BitNo = 0; InvertBit = true;
5778 break;
5779 case 2: // Return the value of the LT bit of CR6.
5780 BitNo = 2; InvertBit = false;
5781 break;
5782 case 3: // Return the inverted value of the LT bit of CR6.
5783 BitNo = 2; InvertBit = true;
5784 break;
5785 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005786
Chris Lattner4211ca92006-04-14 06:01:58 +00005787 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00005788 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5789 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005790 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00005791 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5792 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00005793
Chris Lattner4211ca92006-04-14 06:01:58 +00005794 // If we are supposed to, toggle the bit.
5795 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00005796 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5797 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005798 return Flags;
5799}
5800
Scott Michelcf0da6c2009-02-17 22:15:04 +00005801SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005802 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005803 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00005804 // Create a stack slot that is 16-byte aligned.
5805 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00005806 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00005807 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005808 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005809
Chris Lattner4211ca92006-04-14 06:01:58 +00005810 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00005811 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00005812 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005813 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005814 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00005815 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005816 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005817}
5818
Dan Gohman21cea8a2010-04-17 15:26:15 +00005819SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005820 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005821 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005822 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005823
Owen Anderson9f944592009-08-11 20:47:22 +00005824 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
5825 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005826
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005827 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005828 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005829
Chris Lattner7e4398742006-04-18 03:43:48 +00005830 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00005831 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
5832 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
5833 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005834
Chris Lattner7e4398742006-04-18 03:43:48 +00005835 // Low parts multiplied together, generating 32-bit results (we ignore the
5836 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005837 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00005838 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005839
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005840 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00005841 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00005842 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005843 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005844 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005845 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
5846 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005847 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005848
Owen Anderson9f944592009-08-11 20:47:22 +00005849 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00005850
Chris Lattner96d50482006-04-18 04:28:57 +00005851 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005852 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005853 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005854 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005855
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005856 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005857 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00005858 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005859 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005860
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005861 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005862 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00005863 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005864 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005865
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005866 // Merge the results together.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005867 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005868 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005869 Ops[i*2 ] = 2*i+1;
5870 Ops[i*2+1] = 2*i+1+16;
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005871 }
Owen Anderson9f944592009-08-11 20:47:22 +00005872 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00005873 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005874 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00005875 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005876}
5877
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005878/// LowerOperation - Provide custom lowering hooks for some operations.
5879///
Dan Gohman21cea8a2010-04-17 15:26:15 +00005880SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005881 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005882 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005883 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00005884 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005885 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00005886 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00005887 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005888 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00005889 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
5890 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005891 case ISD::VASTART:
Dan Gohman31ae5862010-04-17 14:41:14 +00005892 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005893
5894 case ISD::VAARG:
Dan Gohman31ae5862010-04-17 14:41:14 +00005895 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00005896
Roman Divackyc3825df2013-07-25 21:36:47 +00005897 case ISD::VACOPY:
5898 return LowerVACOPY(Op, DAG, PPCSubTarget);
5899
Jim Laskeye4f4d042006-12-04 22:04:42 +00005900 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00005901 case ISD::DYNAMIC_STACKALLOC:
5902 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng51096af2008-04-19 01:30:48 +00005903
Hal Finkel756810f2013-03-21 21:37:52 +00005904 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
5905 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
5906
Hal Finkel940ab932014-02-28 00:27:01 +00005907 case ISD::LOAD: return LowerLOAD(Op, DAG);
5908 case ISD::STORE: return LowerSTORE(Op, DAG);
5909 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005910 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005911 case ISD::FP_TO_UINT:
5912 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005913 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00005914 case ISD::UINT_TO_FP:
5915 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00005916 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00005917
Chris Lattner4211ca92006-04-14 06:01:58 +00005918 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00005919 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
5920 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
5921 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00005922
Chris Lattner4211ca92006-04-14 06:01:58 +00005923 // Vector-related lowering.
5924 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5925 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5926 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5927 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005928 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005929
Hal Finkel25c19922013-05-15 21:37:41 +00005930 // For counter-based loop handling.
5931 case ISD::INTRINSIC_W_CHAIN: return SDValue();
5932
Chris Lattnerf6a81562007-12-08 06:59:59 +00005933 // Frame & Return address.
5934 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00005935 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00005936 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005937}
5938
Duncan Sands6ed40142008-12-01 11:39:25 +00005939void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
5940 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005941 SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00005942 const TargetMachine &TM = getTargetMachine();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005943 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00005944 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00005945 default:
Craig Toppere55c5562012-02-07 02:50:20 +00005946 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkel25c19922013-05-15 21:37:41 +00005947 case ISD::INTRINSIC_W_CHAIN: {
5948 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
5949 Intrinsic::ppc_is_decremented_ctr_nonzero)
5950 break;
5951
5952 assert(N->getValueType(0) == MVT::i1 &&
5953 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00005954 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00005955 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
5956 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
5957 N->getOperand(1));
5958
5959 Results.push_back(NewInt);
5960 Results.push_back(NewInt.getValue(1));
5961 break;
5962 }
Roman Divacky4394e682011-06-28 15:30:42 +00005963 case ISD::VAARG: {
5964 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
5965 || TM.getSubtarget<PPCSubtarget>().isPPC64())
5966 return;
5967
5968 EVT VT = N->getValueType(0);
5969
5970 if (VT == MVT::i64) {
5971 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
5972
5973 Results.push_back(NewNode);
5974 Results.push_back(NewNode.getValue(1));
5975 }
5976 return;
5977 }
Duncan Sands6ed40142008-12-01 11:39:25 +00005978 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00005979 assert(N->getValueType(0) == MVT::ppcf128);
5980 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005981 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005982 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00005983 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00005984 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005985 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00005986 DAG.getIntPtrConstant(1));
5987
Ulrich Weigand874fc622013-03-26 10:56:22 +00005988 // Add the two halves of the long double in round-to-zero mode.
5989 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00005990
5991 // We know the low half is about to be thrown away, so just use something
5992 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00005993 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00005994 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00005995 return;
Duncan Sands2a287912008-07-19 16:26:02 +00005996 }
Duncan Sands6ed40142008-12-01 11:39:25 +00005997 case ISD::FP_TO_SINT:
Bill Schmidt41221692013-07-09 18:50:20 +00005998 // LowerFP_TO_INT() can only handle f32 and f64.
5999 if (N->getOperand(0).getValueType() == MVT::ppcf128)
6000 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006001 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00006002 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00006003 }
6004}
6005
6006
Chris Lattner4211ca92006-04-14 06:01:58 +00006007//===----------------------------------------------------------------------===//
6008// Other Lowering Code
6009//===----------------------------------------------------------------------===//
6010
Chris Lattner9b577f12005-08-26 21:23:58 +00006011MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00006012PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman747e55b2009-02-07 16:15:20 +00006013 bool is64bit, unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006014 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesend4eb0522008-08-25 22:34:37 +00006015 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6016
6017 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6018 MachineFunction *F = BB->getParent();
6019 MachineFunction::iterator It = BB;
6020 ++It;
6021
6022 unsigned dest = MI->getOperand(0).getReg();
6023 unsigned ptrA = MI->getOperand(1).getReg();
6024 unsigned ptrB = MI->getOperand(2).getReg();
6025 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006026 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00006027
6028 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6029 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6030 F->insert(It, loopMBB);
6031 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006032 exitMBB->splice(exitMBB->begin(), BB,
6033 llvm::next(MachineBasicBlock::iterator(MI)),
6034 BB->end());
6035 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006036
6037 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006038 unsigned TmpReg = (!BinOpcode) ? incr :
6039 RegInfo.createVirtualRegister(
Dale Johannesenbc698292008-09-02 20:30:23 +00006040 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6041 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006042
6043 // thisMBB:
6044 // ...
6045 // fallthrough --> loopMBB
6046 BB->addSuccessor(loopMBB);
6047
6048 // loopMBB:
6049 // l[wd]arx dest, ptr
6050 // add r0, dest, incr
6051 // st[wd]cx. r0, ptr
6052 // bne- loopMBB
6053 // fallthrough --> exitMBB
6054 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006055 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00006056 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006057 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006058 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
6059 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesend4eb0522008-08-25 22:34:37 +00006060 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006061 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006062 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006063 BB->addSuccessor(loopMBB);
6064 BB->addSuccessor(exitMBB);
6065
6066 // exitMBB:
6067 // ...
6068 BB = exitMBB;
6069 return BB;
6070}
6071
6072MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00006073PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00006074 MachineBasicBlock *BB,
6075 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00006076 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006077 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesena32affb2008-08-28 17:53:09 +00006078 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6079 // In 64 bit mode we have to use 64 bits for addresses, even though the
6080 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
6081 // registers without caring whether they're 32 or 64, but here we're
6082 // doing actual arithmetic on the addresses.
6083 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00006084 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00006085
6086 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6087 MachineFunction *F = BB->getParent();
6088 MachineFunction::iterator It = BB;
6089 ++It;
6090
6091 unsigned dest = MI->getOperand(0).getReg();
6092 unsigned ptrA = MI->getOperand(1).getReg();
6093 unsigned ptrB = MI->getOperand(2).getReg();
6094 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006095 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00006096
6097 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6098 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6099 F->insert(It, loopMBB);
6100 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006101 exitMBB->splice(exitMBB->begin(), BB,
6102 llvm::next(MachineBasicBlock::iterator(MI)),
6103 BB->end());
6104 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006105
6106 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006107 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006108 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6109 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00006110 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6111 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6112 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6113 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
6114 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6115 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6116 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6117 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6118 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
6119 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006120 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006121 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006122 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006123
6124 // thisMBB:
6125 // ...
6126 // fallthrough --> loopMBB
6127 BB->addSuccessor(loopMBB);
6128
6129 // The 4-byte load must be aligned, while a char or short may be
6130 // anywhere in the word. Hence all this nasty bookkeeping code.
6131 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6132 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006133 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00006134 // rlwinm ptr, ptr1, 0, 0, 29
6135 // slw incr2, incr, shift
6136 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6137 // slw mask, mask2, shift
6138 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00006139 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006140 // add tmp, tmpDest, incr2
6141 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00006142 // and tmp3, tmp, mask
6143 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00006144 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00006145 // bne- loopMBB
6146 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006147 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006148 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00006149 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006150 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006151 .addReg(ptrA).addReg(ptrB);
6152 } else {
6153 Ptr1Reg = ptrB;
6154 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006155 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006156 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006157 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006158 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6159 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006160 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006161 .addReg(Ptr1Reg).addImm(0).addImm(61);
6162 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006163 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006164 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006165 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006166 .addReg(incr).addReg(ShiftReg);
6167 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006168 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00006169 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006170 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6171 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00006172 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006173 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006174 .addReg(Mask2Reg).addReg(ShiftReg);
6175
6176 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006177 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006178 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006179 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006180 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006181 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006182 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006183 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006184 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006185 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006186 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006187 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00006188 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006189 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006190 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006191 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006192 BB->addSuccessor(loopMBB);
6193 BB->addSuccessor(exitMBB);
6194
6195 // exitMBB:
6196 // ...
6197 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006198 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6199 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00006200 return BB;
6201}
6202
Hal Finkel756810f2013-03-21 21:37:52 +00006203llvm::MachineBasicBlock*
6204PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6205 MachineBasicBlock *MBB) const {
6206 DebugLoc DL = MI->getDebugLoc();
6207 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6208
6209 MachineFunction *MF = MBB->getParent();
6210 MachineRegisterInfo &MRI = MF->getRegInfo();
6211
6212 const BasicBlock *BB = MBB->getBasicBlock();
6213 MachineFunction::iterator I = MBB;
6214 ++I;
6215
6216 // Memory Reference
6217 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6218 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6219
6220 unsigned DstReg = MI->getOperand(0).getReg();
6221 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6222 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6223 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6224 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6225
6226 MVT PVT = getPointerTy();
6227 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6228 "Invalid Pointer Size!");
6229 // For v = setjmp(buf), we generate
6230 //
6231 // thisMBB:
6232 // SjLjSetup mainMBB
6233 // bl mainMBB
6234 // v_restore = 1
6235 // b sinkMBB
6236 //
6237 // mainMBB:
6238 // buf[LabelOffset] = LR
6239 // v_main = 0
6240 //
6241 // sinkMBB:
6242 // v = phi(main, restore)
6243 //
6244
6245 MachineBasicBlock *thisMBB = MBB;
6246 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6247 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6248 MF->insert(I, mainMBB);
6249 MF->insert(I, sinkMBB);
6250
6251 MachineInstrBuilder MIB;
6252
6253 // Transfer the remainder of BB and its successor edges to sinkMBB.
6254 sinkMBB->splice(sinkMBB->begin(), MBB,
6255 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
6256 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6257
6258 // Note that the structure of the jmp_buf used here is not compatible
6259 // with that used by libc, and is not designed to be. Specifically, it
6260 // stores only those 'reserved' registers that LLVM does not otherwise
6261 // understand how to spill. Also, by convention, by the time this
6262 // intrinsic is called, Clang has already stored the frame address in the
6263 // first slot of the buffer and stack address in the third. Following the
6264 // X86 target code, we'll store the jump address in the second slot. We also
6265 // need to save the TOC pointer (R2) to handle jumps between shared
6266 // libraries, and that will be stored in the fourth slot. The thread
6267 // identifier (R13) is not affected.
6268
6269 // thisMBB:
6270 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6271 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006272 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006273
6274 // Prepare IP either in reg.
6275 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6276 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6277 unsigned BufReg = MI->getOperand(1).getReg();
6278
6279 if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) {
6280 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6281 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006282 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006283 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006284 MIB.setMemRefs(MMOBegin, MMOEnd);
6285 }
6286
Hal Finkelf05d6c72013-07-17 23:50:51 +00006287 // Naked functions never have a base pointer, and so we use r1. For all
6288 // other functions, this decision must be delayed until during PEI.
6289 unsigned BaseReg;
6290 if (MF->getFunction()->getAttributes().hasAttribute(
6291 AttributeSet::FunctionIndex, Attribute::Naked))
6292 BaseReg = PPCSubTarget.isPPC64() ? PPC::X1 : PPC::R1;
6293 else
6294 BaseReg = PPCSubTarget.isPPC64() ? PPC::BP8 : PPC::BP;
6295
6296 MIB = BuildMI(*thisMBB, MI, DL,
6297 TII->get(PPCSubTarget.isPPC64() ? PPC::STD : PPC::STW))
6298 .addReg(BaseReg)
6299 .addImm(BPOffset)
6300 .addReg(BufReg);
6301 MIB.setMemRefs(MMOBegin, MMOEnd);
6302
Hal Finkel756810f2013-03-21 21:37:52 +00006303 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00006304 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling5e7656b2013-06-07 07:55:53 +00006305 const PPCRegisterInfo *TRI =
6306 static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo());
6307 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00006308
6309 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6310
6311 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6312 .addMBB(mainMBB);
6313 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6314
6315 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6316 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6317
6318 // mainMBB:
6319 // mainDstReg = 0
6320 MIB = BuildMI(mainMBB, DL,
6321 TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
6322
6323 // Store IP
6324 if (PPCSubTarget.isPPC64()) {
6325 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6326 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006327 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006328 .addReg(BufReg);
6329 } else {
6330 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6331 .addReg(LabelReg)
6332 .addImm(LabelOffset)
6333 .addReg(BufReg);
6334 }
6335
6336 MIB.setMemRefs(MMOBegin, MMOEnd);
6337
6338 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6339 mainMBB->addSuccessor(sinkMBB);
6340
6341 // sinkMBB:
6342 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6343 TII->get(PPC::PHI), DstReg)
6344 .addReg(mainDstReg).addMBB(mainMBB)
6345 .addReg(restoreDstReg).addMBB(thisMBB);
6346
6347 MI->eraseFromParent();
6348 return sinkMBB;
6349}
6350
6351MachineBasicBlock *
6352PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6353 MachineBasicBlock *MBB) const {
6354 DebugLoc DL = MI->getDebugLoc();
6355 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6356
6357 MachineFunction *MF = MBB->getParent();
6358 MachineRegisterInfo &MRI = MF->getRegInfo();
6359
6360 // Memory Reference
6361 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6362 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6363
6364 MVT PVT = getPointerTy();
6365 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6366 "Invalid Pointer Size!");
6367
6368 const TargetRegisterClass *RC =
6369 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6370 unsigned Tmp = MRI.createVirtualRegister(RC);
6371 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6372 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6373 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006374 unsigned BP = (PVT == MVT::i64) ? PPC::X30 : PPC::R30;
Hal Finkel756810f2013-03-21 21:37:52 +00006375
6376 MachineInstrBuilder MIB;
6377
6378 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6379 const int64_t SPOffset = 2 * PVT.getStoreSize();
6380 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006381 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006382
6383 unsigned BufReg = MI->getOperand(0).getReg();
6384
6385 // Reload FP (the jumped-to function may not have had a
6386 // frame pointer, and if so, then its r31 will be restored
6387 // as necessary).
6388 if (PVT == MVT::i64) {
6389 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6390 .addImm(0)
6391 .addReg(BufReg);
6392 } else {
6393 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6394 .addImm(0)
6395 .addReg(BufReg);
6396 }
6397 MIB.setMemRefs(MMOBegin, MMOEnd);
6398
6399 // Reload IP
6400 if (PVT == MVT::i64) {
6401 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006402 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006403 .addReg(BufReg);
6404 } else {
6405 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6406 .addImm(LabelOffset)
6407 .addReg(BufReg);
6408 }
6409 MIB.setMemRefs(MMOBegin, MMOEnd);
6410
6411 // Reload SP
6412 if (PVT == MVT::i64) {
6413 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006414 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006415 .addReg(BufReg);
6416 } else {
6417 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6418 .addImm(SPOffset)
6419 .addReg(BufReg);
6420 }
6421 MIB.setMemRefs(MMOBegin, MMOEnd);
6422
Hal Finkelf05d6c72013-07-17 23:50:51 +00006423 // Reload BP
6424 if (PVT == MVT::i64) {
6425 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
6426 .addImm(BPOffset)
6427 .addReg(BufReg);
6428 } else {
6429 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
6430 .addImm(BPOffset)
6431 .addReg(BufReg);
6432 }
6433 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00006434
6435 // Reload TOC
6436 if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) {
6437 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006438 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006439 .addReg(BufReg);
6440
6441 MIB.setMemRefs(MMOBegin, MMOEnd);
6442 }
6443
6444 // Jump
6445 BuildMI(*MBB, MI, DL,
6446 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6447 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6448
6449 MI->eraseFromParent();
6450 return MBB;
6451}
6452
Dale Johannesena32affb2008-08-28 17:53:09 +00006453MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00006454PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00006455 MachineBasicBlock *BB) const {
Hal Finkel756810f2013-03-21 21:37:52 +00006456 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6457 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6458 return emitEHSjLjSetJmp(MI, BB);
6459 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6460 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6461 return emitEHSjLjLongJmp(MI, BB);
6462 }
6463
Evan Cheng20350c42006-11-27 23:37:22 +00006464 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00006465
6466 // To "insert" these instructions we actually have to insert their
6467 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00006468 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00006469 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00006470 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00006471
Dan Gohman3b460302008-07-07 23:14:23 +00006472 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00006473
Hal Finkel460e94d2012-06-22 23:10:08 +00006474 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006475 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6476 MI->getOpcode() == PPC::SELECT_I4 ||
6477 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00006478 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00006479 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6480 MI->getOpcode() == PPC::SELECT_CC_I8)
6481 Cond.push_back(MI->getOperand(4));
6482 else
6483 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00006484 Cond.push_back(MI->getOperand(1));
6485
Hal Finkel460e94d2012-06-22 23:10:08 +00006486 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00006487 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6488 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
6489 Cond, MI->getOperand(2).getReg(),
6490 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00006491 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6492 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6493 MI->getOpcode() == PPC::SELECT_CC_F4 ||
6494 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006495 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
6496 MI->getOpcode() == PPC::SELECT_I4 ||
6497 MI->getOpcode() == PPC::SELECT_I8 ||
6498 MI->getOpcode() == PPC::SELECT_F4 ||
6499 MI->getOpcode() == PPC::SELECT_F8 ||
6500 MI->getOpcode() == PPC::SELECT_VRRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00006501 // The incoming instruction knows the destination vreg to set, the
6502 // condition code register to branch on, the true/false values to
6503 // select between, and a branch opcode to use.
6504
6505 // thisMBB:
6506 // ...
6507 // TrueVal = ...
6508 // cmpTY ccX, r1, r2
6509 // bCC copy1MBB
6510 // fallthrough --> copy0MBB
6511 MachineBasicBlock *thisMBB = BB;
6512 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6513 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006514 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006515 F->insert(It, copy0MBB);
6516 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006517
6518 // Transfer the remainder of BB and its successor edges to sinkMBB.
6519 sinkMBB->splice(sinkMBB->begin(), BB,
6520 llvm::next(MachineBasicBlock::iterator(MI)),
6521 BB->end());
6522 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6523
Evan Cheng32e376f2008-07-12 02:23:19 +00006524 // Next, add the true and fallthrough blocks as its successors.
6525 BB->addSuccessor(copy0MBB);
6526 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006527
Hal Finkel940ab932014-02-28 00:27:01 +00006528 if (MI->getOpcode() == PPC::SELECT_I4 ||
6529 MI->getOpcode() == PPC::SELECT_I8 ||
6530 MI->getOpcode() == PPC::SELECT_F4 ||
6531 MI->getOpcode() == PPC::SELECT_F8 ||
6532 MI->getOpcode() == PPC::SELECT_VRRC) {
6533 BuildMI(BB, dl, TII->get(PPC::BC))
6534 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6535 } else {
6536 unsigned SelectPred = MI->getOperand(4).getImm();
6537 BuildMI(BB, dl, TII->get(PPC::BCC))
6538 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6539 }
Dan Gohman34396292010-07-06 20:24:04 +00006540
Evan Cheng32e376f2008-07-12 02:23:19 +00006541 // copy0MBB:
6542 // %FalseValue = ...
6543 // # fallthrough to sinkMBB
6544 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006545
Evan Cheng32e376f2008-07-12 02:23:19 +00006546 // Update machine-CFG edges
6547 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006548
Evan Cheng32e376f2008-07-12 02:23:19 +00006549 // sinkMBB:
6550 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6551 // ...
6552 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00006553 BuildMI(*BB, BB->begin(), dl,
6554 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00006555 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
6556 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6557 }
Dale Johannesena32affb2008-08-28 17:53:09 +00006558 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
6559 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
6560 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
6561 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006562 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
6563 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
6564 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
6565 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006566
6567 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
6568 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
6569 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
6570 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006571 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
6572 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
6573 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
6574 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006575
6576 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
6577 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
6578 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
6579 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006580 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
6581 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
6582 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
6583 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006584
6585 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
6586 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
6587 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
6588 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006589 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
6590 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
6591 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
6592 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006593
6594 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006595 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006596 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006597 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006598 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006599 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006600 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006601 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006602
6603 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6604 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6605 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6606 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006607 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6608 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6609 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6610 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006611
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006612 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6613 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6614 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6615 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6616 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6617 BB = EmitAtomicBinary(MI, BB, false, 0);
6618 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6619 BB = EmitAtomicBinary(MI, BB, true, 0);
6620
Evan Cheng32e376f2008-07-12 02:23:19 +00006621 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6622 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6623 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6624
6625 unsigned dest = MI->getOperand(0).getReg();
6626 unsigned ptrA = MI->getOperand(1).getReg();
6627 unsigned ptrB = MI->getOperand(2).getReg();
6628 unsigned oldval = MI->getOperand(3).getReg();
6629 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006630 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006631
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006632 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6633 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6634 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006635 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006636 F->insert(It, loop1MBB);
6637 F->insert(It, loop2MBB);
6638 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006639 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006640 exitMBB->splice(exitMBB->begin(), BB,
6641 llvm::next(MachineBasicBlock::iterator(MI)),
6642 BB->end());
6643 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006644
6645 // thisMBB:
6646 // ...
6647 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006648 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006649
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006650 // loop1MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006651 // l[wd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006652 // cmp[wd] dest, oldval
6653 // bne- midMBB
6654 // loop2MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006655 // st[wd]cx. newval, ptr
6656 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006657 // b exitBB
6658 // midMBB:
6659 // st[wd]cx. dest, ptr
6660 // exitBB:
6661 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006662 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00006663 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006664 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00006665 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006666 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006667 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6668 BB->addSuccessor(loop2MBB);
6669 BB->addSuccessor(midMBB);
6670
6671 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006672 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng32e376f2008-07-12 02:23:19 +00006673 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006674 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006675 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006676 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006677 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006678 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006679
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006680 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006681 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006682 .addReg(dest).addReg(ptrA).addReg(ptrB);
6683 BB->addSuccessor(exitMBB);
6684
Evan Cheng32e376f2008-07-12 02:23:19 +00006685 // exitMBB:
6686 // ...
6687 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00006688 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6689 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6690 // We must use 64-bit registers for addresses when targeting 64-bit,
6691 // since we're actually doing arithmetic on them. Other registers
6692 // can be 32-bit.
6693 bool is64bit = PPCSubTarget.isPPC64();
6694 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6695
6696 unsigned dest = MI->getOperand(0).getReg();
6697 unsigned ptrA = MI->getOperand(1).getReg();
6698 unsigned ptrB = MI->getOperand(2).getReg();
6699 unsigned oldval = MI->getOperand(3).getReg();
6700 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006701 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00006702
6703 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6704 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6705 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6706 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6707 F->insert(It, loop1MBB);
6708 F->insert(It, loop2MBB);
6709 F->insert(It, midMBB);
6710 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006711 exitMBB->splice(exitMBB->begin(), BB,
6712 llvm::next(MachineBasicBlock::iterator(MI)),
6713 BB->end());
6714 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006715
6716 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006717 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006718 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6719 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00006720 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6721 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6722 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6723 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6724 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6725 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6726 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6727 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6728 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6729 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6730 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6731 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6732 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6733 unsigned Ptr1Reg;
6734 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00006735 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00006736 // thisMBB:
6737 // ...
6738 // fallthrough --> loopMBB
6739 BB->addSuccessor(loop1MBB);
6740
6741 // The 4-byte load must be aligned, while a char or short may be
6742 // anywhere in the word. Hence all this nasty bookkeeping code.
6743 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6744 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006745 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00006746 // rlwinm ptr, ptr1, 0, 0, 29
6747 // slw newval2, newval, shift
6748 // slw oldval2, oldval,shift
6749 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6750 // slw mask, mask2, shift
6751 // and newval3, newval2, mask
6752 // and oldval3, oldval2, mask
6753 // loop1MBB:
6754 // lwarx tmpDest, ptr
6755 // and tmp, tmpDest, mask
6756 // cmpw tmp, oldval3
6757 // bne- midMBB
6758 // loop2MBB:
6759 // andc tmp2, tmpDest, mask
6760 // or tmp4, tmp2, newval3
6761 // stwcx. tmp4, ptr
6762 // bne- loop1MBB
6763 // b exitBB
6764 // midMBB:
6765 // stwcx. tmpDest, ptr
6766 // exitBB:
6767 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006768 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00006769 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006770 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006771 .addReg(ptrA).addReg(ptrB);
6772 } else {
6773 Ptr1Reg = ptrB;
6774 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006775 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006776 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006777 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006778 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6779 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006780 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006781 .addReg(Ptr1Reg).addImm(0).addImm(61);
6782 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006783 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006784 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006785 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006786 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006787 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006788 .addReg(oldval).addReg(ShiftReg);
6789 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006790 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00006791 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006792 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6793 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6794 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00006795 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006796 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006797 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006798 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006799 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006800 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006801 .addReg(OldVal2Reg).addReg(MaskReg);
6802
6803 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006804 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006805 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006806 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6807 .addReg(TmpDestReg).addReg(MaskReg);
6808 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00006809 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006810 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006811 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6812 BB->addSuccessor(loop2MBB);
6813 BB->addSuccessor(midMBB);
6814
6815 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006816 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6817 .addReg(TmpDestReg).addReg(MaskReg);
6818 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6819 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6820 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006821 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006822 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006823 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006824 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006825 BB->addSuccessor(loop1MBB);
6826 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006827
Dale Johannesen340d2642008-08-30 00:08:53 +00006828 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006829 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006830 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00006831 BB->addSuccessor(exitMBB);
6832
6833 // exitMBB:
6834 // ...
6835 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006836 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
6837 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00006838 } else if (MI->getOpcode() == PPC::FADDrtz) {
6839 // This pseudo performs an FADD with rounding mode temporarily forced
6840 // to round-to-zero. We emit this via custom inserter since the FPSCR
6841 // is not modeled at the SelectionDAG level.
6842 unsigned Dest = MI->getOperand(0).getReg();
6843 unsigned Src1 = MI->getOperand(1).getReg();
6844 unsigned Src2 = MI->getOperand(2).getReg();
6845 DebugLoc dl = MI->getDebugLoc();
6846
6847 MachineRegisterInfo &RegInfo = F->getRegInfo();
6848 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
6849
6850 // Save FPSCR value.
6851 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
6852
6853 // Set rounding mode to round-to-zero.
6854 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
6855 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
6856
6857 // Perform addition.
6858 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
6859
6860 // Restore FPSCR value.
6861 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00006862 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
6863 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
6864 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
6865 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
6866 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
6867 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
6868 PPC::ANDIo8 : PPC::ANDIo;
6869 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
6870 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
6871
6872 MachineRegisterInfo &RegInfo = F->getRegInfo();
6873 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
6874 &PPC::GPRCRegClass :
6875 &PPC::G8RCRegClass);
6876
6877 DebugLoc dl = MI->getDebugLoc();
6878 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
6879 .addReg(MI->getOperand(1).getReg()).addImm(1);
6880 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
6881 MI->getOperand(0).getReg())
6882 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Dale Johannesen340d2642008-08-30 00:08:53 +00006883 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006884 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00006885 }
Chris Lattner9b577f12005-08-26 21:23:58 +00006886
Dan Gohman34396292010-07-06 20:24:04 +00006887 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00006888 return BB;
6889}
6890
Chris Lattner4211ca92006-04-14 06:01:58 +00006891//===----------------------------------------------------------------------===//
6892// Target Optimization Hooks
6893//===----------------------------------------------------------------------===//
6894
Hal Finkelb0c810f2013-04-03 17:44:56 +00006895SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
6896 DAGCombinerInfo &DCI) const {
Hal Finkel2e103312013-04-03 04:01:11 +00006897 if (DCI.isAfterLegalizeVectorOps())
6898 return SDValue();
6899
Hal Finkelb0c810f2013-04-03 17:44:56 +00006900 EVT VT = Op.getValueType();
6901
6902 if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) ||
6903 (VT == MVT::f64 && PPCSubTarget.hasFRE()) ||
6904 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel2e103312013-04-03 04:01:11 +00006905
6906 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6907 // For the reciprocal, we need to find the zero of the function:
6908 // F(X) = A X - 1 [which has a zero at X = 1/A]
6909 // =>
6910 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
6911 // does not require additional intermediate precision]
6912
6913 // Convergence is quadratic, so we essentially double the number of digits
6914 // correct after every iteration. The minimum architected relative
6915 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6916 // 23 digits and double has 52 digits.
6917 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00006918 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00006919 ++Iterations;
6920
6921 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006922 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006923
6924 SDValue FPOne =
Hal Finkelb0c810f2013-04-03 17:44:56 +00006925 DAG.getConstantFP(1.0, VT.getScalarType());
6926 if (VT.isVector()) {
6927 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00006928 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00006929 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel2e103312013-04-03 04:01:11 +00006930 FPOne, FPOne, FPOne, FPOne);
6931 }
6932
Hal Finkelb0c810f2013-04-03 17:44:56 +00006933 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006934 DCI.AddToWorklist(Est.getNode());
6935
6936 // Newton iterations: Est = Est + Est (1 - Arg * Est)
6937 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00006938 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00006939 DCI.AddToWorklist(NewEst.getNode());
6940
Hal Finkelb0c810f2013-04-03 17:44:56 +00006941 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006942 DCI.AddToWorklist(NewEst.getNode());
6943
Hal Finkelb0c810f2013-04-03 17:44:56 +00006944 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006945 DCI.AddToWorklist(NewEst.getNode());
6946
Hal Finkelb0c810f2013-04-03 17:44:56 +00006947 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006948 DCI.AddToWorklist(Est.getNode());
6949 }
6950
6951 return Est;
6952 }
6953
6954 return SDValue();
6955}
6956
Hal Finkelb0c810f2013-04-03 17:44:56 +00006957SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel2e103312013-04-03 04:01:11 +00006958 DAGCombinerInfo &DCI) const {
6959 if (DCI.isAfterLegalizeVectorOps())
6960 return SDValue();
6961
Hal Finkelb0c810f2013-04-03 17:44:56 +00006962 EVT VT = Op.getValueType();
6963
6964 if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) ||
6965 (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) ||
6966 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel2e103312013-04-03 04:01:11 +00006967
6968 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6969 // For the reciprocal sqrt, we need to find the zero of the function:
6970 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
6971 // =>
6972 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
6973 // As a result, we precompute A/2 prior to the iteration loop.
6974
6975 // Convergence is quadratic, so we essentially double the number of digits
6976 // correct after every iteration. The minimum architected relative
6977 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6978 // 23 digits and double has 52 digits.
6979 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00006980 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00006981 ++Iterations;
6982
6983 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006984 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006985
Hal Finkelb0c810f2013-04-03 17:44:56 +00006986 SDValue FPThreeHalves =
6987 DAG.getConstantFP(1.5, VT.getScalarType());
6988 if (VT.isVector()) {
6989 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00006990 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00006991 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
6992 FPThreeHalves, FPThreeHalves,
6993 FPThreeHalves, FPThreeHalves);
Hal Finkel2e103312013-04-03 04:01:11 +00006994 }
6995
Hal Finkelb0c810f2013-04-03 17:44:56 +00006996 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006997 DCI.AddToWorklist(Est.getNode());
6998
6999 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
7000 // this entire sequence requires only one FP constant.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007001 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007002 DCI.AddToWorklist(HalfArg.getNode());
7003
Hal Finkelb0c810f2013-04-03 17:44:56 +00007004 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007005 DCI.AddToWorklist(HalfArg.getNode());
7006
7007 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
7008 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007009 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00007010 DCI.AddToWorklist(NewEst.getNode());
7011
Hal Finkelb0c810f2013-04-03 17:44:56 +00007012 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007013 DCI.AddToWorklist(NewEst.getNode());
7014
Hal Finkelb0c810f2013-04-03 17:44:56 +00007015 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007016 DCI.AddToWorklist(NewEst.getNode());
7017
Hal Finkelb0c810f2013-04-03 17:44:56 +00007018 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007019 DCI.AddToWorklist(Est.getNode());
7020 }
7021
7022 return Est;
7023 }
7024
7025 return SDValue();
7026}
7027
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007028// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
7029// not enforce equality of the chain operands.
7030static bool isConsecutiveLS(LSBaseSDNode *LS, LSBaseSDNode *Base,
7031 unsigned Bytes, int Dist,
7032 SelectionDAG &DAG) {
7033 EVT VT = LS->getMemoryVT();
7034 if (VT.getSizeInBits() / 8 != Bytes)
7035 return false;
7036
7037 SDValue Loc = LS->getBasePtr();
7038 SDValue BaseLoc = Base->getBasePtr();
7039 if (Loc.getOpcode() == ISD::FrameIndex) {
7040 if (BaseLoc.getOpcode() != ISD::FrameIndex)
7041 return false;
7042 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7043 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
7044 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
7045 int FS = MFI->getObjectSize(FI);
7046 int BFS = MFI->getObjectSize(BFI);
7047 if (FS != BFS || FS != (int)Bytes) return false;
7048 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
7049 }
7050
7051 // Handle X+C
7052 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
7053 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
7054 return true;
7055
7056 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7057 const GlobalValue *GV1 = NULL;
7058 const GlobalValue *GV2 = NULL;
7059 int64_t Offset1 = 0;
7060 int64_t Offset2 = 0;
7061 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
7062 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
7063 if (isGA1 && isGA2 && GV1 == GV2)
7064 return Offset1 == (Offset2 + Dist*Bytes);
7065 return false;
7066}
7067
Hal Finkel7d8a6912013-05-26 18:08:30 +00007068// Return true is there is a nearyby consecutive load to the one provided
7069// (regardless of alignment). We search up and down the chain, looking though
7070// token factors and other loads (but nothing else). As a result, a true
7071// results indicates that it is safe to create a new consecutive load adjacent
7072// to the load provided.
7073static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
7074 SDValue Chain = LD->getChain();
7075 EVT VT = LD->getMemoryVT();
7076
7077 SmallSet<SDNode *, 16> LoadRoots;
7078 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
7079 SmallSet<SDNode *, 16> Visited;
7080
7081 // First, search up the chain, branching to follow all token-factor operands.
7082 // If we find a consecutive load, then we're done, otherwise, record all
7083 // nodes just above the top-level loads and token factors.
7084 while (!Queue.empty()) {
7085 SDNode *ChainNext = Queue.pop_back_val();
7086 if (!Visited.insert(ChainNext))
7087 continue;
7088
7089 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007090 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007091 return true;
7092
7093 if (!Visited.count(ChainLD->getChain().getNode()))
7094 Queue.push_back(ChainLD->getChain().getNode());
7095 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
7096 for (SDNode::op_iterator O = ChainNext->op_begin(),
7097 OE = ChainNext->op_end(); O != OE; ++O)
7098 if (!Visited.count(O->getNode()))
7099 Queue.push_back(O->getNode());
7100 } else
7101 LoadRoots.insert(ChainNext);
7102 }
7103
7104 // Second, search down the chain, starting from the top-level nodes recorded
7105 // in the first phase. These top-level nodes are the nodes just above all
7106 // loads and token factors. Starting with their uses, recursively look though
7107 // all loads (just the chain uses) and token factors to find a consecutive
7108 // load.
7109 Visited.clear();
7110 Queue.clear();
7111
7112 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
7113 IE = LoadRoots.end(); I != IE; ++I) {
7114 Queue.push_back(*I);
7115
7116 while (!Queue.empty()) {
7117 SDNode *LoadRoot = Queue.pop_back_val();
7118 if (!Visited.insert(LoadRoot))
7119 continue;
7120
7121 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007122 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007123 return true;
7124
7125 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
7126 UE = LoadRoot->use_end(); UI != UE; ++UI)
7127 if (((isa<LoadSDNode>(*UI) &&
7128 cast<LoadSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
7129 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
7130 Queue.push_back(*UI);
7131 }
7132 }
7133
7134 return false;
7135}
7136
Hal Finkel940ab932014-02-28 00:27:01 +00007137SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
7138 DAGCombinerInfo &DCI) const {
7139 SelectionDAG &DAG = DCI.DAG;
7140 SDLoc dl(N);
7141
7142 assert(PPCSubTarget.useCRBits() &&
7143 "Expecting to be tracking CR bits");
7144 // If we're tracking CR bits, we need to be careful that we don't have:
7145 // trunc(binary-ops(zext(x), zext(y)))
7146 // or
7147 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
7148 // such that we're unnecessarily moving things into GPRs when it would be
7149 // better to keep them in CR bits.
7150
7151 // Note that trunc here can be an actual i1 trunc, or can be the effective
7152 // truncation that comes from a setcc or select_cc.
7153 if (N->getOpcode() == ISD::TRUNCATE &&
7154 N->getValueType(0) != MVT::i1)
7155 return SDValue();
7156
7157 if (N->getOperand(0).getValueType() != MVT::i32 &&
7158 N->getOperand(0).getValueType() != MVT::i64)
7159 return SDValue();
7160
7161 if (N->getOpcode() == ISD::SETCC ||
7162 N->getOpcode() == ISD::SELECT_CC) {
7163 // If we're looking at a comparison, then we need to make sure that the
7164 // high bits (all except for the first) don't matter the result.
7165 ISD::CondCode CC =
7166 cast<CondCodeSDNode>(N->getOperand(
7167 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
7168 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
7169
7170 if (ISD::isSignedIntSetCC(CC)) {
7171 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
7172 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
7173 return SDValue();
7174 } else if (ISD::isUnsignedIntSetCC(CC)) {
7175 if (!DAG.MaskedValueIsZero(N->getOperand(0),
7176 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
7177 !DAG.MaskedValueIsZero(N->getOperand(1),
7178 APInt::getHighBitsSet(OpBits, OpBits-1)))
7179 return SDValue();
7180 } else {
7181 // This is neither a signed nor an unsigned comparison, just make sure
7182 // that the high bits are equal.
7183 APInt Op1Zero, Op1One;
7184 APInt Op2Zero, Op2One;
7185 DAG.ComputeMaskedBits(N->getOperand(0), Op1Zero, Op1One);
7186 DAG.ComputeMaskedBits(N->getOperand(1), Op2Zero, Op2One);
7187
7188 // We don't really care about what is known about the first bit (if
7189 // anything), so clear it in all masks prior to comparing them.
7190 Op1Zero.clearBit(0); Op1One.clearBit(0);
7191 Op2Zero.clearBit(0); Op2One.clearBit(0);
7192
7193 if (Op1Zero != Op2Zero || Op1One != Op2One)
7194 return SDValue();
7195 }
7196 }
7197
7198 // We now know that the higher-order bits are irrelevant, we just need to
7199 // make sure that all of the intermediate operations are bit operations, and
7200 // all inputs are extensions.
7201 if (N->getOperand(0).getOpcode() != ISD::AND &&
7202 N->getOperand(0).getOpcode() != ISD::OR &&
7203 N->getOperand(0).getOpcode() != ISD::XOR &&
7204 N->getOperand(0).getOpcode() != ISD::SELECT &&
7205 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
7206 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
7207 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
7208 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
7209 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
7210 return SDValue();
7211
7212 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
7213 N->getOperand(1).getOpcode() != ISD::AND &&
7214 N->getOperand(1).getOpcode() != ISD::OR &&
7215 N->getOperand(1).getOpcode() != ISD::XOR &&
7216 N->getOperand(1).getOpcode() != ISD::SELECT &&
7217 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
7218 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
7219 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
7220 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
7221 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
7222 return SDValue();
7223
7224 SmallVector<SDValue, 4> Inputs;
7225 SmallVector<SDValue, 8> BinOps, PromOps;
7226 SmallPtrSet<SDNode *, 16> Visited;
7227
7228 for (unsigned i = 0; i < 2; ++i) {
7229 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7230 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7231 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7232 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7233 isa<ConstantSDNode>(N->getOperand(i)))
7234 Inputs.push_back(N->getOperand(i));
7235 else
7236 BinOps.push_back(N->getOperand(i));
7237
7238 if (N->getOpcode() == ISD::TRUNCATE)
7239 break;
7240 }
7241
7242 // Visit all inputs, collect all binary operations (and, or, xor and
7243 // select) that are all fed by extensions.
7244 while (!BinOps.empty()) {
7245 SDValue BinOp = BinOps.back();
7246 BinOps.pop_back();
7247
7248 if (!Visited.insert(BinOp.getNode()))
7249 continue;
7250
7251 PromOps.push_back(BinOp);
7252
7253 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7254 // The condition of the select is not promoted.
7255 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7256 continue;
7257 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7258 continue;
7259
7260 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7261 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7262 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7263 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7264 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7265 Inputs.push_back(BinOp.getOperand(i));
7266 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7267 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7268 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7269 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7270 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
7271 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7272 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7273 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7274 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
7275 BinOps.push_back(BinOp.getOperand(i));
7276 } else {
7277 // We have an input that is not an extension or another binary
7278 // operation; we'll abort this transformation.
7279 return SDValue();
7280 }
7281 }
7282 }
7283
7284 // Make sure that this is a self-contained cluster of operations (which
7285 // is not quite the same thing as saying that everything has only one
7286 // use).
7287 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7288 if (isa<ConstantSDNode>(Inputs[i]))
7289 continue;
7290
7291 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7292 UE = Inputs[i].getNode()->use_end();
7293 UI != UE; ++UI) {
7294 SDNode *User = *UI;
7295 if (User != N && !Visited.count(User))
7296 return SDValue();
7297 }
7298 }
7299
7300 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7301 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7302 UE = PromOps[i].getNode()->use_end();
7303 UI != UE; ++UI) {
7304 SDNode *User = *UI;
7305 if (User != N && !Visited.count(User))
7306 return SDValue();
7307 }
7308 }
7309
7310 // Replace all inputs with the extension operand.
7311 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7312 // Constants may have users outside the cluster of to-be-promoted nodes,
7313 // and so we need to replace those as we do the promotions.
7314 if (isa<ConstantSDNode>(Inputs[i]))
7315 continue;
7316 else
7317 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
7318 }
7319
7320 // Replace all operations (these are all the same, but have a different
7321 // (i1) return type). DAG.getNode will validate that the types of
7322 // a binary operator match, so go through the list in reverse so that
7323 // we've likely promoted both operands first. Any intermediate truncations or
7324 // extensions disappear.
7325 while (!PromOps.empty()) {
7326 SDValue PromOp = PromOps.back();
7327 PromOps.pop_back();
7328
7329 if (PromOp.getOpcode() == ISD::TRUNCATE ||
7330 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
7331 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
7332 PromOp.getOpcode() == ISD::ANY_EXTEND) {
7333 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
7334 PromOp.getOperand(0).getValueType() != MVT::i1) {
7335 // The operand is not yet ready (see comment below).
7336 PromOps.insert(PromOps.begin(), PromOp);
7337 continue;
7338 }
7339
7340 SDValue RepValue = PromOp.getOperand(0);
7341 if (isa<ConstantSDNode>(RepValue))
7342 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
7343
7344 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
7345 continue;
7346 }
7347
7348 unsigned C;
7349 switch (PromOp.getOpcode()) {
7350 default: C = 0; break;
7351 case ISD::SELECT: C = 1; break;
7352 case ISD::SELECT_CC: C = 2; break;
7353 }
7354
7355 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7356 PromOp.getOperand(C).getValueType() != MVT::i1) ||
7357 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7358 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
7359 // The to-be-promoted operands of this node have not yet been
7360 // promoted (this should be rare because we're going through the
7361 // list backward, but if one of the operands has several users in
7362 // this cluster of to-be-promoted nodes, it is possible).
7363 PromOps.insert(PromOps.begin(), PromOp);
7364 continue;
7365 }
7366
7367 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7368 PromOp.getNode()->op_end());
7369
7370 // If there are any constant inputs, make sure they're replaced now.
7371 for (unsigned i = 0; i < 2; ++i)
7372 if (isa<ConstantSDNode>(Ops[C+i]))
7373 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
7374
7375 DAG.ReplaceAllUsesOfValueWith(PromOp,
7376 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1,
7377 Ops.data(), Ops.size()));
7378 }
7379
7380 // Now we're left with the initial truncation itself.
7381 if (N->getOpcode() == ISD::TRUNCATE)
7382 return N->getOperand(0);
7383
7384 // Otherwise, this is a comparison. The operands to be compared have just
7385 // changed type (to i1), but everything else is the same.
7386 return SDValue(N, 0);
7387}
7388
7389SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
7390 DAGCombinerInfo &DCI) const {
7391 SelectionDAG &DAG = DCI.DAG;
7392 SDLoc dl(N);
7393
7394 assert(PPCSubTarget.useCRBits() &&
7395 "Expecting to be tracking CR bits");
7396 // If we're tracking CR bits, we need to be careful that we don't have:
7397 // zext(binary-ops(trunc(x), trunc(y)))
7398 // or
7399 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
7400 // such that we're unnecessarily moving things into CR bits that can more
7401 // efficiently stay in GPRs. Note that if we're not certain that the high
7402 // bits are set as required by the final extension, we still may need to do
7403 // some masking to get the proper behavior.
7404
7405 if (N->getValueType(0) != MVT::i32 &&
7406 N->getValueType(0) != MVT::i64)
7407 return SDValue();
7408
7409 if (N->getOperand(0).getValueType() != MVT::i1)
7410 return SDValue();
7411
7412 if (N->getOperand(0).getOpcode() != ISD::AND &&
7413 N->getOperand(0).getOpcode() != ISD::OR &&
7414 N->getOperand(0).getOpcode() != ISD::XOR &&
7415 N->getOperand(0).getOpcode() != ISD::SELECT &&
7416 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
7417 return SDValue();
7418
7419 SmallVector<SDValue, 4> Inputs;
7420 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
7421 SmallPtrSet<SDNode *, 16> Visited;
7422
7423 // Visit all inputs, collect all binary operations (and, or, xor and
7424 // select) that are all fed by truncations.
7425 while (!BinOps.empty()) {
7426 SDValue BinOp = BinOps.back();
7427 BinOps.pop_back();
7428
7429 if (!Visited.insert(BinOp.getNode()))
7430 continue;
7431
7432 PromOps.push_back(BinOp);
7433
7434 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7435 // The condition of the select is not promoted.
7436 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7437 continue;
7438 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7439 continue;
7440
7441 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7442 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7443 Inputs.push_back(BinOp.getOperand(i));
7444 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7445 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7446 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7447 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7448 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
7449 BinOps.push_back(BinOp.getOperand(i));
7450 } else {
7451 // We have an input that is not a truncation or another binary
7452 // operation; we'll abort this transformation.
7453 return SDValue();
7454 }
7455 }
7456 }
7457
7458 // Make sure that this is a self-contained cluster of operations (which
7459 // is not quite the same thing as saying that everything has only one
7460 // use).
7461 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7462 if (isa<ConstantSDNode>(Inputs[i]))
7463 continue;
7464
7465 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7466 UE = Inputs[i].getNode()->use_end();
7467 UI != UE; ++UI) {
7468 SDNode *User = *UI;
7469 if (User != N && !Visited.count(User))
7470 return SDValue();
7471 }
7472 }
7473
7474 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7475 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7476 UE = PromOps[i].getNode()->use_end();
7477 UI != UE; ++UI) {
7478 SDNode *User = *UI;
7479 if (User != N && !Visited.count(User))
7480 return SDValue();
7481 }
7482 }
7483
7484 bool ReallyNeedsExt = false;
7485 if (N->getOpcode() != ISD::ANY_EXTEND) {
7486 // If all of the inputs are not already sign/zero extended, then
7487 // we'll still need to do that at the end.
7488 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7489 if (isa<ConstantSDNode>(Inputs[i]))
7490 continue;
7491
7492 unsigned OpBits =
7493 Inputs[i].getOperand(0).getValueSizeInBits();
7494 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
7495 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
7496 APInt::getHighBitsSet(OpBits,
7497 OpBits-1))) ||
7498 (N->getOpcode() == ISD::SIGN_EXTEND &&
7499 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) != OpBits)) {
7500 ReallyNeedsExt = true;
7501 break;
7502 }
7503 }
7504 }
7505
7506 // Replace all inputs, either with the truncation operand, or a
7507 // truncation or extension to the final output type.
7508 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7509 // Constant inputs need to be replaced with the to-be-promoted nodes that
7510 // use them because they might have users outside of the cluster of
7511 // promoted nodes.
7512 if (isa<ConstantSDNode>(Inputs[i]))
7513 continue;
7514
7515 SDValue InSrc = Inputs[i].getOperand(0);
7516 if (Inputs[i].getValueType() == N->getValueType(0))
7517 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
7518 else if (N->getOpcode() == ISD::SIGN_EXTEND)
7519 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7520 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
7521 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7522 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7523 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
7524 else
7525 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7526 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
7527 }
7528
7529 // Replace all operations (these are all the same, but have a different
7530 // (promoted) return type). DAG.getNode will validate that the types of
7531 // a binary operator match, so go through the list in reverse so that
7532 // we've likely promoted both operands first.
7533 while (!PromOps.empty()) {
7534 SDValue PromOp = PromOps.back();
7535 PromOps.pop_back();
7536
7537 unsigned C;
7538 switch (PromOp.getOpcode()) {
7539 default: C = 0; break;
7540 case ISD::SELECT: C = 1; break;
7541 case ISD::SELECT_CC: C = 2; break;
7542 }
7543
7544 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7545 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
7546 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7547 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
7548 // The to-be-promoted operands of this node have not yet been
7549 // promoted (this should be rare because we're going through the
7550 // list backward, but if one of the operands has several users in
7551 // this cluster of to-be-promoted nodes, it is possible).
7552 PromOps.insert(PromOps.begin(), PromOp);
7553 continue;
7554 }
7555
7556 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7557 PromOp.getNode()->op_end());
7558
7559 // If this node has constant inputs, then they'll need to be promoted here.
7560 for (unsigned i = 0; i < 2; ++i) {
7561 if (!isa<ConstantSDNode>(Ops[C+i]))
7562 continue;
7563 if (Ops[C+i].getValueType() == N->getValueType(0))
7564 continue;
7565
7566 if (N->getOpcode() == ISD::SIGN_EXTEND)
7567 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7568 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7569 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7570 else
7571 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7572 }
7573
7574 DAG.ReplaceAllUsesOfValueWith(PromOp,
7575 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0),
7576 Ops.data(), Ops.size()));
7577 }
7578
7579 // Now we're left with the initial extension itself.
7580 if (!ReallyNeedsExt)
7581 return N->getOperand(0);
7582
7583 // To zero extend, just mask off everything except for the first bit.
7584 if (N->getOpcode() == ISD::ZERO_EXTEND)
7585 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
7586 DAG.getConstant(1, N->getValueType(0)));
7587
7588 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
7589 "Invalid extension type");
7590 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0));
7591 SDValue ShiftCst =
7592 DAG.getConstant(N->getValueSizeInBits(0)-1, ShiftAmountTy);
7593 return DAG.getNode(ISD::SRA, dl, N->getValueType(0),
7594 DAG.getNode(ISD::SHL, dl, N->getValueType(0),
7595 N->getOperand(0), ShiftCst), ShiftCst);
7596}
7597
Duncan Sandsdc2dac12008-11-24 14:53:14 +00007598SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
7599 DAGCombinerInfo &DCI) const {
Dan Gohman57c732b2010-04-21 01:34:56 +00007600 const TargetMachine &TM = getTargetMachine();
Chris Lattnerf4184352006-03-01 04:57:39 +00007601 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007602 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00007603 switch (N->getOpcode()) {
7604 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00007605 case PPCISD::SHL:
7606 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007607 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007608 return N->getOperand(0);
7609 }
7610 break;
7611 case PPCISD::SRL:
7612 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007613 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007614 return N->getOperand(0);
7615 }
7616 break;
7617 case PPCISD::SRA:
7618 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007619 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007620 C->isAllOnesValue()) // -1 >>s V -> -1.
7621 return N->getOperand(0);
7622 }
7623 break;
Hal Finkel940ab932014-02-28 00:27:01 +00007624 case ISD::SIGN_EXTEND:
7625 case ISD::ZERO_EXTEND:
7626 case ISD::ANY_EXTEND:
7627 return DAGCombineExtBoolTrunc(N, DCI);
7628 case ISD::TRUNCATE:
7629 case ISD::SETCC:
7630 case ISD::SELECT_CC:
7631 return DAGCombineTruncBoolExt(N, DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007632 case ISD::FDIV: {
7633 assert(TM.Options.UnsafeFPMath &&
7634 "Reciprocal estimates require UnsafeFPMath");
Scott Michelcf0da6c2009-02-17 22:15:04 +00007635
Hal Finkel2e103312013-04-03 04:01:11 +00007636 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007637 SDValue RV =
7638 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007639 if (RV.getNode() != 0) {
7640 DCI.AddToWorklist(RV.getNode());
7641 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7642 N->getOperand(0), RV);
7643 }
Hal Finkelf96c18e2013-04-04 22:44:12 +00007644 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
7645 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7646 SDValue RV =
7647 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7648 DCI);
7649 if (RV.getNode() != 0) {
7650 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007651 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007652 N->getValueType(0), RV);
7653 DCI.AddToWorklist(RV.getNode());
7654 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7655 N->getOperand(0), RV);
7656 }
7657 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
7658 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7659 SDValue RV =
7660 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7661 DCI);
7662 if (RV.getNode() != 0) {
7663 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007664 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007665 N->getValueType(0), RV,
7666 N->getOperand(1).getOperand(1));
7667 DCI.AddToWorklist(RV.getNode());
7668 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7669 N->getOperand(0), RV);
7670 }
Hal Finkel2e103312013-04-03 04:01:11 +00007671 }
7672
Hal Finkelb0c810f2013-04-03 17:44:56 +00007673 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007674 if (RV.getNode() != 0) {
7675 DCI.AddToWorklist(RV.getNode());
7676 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7677 N->getOperand(0), RV);
7678 }
7679
7680 }
7681 break;
7682 case ISD::FSQRT: {
7683 assert(TM.Options.UnsafeFPMath &&
7684 "Reciprocal estimates require UnsafeFPMath");
7685
7686 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
7687 // reciprocal sqrt.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007688 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007689 if (RV.getNode() != 0) {
7690 DCI.AddToWorklist(RV.getNode());
Hal Finkelb0c810f2013-04-03 17:44:56 +00007691 RV = DAGCombineFastRecip(RV, DCI);
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007692 if (RV.getNode() != 0) {
7693 // Unfortunately, RV is now NaN if the input was exactly 0. Select out
7694 // this case and force the answer to 0.
7695
7696 EVT VT = RV.getValueType();
7697
7698 SDValue Zero = DAG.getConstantFP(0.0, VT.getScalarType());
7699 if (VT.isVector()) {
7700 assert(VT.getVectorNumElements() == 4 && "Unknown vector type");
7701 Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Zero, Zero, Zero, Zero);
7702 }
7703
7704 SDValue ZeroCmp =
7705 DAG.getSetCC(dl, getSetCCResultType(*DAG.getContext(), VT),
7706 N->getOperand(0), Zero, ISD::SETEQ);
7707 DCI.AddToWorklist(ZeroCmp.getNode());
7708 DCI.AddToWorklist(RV.getNode());
7709
7710 RV = DAG.getNode(VT.isVector() ? ISD::VSELECT : ISD::SELECT, dl, VT,
7711 ZeroCmp, Zero, RV);
Hal Finkel2e103312013-04-03 04:01:11 +00007712 return RV;
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007713 }
Hal Finkel2e103312013-04-03 04:01:11 +00007714 }
7715
7716 }
7717 break;
Chris Lattnerf4184352006-03-01 04:57:39 +00007718 case ISD::SINT_TO_FP:
Chris Lattnera35f3062006-06-16 17:34:12 +00007719 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007720 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
7721 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
7722 // We allow the src/dst to be either f32/f64, but the intermediate
7723 // type must be i64.
Owen Anderson9f944592009-08-11 20:47:22 +00007724 if (N->getOperand(0).getValueType() == MVT::i64 &&
7725 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007726 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007727 if (Val.getValueType() == MVT::f32) {
7728 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007729 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007730 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007731
Owen Anderson9f944592009-08-11 20:47:22 +00007732 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007733 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007734 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007735 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007736 if (N->getValueType(0) == MVT::f32) {
7737 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner72733e52008-01-17 07:00:52 +00007738 DAG.getIntPtrConstant(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007739 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007740 }
7741 return Val;
Owen Anderson9f944592009-08-11 20:47:22 +00007742 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007743 // If the intermediate type is i32, we can avoid the load/store here
7744 // too.
Chris Lattnerf4184352006-03-01 04:57:39 +00007745 }
Chris Lattnerf4184352006-03-01 04:57:39 +00007746 }
7747 }
7748 break;
Chris Lattner27f53452006-03-01 05:50:56 +00007749 case ISD::STORE:
7750 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
7751 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnerf5b46f72008-01-18 16:54:56 +00007752 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00007753 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00007754 N->getOperand(1).getValueType() == MVT::i32 &&
7755 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007756 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007757 if (Val.getValueType() == MVT::f32) {
7758 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007759 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007760 }
Owen Anderson9f944592009-08-11 20:47:22 +00007761 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007762 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007763
Hal Finkel60c75102013-04-01 15:37:53 +00007764 SDValue Ops[] = {
7765 N->getOperand(0), Val, N->getOperand(2),
7766 DAG.getValueType(N->getOperand(1).getValueType())
7767 };
7768
7769 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
7770 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
7771 cast<StoreSDNode>(N)->getMemoryVT(),
7772 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00007773 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007774 return Val;
7775 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007776
Chris Lattnera7976d32006-07-10 20:56:58 +00007777 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00007778 if (cast<StoreSDNode>(N)->isUnindexed() &&
7779 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00007780 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00007781 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00007782 N->getOperand(1).getValueType() == MVT::i16 ||
7783 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00007784 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007785 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007786 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00007787 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00007788 if (BSwapOp.getValueType() == MVT::i16)
7789 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00007790
Dan Gohman48b185d2009-09-25 20:36:54 +00007791 SDValue Ops[] = {
7792 N->getOperand(0), BSwapOp, N->getOperand(2),
7793 DAG.getValueType(N->getOperand(1).getValueType())
7794 };
7795 return
7796 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
7797 Ops, array_lengthof(Ops),
7798 cast<StoreSDNode>(N)->getMemoryVT(),
7799 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00007800 }
7801 break;
Hal Finkelcf2e9082013-05-24 23:00:14 +00007802 case ISD::LOAD: {
7803 LoadSDNode *LD = cast<LoadSDNode>(N);
7804 EVT VT = LD->getValueType(0);
7805 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
7806 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
7807 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
7808 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
Hal Finkel40c34782013-09-15 22:09:58 +00007809 (VT == MVT::v16i8 || VT == MVT::v8i16 ||
7810 VT == MVT::v4i32 || VT == MVT::v4f32) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +00007811 LD->getAlignment() < ABIAlignment) {
7812 // This is a type-legal unaligned Altivec load.
7813 SDValue Chain = LD->getChain();
7814 SDValue Ptr = LD->getBasePtr();
7815
7816 // This implements the loading of unaligned vectors as described in
7817 // the venerable Apple Velocity Engine overview. Specifically:
7818 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
7819 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
7820 //
7821 // The general idea is to expand a sequence of one or more unaligned
7822 // loads into a alignment-based permutation-control instruction (lvsl),
7823 // a series of regular vector loads (which always truncate their
7824 // input address to an aligned address), and a series of permutations.
7825 // The results of these permutations are the requested loaded values.
7826 // The trick is that the last "extra" load is not taken from the address
7827 // you might suspect (sizeof(vector) bytes after the last requested
7828 // load), but rather sizeof(vector) - 1 bytes after the last
7829 // requested vector. The point of this is to avoid a page fault if the
Alp Tokercb402912014-01-24 17:20:08 +00007830 // base address happened to be aligned. This works because if the base
Hal Finkelcf2e9082013-05-24 23:00:14 +00007831 // address is aligned, then adding less than a full vector length will
7832 // cause the last vector in the sequence to be (re)loaded. Otherwise,
7833 // the next vector will be fetched as you might suspect was necessary.
7834
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007835 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00007836 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007837 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
7838 // optimization later.
Hal Finkelcf2e9082013-05-24 23:00:14 +00007839 SDValue PermCntl = BuildIntrinsicOp(Intrinsic::ppc_altivec_lvsl, Ptr,
7840 DAG, dl, MVT::v16i8);
7841
7842 // Refine the alignment of the original load (a "new" load created here
7843 // which was identical to the first except for the alignment would be
7844 // merged with the existing node regardless).
7845 MachineFunction &MF = DAG.getMachineFunction();
7846 MachineMemOperand *MMO =
7847 MF.getMachineMemOperand(LD->getPointerInfo(),
7848 LD->getMemOperand()->getFlags(),
7849 LD->getMemoryVT().getStoreSize(),
7850 ABIAlignment);
7851 LD->refineAlignment(MMO);
7852 SDValue BaseLoad = SDValue(LD, 0);
7853
7854 // Note that the value of IncOffset (which is provided to the next
7855 // load's pointer info offset value, and thus used to calculate the
7856 // alignment), and the value of IncValue (which is actually used to
7857 // increment the pointer value) are different! This is because we
7858 // require the next load to appear to be aligned, even though it
7859 // is actually offset from the base pointer by a lesser amount.
7860 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +00007861 int IncValue = IncOffset;
7862
7863 // Walk (both up and down) the chain looking for another load at the real
7864 // (aligned) offset (the alignment of the other load does not matter in
7865 // this case). If found, then do not use the offset reduction trick, as
7866 // that will prevent the loads from being later combined (as they would
7867 // otherwise be duplicates).
7868 if (!findConsecutiveLoad(LD, DAG))
7869 --IncValue;
7870
Hal Finkelcf2e9082013-05-24 23:00:14 +00007871 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
7872 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
7873
Hal Finkelcf2e9082013-05-24 23:00:14 +00007874 SDValue ExtraLoad =
7875 DAG.getLoad(VT, dl, Chain, Ptr,
7876 LD->getPointerInfo().getWithOffset(IncOffset),
7877 LD->isVolatile(), LD->isNonTemporal(),
7878 LD->isInvariant(), ABIAlignment);
7879
7880 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7881 BaseLoad.getValue(1), ExtraLoad.getValue(1));
7882
7883 if (BaseLoad.getValueType() != MVT::v4i32)
7884 BaseLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, BaseLoad);
7885
7886 if (ExtraLoad.getValueType() != MVT::v4i32)
7887 ExtraLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ExtraLoad);
7888
7889 SDValue Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
7890 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
7891
7892 if (VT != MVT::v4i32)
7893 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
7894
7895 // Now we need to be really careful about how we update the users of the
7896 // original load. We cannot just call DCI.CombineTo (or
7897 // DAG.ReplaceAllUsesWith for that matter), because the load still has
7898 // uses created here (the permutation for example) that need to stay.
7899 SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
7900 while (UI != UE) {
7901 SDUse &Use = UI.getUse();
7902 SDNode *User = *UI;
7903 // Note: BaseLoad is checked here because it might not be N, but a
7904 // bitcast of N.
7905 if (User == Perm.getNode() || User == BaseLoad.getNode() ||
7906 User == TF.getNode() || Use.getResNo() > 1) {
7907 ++UI;
7908 continue;
7909 }
7910
7911 SDValue To = Use.getResNo() ? TF : Perm;
7912 ++UI;
7913
7914 SmallVector<SDValue, 8> Ops;
7915 for (SDNode::op_iterator O = User->op_begin(),
7916 OE = User->op_end(); O != OE; ++O) {
7917 if (*O == Use)
7918 Ops.push_back(To);
7919 else
7920 Ops.push_back(*O);
7921 }
7922
7923 DAG.UpdateNodeOperands(User, Ops.data(), Ops.size());
7924 }
7925
7926 return SDValue(N, 0);
7927 }
7928 }
7929 break;
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007930 case ISD::INTRINSIC_WO_CHAIN:
7931 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() ==
7932 Intrinsic::ppc_altivec_lvsl &&
7933 N->getOperand(1)->getOpcode() == ISD::ADD) {
7934 SDValue Add = N->getOperand(1);
7935
7936 if (DAG.MaskedValueIsZero(Add->getOperand(1),
7937 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
7938 Add.getValueType().getScalarType().getSizeInBits()))) {
7939 SDNode *BasePtr = Add->getOperand(0).getNode();
7940 for (SDNode::use_iterator UI = BasePtr->use_begin(),
7941 UE = BasePtr->use_end(); UI != UE; ++UI) {
7942 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
7943 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
7944 Intrinsic::ppc_altivec_lvsl) {
7945 // We've found another LVSL, and this address if an aligned
7946 // multiple of that one. The results will be the same, so use the
7947 // one we've just found instead.
7948
7949 return SDValue(*UI, 0);
7950 }
7951 }
7952 }
7953 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +00007954
7955 break;
Chris Lattnera7976d32006-07-10 20:56:58 +00007956 case ISD::BSWAP:
7957 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +00007958 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +00007959 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007960 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
7961 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00007962 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007963 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007964 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00007965 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +00007966 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007967 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +00007968 LD->getChain(), // Chain
7969 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007970 DAG.getValueType(N->getValueType(0)) // VT
7971 };
Dan Gohman48b185d2009-09-25 20:36:54 +00007972 SDValue BSLoad =
7973 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +00007974 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
7975 MVT::i64 : MVT::i32, MVT::Other),
Hal Finkel93492fa2013-03-28 19:43:12 +00007976 Ops, 3, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00007977
Scott Michelcf0da6c2009-02-17 22:15:04 +00007978 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007979 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +00007980 if (N->getValueType(0) == MVT::i16)
7981 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007982
Chris Lattnera7976d32006-07-10 20:56:58 +00007983 // First, combine the bswap away. This makes the value produced by the
7984 // load dead.
7985 DCI.CombineTo(N, ResVal);
7986
7987 // Next, combine the load away, we give it a bogus result value but a real
7988 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +00007989 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00007990
Chris Lattnera7976d32006-07-10 20:56:58 +00007991 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007992 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +00007993 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007994
Chris Lattner27f53452006-03-01 05:50:56 +00007995 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +00007996 case PPCISD::VCMP: {
7997 // If a VCMPo node already exists with exactly the same operands as this
7998 // node, use its result instead of this node (VCMPo computes both a CR6 and
7999 // a normal output).
8000 //
8001 if (!N->getOperand(0).hasOneUse() &&
8002 !N->getOperand(1).hasOneUse() &&
8003 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00008004
Chris Lattnerd4058a52006-03-31 06:02:07 +00008005 // Scan all of the users of the LHS, looking for VCMPo's that match.
8006 SDNode *VCMPoNode = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008007
Gabor Greiff304a7a2008-08-28 21:40:38 +00008008 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +00008009 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
8010 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008011 if (UI->getOpcode() == PPCISD::VCMPo &&
8012 UI->getOperand(1) == N->getOperand(1) &&
8013 UI->getOperand(2) == N->getOperand(2) &&
8014 UI->getOperand(0) == N->getOperand(0)) {
8015 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008016 break;
8017 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008018
Chris Lattner518834c2006-04-18 18:28:22 +00008019 // If there is no VCMPo node, or if the flag value has a single use, don't
8020 // transform this.
8021 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
8022 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008023
8024 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +00008025 // chain, this transformation is more complex. Note that multiple things
8026 // could use the value result, which we should ignore.
8027 SDNode *FlagUser = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008028 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner518834c2006-04-18 18:28:22 +00008029 FlagUser == 0; ++UI) {
8030 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008031 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +00008032 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008033 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +00008034 FlagUser = User;
8035 break;
8036 }
8037 }
8038 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008039
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008040 // If the user is a MFOCRF instruction, we know this is safe.
8041 // Otherwise we give up for right now.
8042 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008043 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +00008044 }
8045 break;
8046 }
Hal Finkel940ab932014-02-28 00:27:01 +00008047 case ISD::BRCOND: {
8048 SDValue Cond = N->getOperand(1);
8049 SDValue Target = N->getOperand(2);
8050
8051 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8052 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
8053 Intrinsic::ppc_is_decremented_ctr_nonzero) {
8054
8055 // We now need to make the intrinsic dead (it cannot be instruction
8056 // selected).
8057 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
8058 assert(Cond.getNode()->hasOneUse() &&
8059 "Counter decrement has more than one use");
8060
8061 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
8062 N->getOperand(0), Target);
8063 }
8064 }
8065 break;
Chris Lattner9754d142006-04-18 17:59:36 +00008066 case ISD::BR_CC: {
8067 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008068 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +00008069 // lowering is done pre-legalize, because the legalizer lowers the predicate
8070 // compare down to code that is difficult to reassemble.
8071 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008072 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +00008073
8074 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
8075 // value. If so, pass-through the AND to get to the intrinsic.
8076 if (LHS.getOpcode() == ISD::AND &&
8077 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8078 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
8079 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8080 isa<ConstantSDNode>(LHS.getOperand(1)) &&
8081 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
8082 isZero())
8083 LHS = LHS.getOperand(0);
8084
8085 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8086 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
8087 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8088 isa<ConstantSDNode>(RHS)) {
8089 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
8090 "Counter decrement comparison is not EQ or NE");
8091
8092 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
8093 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
8094 (CC == ISD::SETNE && !Val);
8095
8096 // We now need to make the intrinsic dead (it cannot be instruction
8097 // selected).
8098 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
8099 assert(LHS.getNode()->hasOneUse() &&
8100 "Counter decrement has more than one use");
8101
8102 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
8103 N->getOperand(0), N->getOperand(4));
8104 }
8105
Chris Lattner9754d142006-04-18 17:59:36 +00008106 int CompareOpc;
8107 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008108
Chris Lattner9754d142006-04-18 17:59:36 +00008109 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8110 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
8111 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
8112 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00008113
Chris Lattner9754d142006-04-18 17:59:36 +00008114 // If this is a comparison against something other than 0/1, then we know
8115 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008116 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00008117 if (Val != 0 && Val != 1) {
8118 if (CC == ISD::SETEQ) // Cond never true, remove branch.
8119 return N->getOperand(0);
8120 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +00008121 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +00008122 N->getOperand(0), N->getOperand(4));
8123 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008124
Chris Lattner9754d142006-04-18 17:59:36 +00008125 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008126
Chris Lattner9754d142006-04-18 17:59:36 +00008127 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008128 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008129 LHS.getOperand(2), // LHS of compare
8130 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +00008131 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008132 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00008133 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00008134 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008135
Chris Lattner9754d142006-04-18 17:59:36 +00008136 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008137 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +00008138 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +00008139 default: // Can't happen, don't crash on invalid number though.
8140 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008141 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +00008142 break;
8143 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008144 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +00008145 break;
8146 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008147 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +00008148 break;
8149 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008150 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +00008151 break;
8152 }
8153
Owen Anderson9f944592009-08-11 20:47:22 +00008154 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
8155 DAG.getConstant(CompOpc, MVT::i32),
8156 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +00008157 N->getOperand(4), CompNode.getValue(1));
8158 }
8159 break;
8160 }
Chris Lattnerf4184352006-03-01 04:57:39 +00008161 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008162
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008163 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +00008164}
8165
Chris Lattner4211ca92006-04-14 06:01:58 +00008166//===----------------------------------------------------------------------===//
8167// Inline Assembly Support
8168//===----------------------------------------------------------------------===//
8169
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008170void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Scott Michelcf0da6c2009-02-17 22:15:04 +00008171 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +00008172 APInt &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +00008173 const SelectionDAG &DAG,
Chris Lattnerc5287c02006-04-02 06:26:07 +00008174 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00008175 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +00008176 switch (Op.getOpcode()) {
8177 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008178 case PPCISD::LBRX: {
8179 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +00008180 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +00008181 KnownZero = 0xFFFF0000;
8182 break;
8183 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008184 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00008185 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +00008186 default: break;
8187 case Intrinsic::ppc_altivec_vcmpbfp_p:
8188 case Intrinsic::ppc_altivec_vcmpeqfp_p:
8189 case Intrinsic::ppc_altivec_vcmpequb_p:
8190 case Intrinsic::ppc_altivec_vcmpequh_p:
8191 case Intrinsic::ppc_altivec_vcmpequw_p:
8192 case Intrinsic::ppc_altivec_vcmpgefp_p:
8193 case Intrinsic::ppc_altivec_vcmpgtfp_p:
8194 case Intrinsic::ppc_altivec_vcmpgtsb_p:
8195 case Intrinsic::ppc_altivec_vcmpgtsh_p:
8196 case Intrinsic::ppc_altivec_vcmpgtsw_p:
8197 case Intrinsic::ppc_altivec_vcmpgtub_p:
8198 case Intrinsic::ppc_altivec_vcmpgtuh_p:
8199 case Intrinsic::ppc_altivec_vcmpgtuw_p:
8200 KnownZero = ~1U; // All bits but the low one are known to be zero.
8201 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008202 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008203 }
8204 }
8205}
8206
8207
Chris Lattnerd6855142007-03-25 02:14:49 +00008208/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +00008209/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008210PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00008211PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
8212 if (Constraint.size() == 1) {
8213 switch (Constraint[0]) {
8214 default: break;
8215 case 'b':
8216 case 'r':
8217 case 'f':
8218 case 'v':
8219 case 'y':
8220 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +00008221 case 'Z':
8222 // FIXME: While Z does indicate a memory constraint, it specifically
8223 // indicates an r+r address (used in conjunction with the 'y' modifier
8224 // in the replacement string). Currently, we're forcing the base
8225 // register to be r0 in the asm printer (which is interpreted as zero)
8226 // and forming the complete address in the second register. This is
8227 // suboptimal.
8228 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +00008229 }
8230 }
8231 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +00008232}
8233
John Thompsone8360b72010-10-29 17:29:13 +00008234/// Examine constraint type and operand type and determine a weight value.
8235/// This object must already have been set up with the operand type
8236/// and the current alternative constraint selected.
8237TargetLowering::ConstraintWeight
8238PPCTargetLowering::getSingleConstraintMatchWeight(
8239 AsmOperandInfo &info, const char *constraint) const {
8240 ConstraintWeight weight = CW_Invalid;
8241 Value *CallOperandVal = info.CallOperandVal;
8242 // If we don't have a value, we can't do a match,
8243 // but allow it at the lowest weight.
8244 if (CallOperandVal == NULL)
8245 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00008246 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00008247 // Look at the constraint type.
8248 switch (*constraint) {
8249 default:
8250 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8251 break;
8252 case 'b':
8253 if (type->isIntegerTy())
8254 weight = CW_Register;
8255 break;
8256 case 'f':
8257 if (type->isFloatTy())
8258 weight = CW_Register;
8259 break;
8260 case 'd':
8261 if (type->isDoubleTy())
8262 weight = CW_Register;
8263 break;
8264 case 'v':
8265 if (type->isVectorTy())
8266 weight = CW_Register;
8267 break;
8268 case 'y':
8269 weight = CW_Register;
8270 break;
Hal Finkel4f24c622012-11-05 18:18:42 +00008271 case 'Z':
8272 weight = CW_Memory;
8273 break;
John Thompsone8360b72010-10-29 17:29:13 +00008274 }
8275 return weight;
8276}
8277
Scott Michelcf0da6c2009-02-17 22:15:04 +00008278std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner584a11a2006-11-02 01:44:04 +00008279PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00008280 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +00008281 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +00008282 // GCC RS6000 Constraint Letters
8283 switch (Constraint[0]) {
8284 case 'b': // R1-R31
Hal Finkel638a9fa2013-03-19 18:51:05 +00008285 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
8286 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
8287 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008288 case 'r': // R0-R31
Owen Anderson9f944592009-08-11 20:47:22 +00008289 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +00008290 return std::make_pair(0U, &PPC::G8RCRegClass);
8291 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008292 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008293 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +00008294 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008295 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +00008296 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008297 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008298 case 'v':
Craig Topperabadc662012-04-20 06:31:50 +00008299 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008300 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +00008301 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008302 }
8303 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008304
Hal Finkelb176acb2013-08-03 12:25:10 +00008305 std::pair<unsigned, const TargetRegisterClass*> R =
8306 TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8307
8308 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
8309 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
8310 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
8311 // register.
8312 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
8313 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
8314 if (R.first && VT == MVT::i64 && PPCSubTarget.isPPC64() &&
8315 PPC::GPRCRegClass.contains(R.first)) {
8316 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
8317 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +00008318 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +00008319 &PPC::G8RCRegClass);
8320 }
8321
8322 return R;
Chris Lattner01513612006-01-31 19:20:21 +00008323}
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008324
Chris Lattner584a11a2006-11-02 01:44:04 +00008325
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008326/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +00008327/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +00008328void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00008329 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008330 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00008331 SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008332 SDValue Result(0,0);
Eric Christopher0713a9d2011-06-08 23:55:35 +00008333
Eric Christopherde9399b2011-06-02 23:16:42 +00008334 // Only support length 1 constraints.
8335 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008336
Eric Christopherde9399b2011-06-02 23:16:42 +00008337 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008338 switch (Letter) {
8339 default: break;
8340 case 'I':
8341 case 'J':
8342 case 'K':
8343 case 'L':
8344 case 'M':
8345 case 'N':
8346 case 'O':
8347 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +00008348 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008349 if (!CST) return; // Must be an immediate to match.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008350 unsigned Value = CST->getZExtValue();
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008351 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008352 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008353 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008354 if ((short)Value == (int)Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008355 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008356 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008357 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
8358 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008359 if ((short)Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008360 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008361 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008362 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008363 if ((Value >> 16) == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008364 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008365 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008366 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008367 if (Value > 31)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008368 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008369 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008370 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008371 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008372 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008373 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008374 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008375 if (Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008376 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008377 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008378 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008379 if ((short)-Value == (int)-Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008380 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008381 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008382 }
8383 break;
8384 }
8385 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008386
Gabor Greiff304a7a2008-08-28 21:40:38 +00008387 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008388 Ops.push_back(Result);
8389 return;
8390 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008391
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008392 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +00008393 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008394}
Evan Cheng2dd2c652006-03-13 23:20:37 +00008395
Chris Lattner1eb94d92007-03-30 23:15:24 +00008396// isLegalAddressingMode - Return true if the addressing mode represented
8397// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008398bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00008399 Type *Ty) const {
Chris Lattner1eb94d92007-03-30 23:15:24 +00008400 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelcf0da6c2009-02-17 22:15:04 +00008401
Chris Lattner1eb94d92007-03-30 23:15:24 +00008402 // PPC allows a sign-extended 16-bit immediate field.
8403 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
8404 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008405
Chris Lattner1eb94d92007-03-30 23:15:24 +00008406 // No global is ever allowed as a base.
8407 if (AM.BaseGV)
8408 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008409
8410 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +00008411 switch (AM.Scale) {
8412 case 0: // "r+i" or just "i", depending on HasBaseReg.
8413 break;
8414 case 1:
8415 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
8416 return false;
8417 // Otherwise we have r+r or r+i.
8418 break;
8419 case 2:
8420 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
8421 return false;
8422 // Allow 2*r as r+r.
8423 break;
Chris Lattner19ccd622007-04-09 22:10:05 +00008424 default:
8425 // No other scales are supported.
8426 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +00008427 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008428
Chris Lattner1eb94d92007-03-30 23:15:24 +00008429 return true;
8430}
8431
Dan Gohman21cea8a2010-04-17 15:26:15 +00008432SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
8433 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00008434 MachineFunction &MF = DAG.getMachineFunction();
8435 MachineFrameInfo *MFI = MF.getFrameInfo();
8436 MFI->setReturnAddressIsTaken(true);
8437
Bill Wendling908bf812014-01-06 00:43:20 +00008438 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008439 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008440
Andrew Trickef9de2a2013-05-25 02:42:55 +00008441 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008442 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +00008443
Dale Johannesen81bfca72010-05-03 22:59:34 +00008444 // Make sure the function does not optimize away the store of the RA to
8445 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +00008446 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008447 FuncInfo->setLRStoreRequired();
8448 bool isPPC64 = PPCSubTarget.isPPC64();
8449 bool isDarwinABI = PPCSubTarget.isDarwinABI();
8450
8451 if (Depth > 0) {
8452 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8453 SDValue Offset =
Wesley Peck527da1b2010-11-23 03:31:01 +00008454
Anton Korobeynikov2f931282011-01-10 12:39:04 +00008455 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen81bfca72010-05-03 22:59:34 +00008456 isPPC64? MVT::i64 : MVT::i32);
8457 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
8458 DAG.getNode(ISD::ADD, dl, getPointerTy(),
8459 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008460 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008461 }
Chris Lattnerf6a81562007-12-08 06:59:59 +00008462
Chris Lattnerf6a81562007-12-08 06:59:59 +00008463 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008464 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008465 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008466 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +00008467}
8468
Dan Gohman21cea8a2010-04-17 15:26:15 +00008469SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
8470 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008471 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008472 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00008473
Owen Anderson53aa7a92009-08-10 22:56:29 +00008474 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00008475 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008476
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008477 MachineFunction &MF = DAG.getMachineFunction();
8478 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008479 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +00008480
8481 // Naked functions never have a frame pointer, and so we use r1. For all
8482 // other functions, this decision must be delayed until during PEI.
8483 unsigned FrameReg;
8484 if (MF.getFunction()->getAttributes().hasAttribute(
8485 AttributeSet::FunctionIndex, Attribute::Naked))
8486 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
8487 else
8488 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
8489
Dale Johannesen81bfca72010-05-03 22:59:34 +00008490 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
8491 PtrVT);
8492 while (Depth--)
8493 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008494 FrameAddr, MachinePointerInfo(), false, false,
8495 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008496 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008497}
Dan Gohmanc14e5222008-10-21 03:41:46 +00008498
8499bool
8500PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8501 // The PowerPC target isn't yet aware of offsets.
8502 return false;
8503}
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008504
Evan Chengd9929f02010-04-01 20:10:42 +00008505/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +00008506/// and store operations as a result of memset, memcpy, and memmove
8507/// lowering. If DstAlign is zero that means it's safe to destination
8508/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
8509/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +00008510/// probably because the source does not need to be loaded. If 'IsMemset' is
8511/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
8512/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
8513/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +00008514/// It returns EVT::Other if the type should be determined using generic
8515/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +00008516EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
8517 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00008518 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00008519 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +00008520 MachineFunction &MF) const {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008521 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +00008522 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008523 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00008524 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008525 }
8526}
Hal Finkel88ed4e32012-04-01 19:23:08 +00008527
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008528bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +00008529 unsigned,
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008530 bool *Fast) const {
8531 if (DisablePPCUnaligned)
8532 return false;
8533
8534 // PowerPC supports unaligned memory access for simple non-vector types.
8535 // Although accessing unaligned addresses is not as efficient as accessing
8536 // aligned addresses, it is generally more efficient than manual expansion,
8537 // and generally only traps for software emulation when crossing page
8538 // boundaries.
8539
8540 if (!VT.isSimple())
8541 return false;
8542
8543 if (VT.getSimpleVT().isVector())
8544 return false;
8545
8546 if (VT == MVT::ppcf128)
8547 return false;
8548
8549 if (Fast)
8550 *Fast = true;
8551
8552 return true;
8553}
8554
Stephen Lin73de7bf2013-07-09 18:16:56 +00008555bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
8556 VT = VT.getScalarType();
8557
Hal Finkel0a479ae2012-06-22 00:49:52 +00008558 if (!VT.isSimple())
8559 return false;
8560
8561 switch (VT.getSimpleVT().SimpleTy) {
8562 case MVT::f32:
8563 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +00008564 return true;
8565 default:
8566 break;
8567 }
8568
8569 return false;
8570}
8571
Hal Finkel88ed4e32012-04-01 19:23:08 +00008572Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel21442b22013-09-11 23:05:25 +00008573 if (DisableILPPref || PPCSubTarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008574 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +00008575
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008576 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +00008577}
8578
Bill Schmidt0cf702f2013-07-30 00:50:39 +00008579// Create a fast isel object.
8580FastISel *
8581PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
8582 const TargetLibraryInfo *LibInfo) const {
8583 return PPC::createFastISel(FuncInfo, LibInfo);
8584}