blob: 7dc47be0d510b71015fe7bd2343a0ba759d5cefc [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 Finkel46043ed2014-03-01 21:36:57 +0000566 setTargetDAGCombine(ISD::SIGN_EXTEND);
567 setTargetDAGCombine(ISD::ZERO_EXTEND);
568 setTargetDAGCombine(ISD::ANY_EXTEND);
569
Hal Finkel940ab932014-02-28 00:27:01 +0000570 if (Subtarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000571 setTargetDAGCombine(ISD::TRUNCATE);
572 setTargetDAGCombine(ISD::SETCC);
573 setTargetDAGCombine(ISD::SELECT_CC);
574 }
575
Hal Finkel2e103312013-04-03 04:01:11 +0000576 // Use reciprocal estimates.
577 if (TM.Options.UnsafeFPMath) {
578 setTargetDAGCombine(ISD::FDIV);
579 setTargetDAGCombine(ISD::FSQRT);
580 }
581
Dale Johannesen10432e52007-10-19 00:59:18 +0000582 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng39e90022012-07-02 22:39:56 +0000583 if (Subtarget->isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000584 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000585 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
586 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000587 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
588 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000589 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
590 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
591 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
592 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
593 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000594 }
595
Hal Finkel940ab932014-02-28 00:27:01 +0000596 // With 32 condition bits, we don't need to sink (and duplicate) compares
597 // aggressively in CodeGenPrep.
598 if (Subtarget->useCRBits())
599 setHasMultipleConditionRegisters();
600
Hal Finkel65298572011-10-17 18:53:03 +0000601 setMinFunctionAlignment(2);
602 if (PPCSubTarget.isDarwin())
603 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000604
Evan Cheng39e90022012-07-02 22:39:56 +0000605 if (isPPC64 && Subtarget->isJITCodeModel())
606 // Temporary workaround for the inability of PPC64 JIT to handle jump
607 // tables.
608 setSupportJumpTables(false);
609
Eli Friedman30a49e92011-08-03 21:06:02 +0000610 setInsertFencesForAtomic(true);
611
Hal Finkel21442b22013-09-11 23:05:25 +0000612 if (Subtarget->enableMachineScheduler())
613 setSchedulingPreference(Sched::Source);
614 else
615 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000616
Chris Lattnerf22556d2005-08-16 17:14:42 +0000617 computeRegisterProperties();
Hal Finkel742b5352012-08-28 16:12:39 +0000618
619 // The Freescale cores does better with aggressive inlining of memcpy and
620 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
621 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
622 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000623 MaxStoresPerMemset = 32;
624 MaxStoresPerMemsetOptSize = 16;
625 MaxStoresPerMemcpy = 32;
626 MaxStoresPerMemcpyOptSize = 8;
627 MaxStoresPerMemmove = 32;
628 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel742b5352012-08-28 16:12:39 +0000629
630 setPrefFunctionAlignment(4);
Hal Finkel742b5352012-08-28 16:12:39 +0000631 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000632}
633
Hal Finkel262a2242013-09-12 23:20:06 +0000634/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
635/// the desired ByVal argument alignment.
636static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
637 unsigned MaxMaxAlign) {
638 if (MaxAlign == MaxMaxAlign)
639 return;
640 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
641 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
642 MaxAlign = 32;
643 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
644 MaxAlign = 16;
645 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
646 unsigned EltAlign = 0;
647 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
648 if (EltAlign > MaxAlign)
649 MaxAlign = EltAlign;
650 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
651 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
652 unsigned EltAlign = 0;
653 getMaxByValAlign(STy->getElementType(i), EltAlign, MaxMaxAlign);
654 if (EltAlign > MaxAlign)
655 MaxAlign = EltAlign;
656 if (MaxAlign == MaxMaxAlign)
657 break;
658 }
659 }
660}
661
Dale Johannesencbde4c22008-02-28 22:31:51 +0000662/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
663/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000664unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000665 // Darwin passes everything on 4 byte boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000666 if (PPCSubTarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000667 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000668
669 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000670 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000671 unsigned Align = PPCSubTarget.isPPC64() ? 8 : 4;
672 if (PPCSubTarget.hasAltivec() || PPCSubTarget.hasQPX())
673 getMaxByValAlign(Ty, Align, PPCSubTarget.hasQPX() ? 32 : 16);
674 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000675}
676
Chris Lattner347ed8a2006-01-09 23:52:17 +0000677const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
678 switch (Opcode) {
679 default: return 0;
Evan Cheng32e376f2008-07-12 02:23:19 +0000680 case PPCISD::FSEL: return "PPCISD::FSEL";
681 case PPCISD::FCFID: return "PPCISD::FCFID";
682 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
683 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000684 case PPCISD::FRE: return "PPCISD::FRE";
685 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000686 case PPCISD::STFIWX: return "PPCISD::STFIWX";
687 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
688 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
689 case PPCISD::VPERM: return "PPCISD::VPERM";
690 case PPCISD::Hi: return "PPCISD::Hi";
691 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000692 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller79fef932009-12-18 13:00:15 +0000693 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
694 case PPCISD::LOAD: return "PPCISD::LOAD";
695 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000696 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
697 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
698 case PPCISD::SRL: return "PPCISD::SRL";
699 case PPCISD::SRA: return "PPCISD::SRA";
700 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000701 case PPCISD::CALL: return "PPCISD::CALL";
702 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000703 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000704 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng32e376f2008-07-12 02:23:19 +0000705 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel756810f2013-03-21 21:37:52 +0000706 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
707 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000708 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng32e376f2008-07-12 02:23:19 +0000709 case PPCISD::VCMP: return "PPCISD::VCMP";
710 case PPCISD::VCMPo: return "PPCISD::VCMPo";
711 case PPCISD::LBRX: return "PPCISD::LBRX";
712 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng32e376f2008-07-12 02:23:19 +0000713 case PPCISD::LARX: return "PPCISD::LARX";
714 case PPCISD::STCX: return "PPCISD::STCX";
715 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +0000716 case PPCISD::BDNZ: return "PPCISD::BDNZ";
717 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000718 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000719 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000720 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +0000721 case PPCISD::CR6SET: return "PPCISD::CR6SET";
722 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34627e32012-11-27 17:35:46 +0000723 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
724 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
725 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Roman Divacky32143e22013-12-20 18:08:54 +0000726 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000727 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
728 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000729 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000730 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
731 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
732 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000733 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
734 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
735 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
736 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
737 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +0000738 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +0000739 case PPCISD::SC: return "PPCISD::SC";
Chris Lattner347ed8a2006-01-09 23:52:17 +0000740 }
741}
742
Matt Arsenault758659232013-05-18 00:21:46 +0000743EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000744 if (!VT.isVector())
Hal Finkel940ab932014-02-28 00:27:01 +0000745 return PPCSubTarget.useCRBits() ? MVT::i1 : MVT::i32;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000746 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000747}
748
Chris Lattner4211ca92006-04-14 06:01:58 +0000749//===----------------------------------------------------------------------===//
750// Node matching predicates, for use by the tblgen matching code.
751//===----------------------------------------------------------------------===//
752
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000753/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000754static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000755 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000756 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000757 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000758 // Maybe this has already been legalized into the constant pool?
759 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000760 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000761 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000762 }
763 return false;
764}
765
Chris Lattnere8b83b42006-04-06 17:23:16 +0000766/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
767/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000768static bool isConstantOrUndef(int Op, int Val) {
769 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000770}
771
772/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
773/// VPKUHUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000774bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000775 if (!isUnary) {
776 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000777 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000778 return false;
779 } else {
780 for (unsigned i = 0; i != 8; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000781 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
782 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000783 return false;
784 }
Chris Lattner1d338192006-04-06 18:26:28 +0000785 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000786}
787
788/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
789/// VPKUWUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000790bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000791 if (!isUnary) {
792 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000793 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
794 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000795 return false;
796 } else {
797 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000798 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
799 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
800 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
801 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000802 return false;
803 }
Chris Lattner1d338192006-04-06 18:26:28 +0000804 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000805}
806
Chris Lattnerf38e0332006-04-06 22:02:42 +0000807/// isVMerge - Common function, used to match vmrg* shuffles.
808///
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000809static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +0000810 unsigned LHSStart, unsigned RHSStart) {
Owen Anderson9f944592009-08-11 20:47:22 +0000811 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000812 "PPC only supports shuffles by bytes!");
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000813 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
814 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000815
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000816 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
817 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000818 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000819 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000820 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000821 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000822 return false;
823 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000824 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +0000825}
826
827/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
828/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000829bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000830 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000831 if (!isUnary)
832 return isVMerge(N, UnitSize, 8, 24);
833 return isVMerge(N, UnitSize, 8, 8);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000834}
835
836/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
837/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000838bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000839 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000840 if (!isUnary)
841 return isVMerge(N, UnitSize, 0, 16);
842 return isVMerge(N, UnitSize, 0, 0);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000843}
844
845
Chris Lattner1d338192006-04-06 18:26:28 +0000846/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
847/// amount, otherwise return -1.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000848int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Owen Anderson9f944592009-08-11 20:47:22 +0000849 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000850 "PPC only supports shuffles by bytes!");
851
852 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +0000853
Chris Lattner1d338192006-04-06 18:26:28 +0000854 // Find the first non-undef value in the shuffle mask.
855 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000856 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +0000857 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000858
Chris Lattner1d338192006-04-06 18:26:28 +0000859 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000860
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000861 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +0000862 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000863 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +0000864 if (ShiftAmt < i) return -1;
865 ShiftAmt -= i;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000866
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000867 if (!isUnary) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000868 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000869 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000870 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000871 return -1;
872 } else {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000873 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000874 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000875 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000876 return -1;
877 }
Chris Lattner1d338192006-04-06 18:26:28 +0000878 return ShiftAmt;
879}
Chris Lattnerffc47562006-03-20 06:33:01 +0000880
881/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
882/// specifies a splat of a single element that is suitable for input to
883/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000884bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +0000885 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +0000886 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000887
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000888 // This is a splat operation if each element of the permute is the same, and
889 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000890 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +0000891
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000892 // FIXME: Handle UNDEF elements too!
893 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +0000894 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000895
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000896 // Check that the indices are consecutive, in the case of a multi-byte element
897 // splatted with a v16i8 mask.
898 for (unsigned i = 1; i != EltSize; ++i)
899 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000900 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000901
Chris Lattner95c7adc2006-04-04 17:25:31 +0000902 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000903 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +0000904 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000905 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000906 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000907 }
Chris Lattner95c7adc2006-04-04 17:25:31 +0000908 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +0000909}
910
Evan Cheng581d2792007-07-30 07:51:22 +0000911/// isAllNegativeZeroVector - Returns true if all elements of build_vector
912/// are -0.0.
913bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000914 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
915
916 APInt APVal, APUndef;
917 unsigned BitSize;
918 bool HasAnyUndefs;
Wesley Peck527da1b2010-11-23 03:31:01 +0000919
Dale Johannesen5f4eecf2009-11-13 01:45:18 +0000920 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000921 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000922 return CFP->getValueAPF().isNegZero();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000923
Evan Cheng581d2792007-07-30 07:51:22 +0000924 return false;
925}
926
Chris Lattnerffc47562006-03-20 06:33:01 +0000927/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
928/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner95c7adc2006-04-04 17:25:31 +0000929unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000930 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
931 assert(isSplatShuffleMask(SVOp, EltSize));
932 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +0000933}
934
Chris Lattner74cf9ff2006-04-12 17:37:20 +0000935/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +0000936/// by using a vspltis[bhw] instruction of the specified element size, return
937/// the constant being splatted. The ByteSize field indicates the number of
938/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000939SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
940 SDValue OpVal(0, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000941
942 // If ByteSize of the splat is bigger than the element size of the
943 // build_vector, then we have a case where we are checking for a splat where
944 // multiple elements of the buildvector are folded together into a single
945 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
946 unsigned EltSize = 16/N->getNumOperands();
947 if (EltSize < ByteSize) {
948 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000949 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000950 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000951
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000952 // See if all of the elements in the buildvector agree across.
953 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
954 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
955 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000956 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000957
Scott Michelcf0da6c2009-02-17 22:15:04 +0000958
Gabor Greiff304a7a2008-08-28 21:40:38 +0000959 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000960 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
961 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000962 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000963 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000964
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000965 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
966 // either constant or undef values that are identical for each chunk. See
967 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000968
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000969 // Check to see if all of the leading entries are either 0 or -1. If
970 // neither, then this won't fit into the immediate field.
971 bool LeadingZero = true;
972 bool LeadingOnes = true;
973 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000974 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000975
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000976 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
977 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
978 }
979 // Finally, check the least significant entry.
980 if (LeadingZero) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000981 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +0000982 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +0000983 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000984 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +0000985 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000986 }
987 if (LeadingOnes) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000988 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +0000989 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +0000990 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000991 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +0000992 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000993 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000994
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000995 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000996 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000997
Chris Lattner2771e2c2006-03-25 06:12:06 +0000998 // Check to see if this buildvec has a single non-undef value in its elements.
999 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1000 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001001 if (OpVal.getNode() == 0)
Chris Lattner2771e2c2006-03-25 06:12:06 +00001002 OpVal = N->getOperand(i);
1003 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001004 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001005 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001006
Gabor Greiff304a7a2008-08-28 21:40:38 +00001007 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001008
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001009 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001010 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001011 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001012 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001013 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001014 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001015 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001016 }
1017
1018 // If the splat value is larger than the element value, then we can never do
1019 // this splat. The only case that we could fit the replicated bits into our
1020 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001021 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001022
Chris Lattner2771e2c2006-03-25 06:12:06 +00001023 // If the element value is larger than the splat value, cut it in half and
1024 // check to see if the two halves are equal. Continue doing this until we
1025 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
1026 while (ValSizeInBytes > ByteSize) {
1027 ValSizeInBytes >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001028
Chris Lattner2771e2c2006-03-25 06:12:06 +00001029 // If the top half equals the bottom half, we're still ok.
Chris Lattner39cc7172006-04-05 17:39:25 +00001030 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
1031 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001032 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001033 }
1034
1035 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001036 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001037
Evan Chengb1ddc982006-03-26 09:52:32 +00001038 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001039 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001040
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001041 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001042 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +00001043 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001044 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001045}
1046
Chris Lattner4211ca92006-04-14 06:01:58 +00001047//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001048// Addressing Mode Selection
1049//===----------------------------------------------------------------------===//
1050
1051/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1052/// or 64-bit immediate, and if the value can be accurately represented as a
1053/// sign extension from a 16-bit value. If so, this returns true and the
1054/// immediate.
1055static bool isIntS16Immediate(SDNode *N, short &Imm) {
1056 if (N->getOpcode() != ISD::Constant)
1057 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001058
Dan Gohmaneffb8942008-09-12 16:56:44 +00001059 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001060 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001061 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001062 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001063 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001064}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001065static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001066 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001067}
1068
1069
1070/// SelectAddressRegReg - Given the specified addressed, check to see if it
1071/// can be represented as an indexed [r+r] operation. Returns false if it
1072/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001073bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1074 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001075 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001076 short imm = 0;
1077 if (N.getOpcode() == ISD::ADD) {
1078 if (isIntS16Immediate(N.getOperand(1), imm))
1079 return false; // r+i
1080 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1081 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001082
Chris Lattnera801fced2006-11-08 02:15:41 +00001083 Base = N.getOperand(0);
1084 Index = N.getOperand(1);
1085 return true;
1086 } else if (N.getOpcode() == ISD::OR) {
1087 if (isIntS16Immediate(N.getOperand(1), imm))
1088 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001089
Chris Lattnera801fced2006-11-08 02:15:41 +00001090 // If this is an or of disjoint bitfields, we can codegen this as an add
1091 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1092 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001093 APInt LHSKnownZero, LHSKnownOne;
1094 APInt RHSKnownZero, RHSKnownOne;
1095 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001096 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001097
Dan Gohmanf19609a2008-02-27 01:23:58 +00001098 if (LHSKnownZero.getBoolValue()) {
1099 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001100 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001101 // If all of the bits are known zero on the LHS or RHS, the add won't
1102 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001103 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001104 Base = N.getOperand(0);
1105 Index = N.getOperand(1);
1106 return true;
1107 }
1108 }
1109 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001110
Chris Lattnera801fced2006-11-08 02:15:41 +00001111 return false;
1112}
1113
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001114// If we happen to be doing an i64 load or store into a stack slot that has
1115// less than a 4-byte alignment, then the frame-index elimination may need to
1116// use an indexed load or store instruction (because the offset may not be a
1117// multiple of 4). The extra register needed to hold the offset comes from the
1118// register scavenger, and it is possible that the scavenger will need to use
1119// an emergency spill slot. As a result, we need to make sure that a spill slot
1120// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1121// stack slot.
1122static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1123 // FIXME: This does not handle the LWA case.
1124 if (VT != MVT::i64)
1125 return;
1126
Hal Finkel7ab3db52013-07-10 15:29:01 +00001127 // NOTE: We'll exclude negative FIs here, which come from argument
1128 // lowering, because there are no known test cases triggering this problem
1129 // using packed structures (or similar). We can remove this exclusion if
1130 // we find such a test case. The reason why this is so test-case driven is
1131 // because this entire 'fixup' is only to prevent crashes (from the
1132 // register scavenger) on not-really-valid inputs. For example, if we have:
1133 // %a = alloca i1
1134 // %b = bitcast i1* %a to i64*
1135 // store i64* a, i64 b
1136 // then the store should really be marked as 'align 1', but is not. If it
1137 // were marked as 'align 1' then the indexed form would have been
1138 // instruction-selected initially, and the problem this 'fixup' is preventing
1139 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001140 if (FrameIdx < 0)
1141 return;
1142
1143 MachineFunction &MF = DAG.getMachineFunction();
1144 MachineFrameInfo *MFI = MF.getFrameInfo();
1145
1146 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1147 if (Align >= 4)
1148 return;
1149
1150 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1151 FuncInfo->setHasNonRISpills();
1152}
1153
Chris Lattnera801fced2006-11-08 02:15:41 +00001154/// Returns true if the address N can be represented by a base register plus
1155/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001156/// represented as reg+reg. If Aligned is true, only accept displacements
1157/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001158bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001159 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001160 SelectionDAG &DAG,
1161 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001162 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001163 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001164 // If this can be more profitably realized as r+r, fail.
1165 if (SelectAddressRegReg(N, Disp, Base, DAG))
1166 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001167
Chris Lattnera801fced2006-11-08 02:15:41 +00001168 if (N.getOpcode() == ISD::ADD) {
1169 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001170 if (isIntS16Immediate(N.getOperand(1), imm) &&
1171 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001172 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001173 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1174 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001175 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001176 } else {
1177 Base = N.getOperand(0);
1178 }
1179 return true; // [r+i]
1180 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1181 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001182 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001183 && "Cannot handle constant offsets yet!");
1184 Disp = N.getOperand(1).getOperand(0); // The global address.
1185 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001186 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001187 Disp.getOpcode() == ISD::TargetConstantPool ||
1188 Disp.getOpcode() == ISD::TargetJumpTable);
1189 Base = N.getOperand(0);
1190 return true; // [&g+r]
1191 }
1192 } else if (N.getOpcode() == ISD::OR) {
1193 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001194 if (isIntS16Immediate(N.getOperand(1), imm) &&
1195 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001196 // If this is an or of disjoint bitfields, we can codegen this as an add
1197 // (for better address arithmetic) if the LHS and RHS of the OR are
1198 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001199 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001200 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001201
Dan Gohmanf19609a2008-02-27 01:23:58 +00001202 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001203 // If all of the bits are known zero on the LHS or RHS, the add won't
1204 // carry.
1205 Base = N.getOperand(0);
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001206 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001207 return true;
1208 }
1209 }
1210 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1211 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001212
Chris Lattnera801fced2006-11-08 02:15:41 +00001213 // If this address fits entirely in a 16-bit sext immediate field, codegen
1214 // this as "d, 0"
1215 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001216 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001217 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkelf70c41e2013-03-21 23:45:03 +00001218 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1219 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001220 return true;
1221 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001222
1223 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001224 if ((CN->getValueType(0) == MVT::i32 ||
1225 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1226 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001227 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001228
Chris Lattnera801fced2006-11-08 02:15:41 +00001229 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001230 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001231
Owen Anderson9f944592009-08-11 20:47:22 +00001232 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1233 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001234 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001235 return true;
1236 }
1237 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001238
Chris Lattnera801fced2006-11-08 02:15:41 +00001239 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001240 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001241 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001242 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1243 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001244 Base = N;
1245 return true; // [r+0]
1246}
1247
1248/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1249/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001250bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1251 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001252 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001253 // Check to see if we can easily represent this as an [r+r] address. This
1254 // will fail if it thinks that the address is more profitably represented as
1255 // reg+imm, e.g. where imm = 0.
1256 if (SelectAddressRegReg(N, Base, Index, DAG))
1257 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001258
Chris Lattnera801fced2006-11-08 02:15:41 +00001259 // If the operand is an addition, always emit this as [r+r], since this is
1260 // better (for code size, and execution, as the memop does the add for free)
1261 // than emitting an explicit add.
1262 if (N.getOpcode() == ISD::ADD) {
1263 Base = N.getOperand(0);
1264 Index = N.getOperand(1);
1265 return true;
1266 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001267
Chris Lattnera801fced2006-11-08 02:15:41 +00001268 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkelf70c41e2013-03-21 23:45:03 +00001269 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1270 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001271 Index = N;
1272 return true;
1273}
1274
Chris Lattnera801fced2006-11-08 02:15:41 +00001275/// getPreIndexedAddressParts - returns true by value, base pointer and
1276/// offset pointer and addressing mode by reference if the node's address
1277/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001278bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1279 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001280 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001281 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001282 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001283
Ulrich Weigande90b0222013-03-22 14:58:48 +00001284 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001285 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001286 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001287 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001288 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1289 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001290 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001291 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001292 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001293 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001294 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001295 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001296 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001297 } else
1298 return false;
1299
Chris Lattner68371252006-11-14 01:38:31 +00001300 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands13237ac2008-06-06 12:08:01 +00001301 if (VT.isVector())
Chris Lattner68371252006-11-14 01:38:31 +00001302 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001303
Ulrich Weigande90b0222013-03-22 14:58:48 +00001304 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1305
1306 // Common code will reject creating a pre-inc form if the base pointer
1307 // is a frame index, or if N is a store and the base pointer is either
1308 // the same as or a predecessor of the value being stored. Check for
1309 // those situations here, and try with swapped Base/Offset instead.
1310 bool Swap = false;
1311
1312 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1313 Swap = true;
1314 else if (!isLoad) {
1315 SDValue Val = cast<StoreSDNode>(N)->getValue();
1316 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1317 Swap = true;
1318 }
1319
1320 if (Swap)
1321 std::swap(Base, Offset);
1322
Hal Finkelca542be2012-06-20 15:43:03 +00001323 AM = ISD::PRE_INC;
1324 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001325 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001326
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001327 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001328 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001329 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001330 return false;
1331 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001332 // LDU/STU need an address with at least 4-byte alignment.
1333 if (Alignment < 4)
1334 return false;
1335
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001336 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001337 return false;
1338 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001339
Chris Lattnerb314b152006-11-11 00:08:42 +00001340 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001341 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1342 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001343 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001344 LD->getExtensionType() == ISD::SEXTLOAD &&
1345 isa<ConstantSDNode>(Offset))
1346 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001347 }
1348
Chris Lattnerce645542006-11-10 02:08:47 +00001349 AM = ISD::PRE_INC;
1350 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001351}
1352
1353//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001354// LowerOperation implementation
1355//===----------------------------------------------------------------------===//
1356
Chris Lattneredb9d842010-11-15 02:46:57 +00001357/// GetLabelAccessInfo - Return true if we should reference labels using a
1358/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1359static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Chris Lattnerdd6df842010-11-15 03:13:19 +00001360 unsigned &LoOpFlags, const GlobalValue *GV = 0) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001361 HiOpFlags = PPCII::MO_HA;
1362 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001363
Chris Lattneredb9d842010-11-15 02:46:57 +00001364 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1365 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peck527da1b2010-11-23 03:31:01 +00001366 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattneredb9d842010-11-15 02:46:57 +00001367 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattnerdd6df842010-11-15 03:13:19 +00001368 if (isPIC) {
1369 HiOpFlags |= PPCII::MO_PIC_FLAG;
1370 LoOpFlags |= PPCII::MO_PIC_FLAG;
1371 }
1372
1373 // If this is a reference to a global value that requires a non-lazy-ptr, make
1374 // sure that instruction lowering adds it.
1375 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1376 HiOpFlags |= PPCII::MO_NLP_FLAG;
1377 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001378
Chris Lattnerdd6df842010-11-15 03:13:19 +00001379 if (GV->hasHiddenVisibility()) {
1380 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1381 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1382 }
1383 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001384
Chris Lattneredb9d842010-11-15 02:46:57 +00001385 return isPIC;
1386}
1387
1388static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1389 SelectionDAG &DAG) {
1390 EVT PtrVT = HiPart.getValueType();
1391 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001392 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001393
1394 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1395 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001396
Chris Lattneredb9d842010-11-15 02:46:57 +00001397 // With PIC, the first instruction is actually "GR+hi(&G)".
1398 if (isPIC)
1399 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1400 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001401
Chris Lattneredb9d842010-11-15 02:46:57 +00001402 // Generate non-pic code that has direct accesses to the constant pool.
1403 // The address of the global is just (hi(&g)+lo(&g)).
1404 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1405}
1406
Scott Michelcf0da6c2009-02-17 22:15:04 +00001407SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001408 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001409 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001410 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001411 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001412
Roman Divackyace47072012-08-24 16:26:02 +00001413 // 64-bit SVR4 ABI code is always position-independent.
1414 // The actual address of the GlobalValue is stored in the TOC.
1415 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1416 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001417 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001418 DAG.getRegister(PPC::X2, MVT::i64));
1419 }
1420
Chris Lattneredb9d842010-11-15 02:46:57 +00001421 unsigned MOHiFlag, MOLoFlag;
1422 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1423 SDValue CPIHi =
1424 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1425 SDValue CPILo =
1426 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1427 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001428}
1429
Dan Gohman21cea8a2010-04-17 15:26:15 +00001430SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001431 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001432 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001433
Roman Divackyace47072012-08-24 16:26:02 +00001434 // 64-bit SVR4 ABI code is always position-independent.
1435 // The actual address of the GlobalValue is stored in the TOC.
1436 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1437 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001438 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001439 DAG.getRegister(PPC::X2, MVT::i64));
1440 }
1441
Chris Lattneredb9d842010-11-15 02:46:57 +00001442 unsigned MOHiFlag, MOLoFlag;
1443 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1444 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1445 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1446 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001447}
1448
Dan Gohman21cea8a2010-04-17 15:26:15 +00001449SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1450 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001451 EVT PtrVT = Op.getValueType();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001452
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001453 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peck527da1b2010-11-23 03:31:01 +00001454
Chris Lattneredb9d842010-11-15 02:46:57 +00001455 unsigned MOHiFlag, MOLoFlag;
1456 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001457 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1458 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001459 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1460}
1461
Roman Divackye3f15c982012-06-04 17:36:38 +00001462SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1463 SelectionDAG &DAG) const {
1464
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001465 // FIXME: TLS addresses currently use medium model code sequences,
1466 // which is the most useful form. Eventually support for small and
1467 // large models could be added if users need it, at the cost of
1468 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001469 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001470 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001471 const GlobalValue *GV = GA->getGlobal();
1472 EVT PtrVT = getPointerTy();
1473 bool is64bit = PPCSubTarget.isPPC64();
1474
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001475 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001476
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001477 if (Model == TLSModel::LocalExec) {
1478 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001479 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001480 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001481 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001482 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1483 is64bit ? MVT::i64 : MVT::i32);
1484 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1485 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1486 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001487
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001488 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001489 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001490 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1491 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00001492 SDValue GOTPtr;
1493 if (is64bit) {
1494 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1495 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1496 PtrVT, GOTReg, TGA);
1497 } else
1498 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001499 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00001500 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001501 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001502 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001503
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001504 if (Model == TLSModel::GeneralDynamic) {
1505 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1506 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1507 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1508 GOTReg, TGA);
1509 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1510 GOTEntryHi, TGA);
1511
1512 // We need a chain node, and don't have one handy. The underlying
1513 // call has no side effects, so using the function entry node
1514 // suffices.
1515 SDValue Chain = DAG.getEntryNode();
1516 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1517 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1518 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1519 PtrVT, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001520 // The return value from GET_TLS_ADDR really is in X3 already, but
1521 // some hacks are needed here to tie everything together. The extra
1522 // copies dissolve during subsequent transforms.
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001523 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1524 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1525 }
1526
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001527 if (Model == TLSModel::LocalDynamic) {
1528 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1529 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1530 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1531 GOTReg, TGA);
1532 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1533 GOTEntryHi, TGA);
1534
1535 // We need a chain node, and don't have one handy. The underlying
1536 // call has no side effects, so using the function entry node
1537 // suffices.
1538 SDValue Chain = DAG.getEntryNode();
1539 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1540 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1541 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1542 PtrVT, ParmReg, TGA);
1543 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1544 // some hacks are needed here to tie everything together. The extra
1545 // copies dissolve during subsequent transforms.
1546 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1547 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt9ed4dbc2012-12-13 20:57:10 +00001548 Chain, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001549 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1550 }
1551
1552 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00001553}
1554
Chris Lattneredb9d842010-11-15 02:46:57 +00001555SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1556 SelectionDAG &DAG) const {
1557 EVT PtrVT = Op.getValueType();
1558 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001559 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00001560 const GlobalValue *GV = GSDN->getGlobal();
1561
Chris Lattneredb9d842010-11-15 02:46:57 +00001562 // 64-bit SVR4 ABI code is always position-independent.
1563 // The actual address of the GlobalValue is stored in the TOC.
1564 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1565 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1566 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1567 DAG.getRegister(PPC::X2, MVT::i64));
1568 }
1569
Chris Lattnerdd6df842010-11-15 03:13:19 +00001570 unsigned MOHiFlag, MOLoFlag;
1571 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00001572
Chris Lattnerdd6df842010-11-15 03:13:19 +00001573 SDValue GAHi =
1574 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1575 SDValue GALo =
1576 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00001577
Chris Lattnerdd6df842010-11-15 03:13:19 +00001578 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00001579
Chris Lattnerdd6df842010-11-15 03:13:19 +00001580 // If the global reference is actually to a non-lazy-pointer, we have to do an
1581 // extra load to get the address of the global.
1582 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1583 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001584 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00001585 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00001586}
1587
Dan Gohman21cea8a2010-04-17 15:26:15 +00001588SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00001589 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001590 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001591
Chris Lattner4211ca92006-04-14 06:01:58 +00001592 // If we're comparing for equality to zero, expose the fact that this is
1593 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1594 // fold the new nodes.
1595 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1596 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001597 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001598 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001599 if (VT.bitsLT(MVT::i32)) {
1600 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001601 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001602 }
Duncan Sands13237ac2008-06-06 12:08:01 +00001603 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001604 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1605 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00001606 DAG.getConstant(Log2b, MVT::i32));
1607 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00001608 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001609 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00001610 // optimized. FIXME: revisit this when we can custom lower all setcc
1611 // optimizations.
1612 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001613 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001614 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001615
Chris Lattner4211ca92006-04-14 06:01:58 +00001616 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00001617 // by xor'ing the rhs with the lhs, which is faster than setting a
1618 // condition register, reading it back out, and masking the correct bit. The
1619 // normal approach here uses sub to do this instead of xor. Using xor exposes
1620 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001621 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00001622 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001623 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001624 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00001625 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001626 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00001627 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001628 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001629}
1630
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001631SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001632 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00001633 SDNode *Node = Op.getNode();
1634 EVT VT = Node->getValueType(0);
1635 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1636 SDValue InChain = Node->getOperand(0);
1637 SDValue VAListPtr = Node->getOperand(1);
1638 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001639 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001640
Roman Divacky4394e682011-06-28 15:30:42 +00001641 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1642
1643 // gpr_index
1644 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1645 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1646 false, false, 0);
1647 InChain = GprIndex.getValue(1);
1648
1649 if (VT == MVT::i64) {
1650 // Check if GprIndex is even
1651 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1652 DAG.getConstant(1, MVT::i32));
1653 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1654 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1655 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1656 DAG.getConstant(1, MVT::i32));
1657 // Align GprIndex to be even if it isn't
1658 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1659 GprIndex);
1660 }
1661
1662 // fpr index is 1 byte after gpr
1663 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1664 DAG.getConstant(1, MVT::i32));
1665
1666 // fpr
1667 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1668 FprPtr, MachinePointerInfo(SV), MVT::i8,
1669 false, false, 0);
1670 InChain = FprIndex.getValue(1);
1671
1672 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1673 DAG.getConstant(8, MVT::i32));
1674
1675 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1676 DAG.getConstant(4, MVT::i32));
1677
1678 // areas
1679 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001680 MachinePointerInfo(), false, false,
1681 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001682 InChain = OverflowArea.getValue(1);
1683
1684 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001685 MachinePointerInfo(), false, false,
1686 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001687 InChain = RegSaveArea.getValue(1);
1688
1689 // select overflow_area if index > 8
1690 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1691 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1692
Roman Divacky4394e682011-06-28 15:30:42 +00001693 // adjustment constant gpr_index * 4/8
1694 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1695 VT.isInteger() ? GprIndex : FprIndex,
1696 DAG.getConstant(VT.isInteger() ? 4 : 8,
1697 MVT::i32));
1698
1699 // OurReg = RegSaveArea + RegConstant
1700 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1701 RegConstant);
1702
1703 // Floating types are 32 bytes into RegSaveArea
1704 if (VT.isFloatingPoint())
1705 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1706 DAG.getConstant(32, MVT::i32));
1707
1708 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1709 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1710 VT.isInteger() ? GprIndex : FprIndex,
1711 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1712 MVT::i32));
1713
1714 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1715 VT.isInteger() ? VAListPtr : FprPtr,
1716 MachinePointerInfo(SV),
1717 MVT::i8, false, false, 0);
1718
1719 // determine if we should load from reg_save_area or overflow_area
1720 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1721
1722 // increase overflow_area by 4/8 if gpr/fpr > 8
1723 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1724 DAG.getConstant(VT.isInteger() ? 4 : 8,
1725 MVT::i32));
1726
1727 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1728 OverflowAreaPlusN);
1729
1730 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1731 OverflowAreaPtr,
1732 MachinePointerInfo(),
1733 MVT::i32, false, false, 0);
1734
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00001735 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001736 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001737}
1738
Roman Divackyc3825df2013-07-25 21:36:47 +00001739SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
1740 const PPCSubtarget &Subtarget) const {
1741 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
1742
1743 // We have to copy the entire va_list struct:
1744 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
1745 return DAG.getMemcpy(Op.getOperand(0), Op,
1746 Op.getOperand(1), Op.getOperand(2),
1747 DAG.getConstant(12, MVT::i32), 8, false, true,
1748 MachinePointerInfo(), MachinePointerInfo());
1749}
1750
Duncan Sandsa0984362011-09-06 13:37:06 +00001751SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1752 SelectionDAG &DAG) const {
1753 return Op.getOperand(0);
1754}
1755
1756SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1757 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00001758 SDValue Chain = Op.getOperand(0);
1759 SDValue Trmp = Op.getOperand(1); // trampoline
1760 SDValue FPtr = Op.getOperand(2); // nested function
1761 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00001762 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00001763
Owen Anderson53aa7a92009-08-10 22:56:29 +00001764 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00001765 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00001766 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001767 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00001768 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00001769
Scott Michelcf0da6c2009-02-17 22:15:04 +00001770 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00001771 TargetLowering::ArgListEntry Entry;
1772
1773 Entry.Ty = IntPtrTy;
1774 Entry.Node = Trmp; Args.push_back(Entry);
1775
1776 // TrampSize == (isPPC64 ? 48 : 40);
1777 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00001778 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00001779 Args.push_back(Entry);
1780
1781 Entry.Node = FPtr; Args.push_back(Entry);
1782 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001783
Bill Wendling95e1af22008-09-17 00:30:57 +00001784 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Justin Holewinskiaa583972012-05-25 16:35:28 +00001785 TargetLowering::CallLoweringInfo CLI(Chain,
1786 Type::getVoidTy(*DAG.getContext()),
1787 false, false, false, false, 0,
1788 CallingConv::C,
Evan Cheng65f9d192012-02-28 18:51:51 +00001789 /*isTailCall=*/false,
Justin Holewinskiaa583972012-05-25 16:35:28 +00001790 /*doesNotRet=*/false,
1791 /*isReturnValueUsed=*/true,
Bill Wendling95e1af22008-09-17 00:30:57 +00001792 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00001793 Args, DAG, dl);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001794 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bill Wendling95e1af22008-09-17 00:30:57 +00001795
Duncan Sandsa0984362011-09-06 13:37:06 +00001796 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00001797}
1798
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001799SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001800 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001801 MachineFunction &MF = DAG.getMachineFunction();
1802 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1803
Andrew Trickef9de2a2013-05-25 02:42:55 +00001804 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001805
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001806 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001807 // vastart just stores the address of the VarArgsFrameIndex slot into the
1808 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001809 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00001810 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001811 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00001812 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1813 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00001814 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001815 }
1816
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001817 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001818 // We suppose the given va_list is already allocated.
1819 //
1820 // typedef struct {
1821 // char gpr; /* index into the array of 8 GPRs
1822 // * stored in the register save area
1823 // * gpr=0 corresponds to r3,
1824 // * gpr=1 to r4, etc.
1825 // */
1826 // char fpr; /* index into the array of 8 FPRs
1827 // * stored in the register save area
1828 // * fpr=0 corresponds to f1,
1829 // * fpr=1 to f2, etc.
1830 // */
1831 // char *overflow_arg_area;
1832 // /* location on stack that holds
1833 // * the next overflow argument
1834 // */
1835 // char *reg_save_area;
1836 // /* where r3:r10 and f1:f8 (if saved)
1837 // * are stored
1838 // */
1839 // } va_list[1];
1840
1841
Dan Gohman31ae5862010-04-17 14:41:14 +00001842 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1843 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001844
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001845
Owen Anderson53aa7a92009-08-10 22:56:29 +00001846 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001847
Dan Gohman31ae5862010-04-17 14:41:14 +00001848 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1849 PtrVT);
1850 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1851 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001852
Duncan Sands13237ac2008-06-06 12:08:01 +00001853 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001854 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001855
Duncan Sands13237ac2008-06-06 12:08:01 +00001856 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001857 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001858
1859 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001860 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001861
Dan Gohman2d489b52008-02-06 22:27:42 +00001862 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001863
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001864 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001865 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00001866 Op.getOperand(1),
1867 MachinePointerInfo(SV),
1868 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001869 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001870 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001871 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001872
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001873 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001874 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00001875 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1876 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00001877 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001878 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001879 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001880
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001881 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001882 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00001883 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1884 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001885 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001886 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001887 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001888
1889 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00001890 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1891 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001892 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001893
Chris Lattner4211ca92006-04-14 06:01:58 +00001894}
1895
Chris Lattner4f2e4e02007-03-06 00:59:59 +00001896#include "PPCGenCallingConv.inc"
1897
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001898// Function whose sole purpose is to kill compiler warnings
1899// stemming from unused functions included from PPCGenCallingConv.inc.
1900CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001901 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001902}
1903
Bill Schmidt230b4512013-06-12 16:39:22 +00001904bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1905 CCValAssign::LocInfo &LocInfo,
1906 ISD::ArgFlagsTy &ArgFlags,
1907 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001908 return true;
1909}
1910
Bill Schmidt230b4512013-06-12 16:39:22 +00001911bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
1912 MVT &LocVT,
1913 CCValAssign::LocInfo &LocInfo,
1914 ISD::ArgFlagsTy &ArgFlags,
1915 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00001916 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001917 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1918 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1919 };
1920 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00001921
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001922 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1923
1924 // Skip one register if the first unallocated register has an even register
1925 // number and there are still argument registers available which have not been
1926 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1927 // need to skip a register if RegNum is odd.
1928 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1929 State.AllocateReg(ArgRegs[RegNum]);
1930 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001931
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001932 // Always return false here, as this function only makes sure that the first
1933 // unallocated register has an odd register number and does not actually
1934 // allocate a register for the current argument.
1935 return false;
1936}
1937
Bill Schmidt230b4512013-06-12 16:39:22 +00001938bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
1939 MVT &LocVT,
1940 CCValAssign::LocInfo &LocInfo,
1941 ISD::ArgFlagsTy &ArgFlags,
1942 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00001943 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001944 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1945 PPC::F8
1946 };
1947
1948 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00001949
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001950 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1951
1952 // If there is only one Floating-point register left we need to put both f64
1953 // values of a split ppc_fp128 value on the stack.
1954 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1955 State.AllocateReg(ArgRegs[RegNum]);
1956 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001957
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001958 // Always return false here, as this function only makes sure that the two f64
1959 // values a ppc_fp128 value is split into are both passed in registers or both
1960 // passed on the stack and does not actually allocate a register for the
1961 // current argument.
1962 return false;
1963}
1964
Chris Lattner43df5b32007-02-25 05:34:32 +00001965/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001966/// on Darwin.
Craig Topperca658c22012-03-11 07:16:55 +00001967static const uint16_t *GetFPR() {
1968 static const uint16_t FPR[] = {
Chris Lattner43df5b32007-02-25 05:34:32 +00001969 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001970 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner43df5b32007-02-25 05:34:32 +00001971 };
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001972
Chris Lattner43df5b32007-02-25 05:34:32 +00001973 return FPR;
1974}
1975
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00001976/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1977/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001978static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00001979 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00001980 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00001981 if (Flags.isByVal())
1982 ArgSize = Flags.getByValSize();
1983 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1984
1985 return ArgSize;
1986}
1987
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001988SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001989PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001990 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001991 const SmallVectorImpl<ISD::InputArg>
1992 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001993 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001994 SmallVectorImpl<SDValue> &InVals)
1995 const {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00001996 if (PPCSubTarget.isSVR4ABI()) {
1997 if (PPCSubTarget.isPPC64())
1998 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
1999 dl, DAG, InVals);
2000 else
2001 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2002 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002003 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002004 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2005 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002006 }
2007}
2008
2009SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002010PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002011 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002012 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002013 const SmallVectorImpl<ISD::InputArg>
2014 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002015 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002016 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002017
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002018 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002019 // +-----------------------------------+
2020 // +--> | Back chain |
2021 // | +-----------------------------------+
2022 // | | Floating-point register save area |
2023 // | +-----------------------------------+
2024 // | | General register save area |
2025 // | +-----------------------------------+
2026 // | | CR save word |
2027 // | +-----------------------------------+
2028 // | | VRSAVE save word |
2029 // | +-----------------------------------+
2030 // | | Alignment padding |
2031 // | +-----------------------------------+
2032 // | | Vector register save area |
2033 // | +-----------------------------------+
2034 // | | Local variable space |
2035 // | +-----------------------------------+
2036 // | | Parameter list area |
2037 // | +-----------------------------------+
2038 // | | LR save word |
2039 // | +-----------------------------------+
2040 // SP--> +--- | Back chain |
2041 // +-----------------------------------+
2042 //
2043 // Specifications:
2044 // System V Application Binary Interface PowerPC Processor Supplement
2045 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002046
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002047 MachineFunction &MF = DAG.getMachineFunction();
2048 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002049 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002050
Owen Anderson53aa7a92009-08-10 22:56:29 +00002051 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002052 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002053 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2054 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002055 unsigned PtrByteSize = 4;
2056
2057 // Assign locations to all of the incoming arguments.
2058 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002059 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002060 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002061
2062 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002063 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002064
Bill Schmidtef17c142013-02-06 17:33:58 +00002065 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002066
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002067 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2068 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002069
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002070 // Arguments stored in registers.
2071 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002072 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002073 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002074
Owen Anderson9f944592009-08-11 20:47:22 +00002075 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002076 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002077 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002078 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002079 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002080 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002081 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002082 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00002083 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002084 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002085 case MVT::f64:
Craig Topperabadc662012-04-20 06:31:50 +00002086 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002087 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002088 case MVT::v16i8:
2089 case MVT::v8i16:
2090 case MVT::v4i32:
2091 case MVT::v4f32:
Craig Topperabadc662012-04-20 06:31:50 +00002092 RC = &PPC::VRRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002093 break;
2094 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002095
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002096 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002097 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002098 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2099 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2100
2101 if (ValVT == MVT::i1)
2102 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002103
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002104 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002105 } else {
2106 // Argument stored in memory.
2107 assert(VA.isMemLoc());
2108
Hal Finkel940ab932014-02-28 00:27:01 +00002109 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002110 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002111 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002112
2113 // Create load nodes to retrieve arguments from the stack.
2114 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002115 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2116 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002117 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002118 }
2119 }
2120
2121 // Assign locations to all of the incoming aggregate by value arguments.
2122 // Aggregates passed by value are stored in the local variable space of the
2123 // caller's stack frame, right above the parameter list area.
2124 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002125 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002126 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002127
2128 // Reserve stack space for the allocations in CCInfo.
2129 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2130
Bill Schmidtef17c142013-02-06 17:33:58 +00002131 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002132
2133 // Area that is at least reserved in the caller of this function.
2134 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00002135
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002136 // Set the size that is at least reserved in caller of this function. Tail
2137 // call optimized function's reserved stack space needs to be aligned so that
2138 // taking the difference between two stack areas will result in an aligned
2139 // stack.
2140 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2141
2142 MinReservedArea =
2143 std::max(MinReservedArea,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002144 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peck527da1b2010-11-23 03:31:01 +00002145
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002146 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002147 getStackAlignment();
2148 unsigned AlignMask = TargetAlign-1;
2149 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peck527da1b2010-11-23 03:31:01 +00002150
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002151 FI->setMinReservedArea(MinReservedArea);
2152
2153 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002154
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002155 // If the function takes variable number of arguments, make a frame index for
2156 // the start of the first vararg value... for expansion of llvm.va_start.
2157 if (isVarArg) {
Craig Topperbef78fc2012-03-11 07:57:25 +00002158 static const uint16_t GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002159 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2160 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2161 };
2162 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2163
Craig Topperbef78fc2012-03-11 07:57:25 +00002164 static const uint16_t FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002165 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2166 PPC::F8
2167 };
2168 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2169
Dan Gohman31ae5862010-04-17 14:41:14 +00002170 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2171 NumGPArgRegs));
2172 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2173 NumFPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002174
2175 // Make room for NumGPArgRegs and NumFPArgRegs.
2176 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson9f944592009-08-11 20:47:22 +00002177 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002178
Dan Gohman31ae5862010-04-17 14:41:14 +00002179 FuncInfo->setVarArgsStackOffset(
2180 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002181 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002182
Dan Gohman31ae5862010-04-17 14:41:14 +00002183 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2184 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002185
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002186 // The fixed integer arguments of a variadic function are stored to the
2187 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2188 // the result of va_next.
2189 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2190 // Get an existing live-in vreg, or add a new one.
2191 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2192 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002193 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002194
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002195 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002196 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2197 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002198 MemOps.push_back(Store);
2199 // Increment the address by four for the next argument to store
2200 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2201 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2202 }
2203
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002204 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2205 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002206 // The double arguments are stored to the VarArgsFrameIndex
2207 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002208 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2209 // Get an existing live-in vreg, or add a new one.
2210 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2211 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002212 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002213
Owen Anderson9f944592009-08-11 20:47:22 +00002214 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002215 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2216 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002217 MemOps.push_back(Store);
2218 // Increment the address by eight for the next argument to store
Owen Anderson9f944592009-08-11 20:47:22 +00002219 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002220 PtrVT);
2221 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2222 }
2223 }
2224
2225 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002226 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002227 MVT::Other, &MemOps[0], MemOps.size());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002228
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002229 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002230}
2231
Bill Schmidt57d6de52012-10-23 15:51:16 +00002232// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2233// value to MVT::i64 and then truncate to the correct register size.
2234SDValue
2235PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2236 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002237 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002238 if (Flags.isSExt())
2239 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2240 DAG.getValueType(ObjectVT));
2241 else if (Flags.isZExt())
2242 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2243 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002244
Hal Finkel940ab932014-02-28 00:27:01 +00002245 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002246}
2247
2248// Set the size that is at least reserved in caller of this function. Tail
2249// call optimized functions' reserved stack space needs to be aligned so that
2250// taking the difference between two stack areas will result in an aligned
2251// stack.
2252void
2253PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2254 unsigned nAltivecParamsAtEnd,
2255 unsigned MinReservedArea,
2256 bool isPPC64) const {
2257 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2258 // Add the Altivec parameters at the end, if needed.
2259 if (nAltivecParamsAtEnd) {
2260 MinReservedArea = ((MinReservedArea+15)/16)*16;
2261 MinReservedArea += 16*nAltivecParamsAtEnd;
2262 }
2263 MinReservedArea =
2264 std::max(MinReservedArea,
2265 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2266 unsigned TargetAlign
2267 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2268 getStackAlignment();
2269 unsigned AlignMask = TargetAlign-1;
2270 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2271 FI->setMinReservedArea(MinReservedArea);
2272}
2273
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002274SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002275PPCTargetLowering::LowerFormalArguments_64SVR4(
2276 SDValue Chain,
2277 CallingConv::ID CallConv, bool isVarArg,
2278 const SmallVectorImpl<ISD::InputArg>
2279 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002280 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002281 SmallVectorImpl<SDValue> &InVals) const {
2282 // TODO: add description of PPC stack frame format, or at least some docs.
2283 //
2284 MachineFunction &MF = DAG.getMachineFunction();
2285 MachineFrameInfo *MFI = MF.getFrameInfo();
2286 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2287
2288 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2289 // Potential tail calls could cause overwriting of argument stack slots.
2290 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2291 (CallConv == CallingConv::Fast));
2292 unsigned PtrByteSize = 8;
2293
2294 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2295 // Area that is at least reserved in caller of this function.
2296 unsigned MinReservedArea = ArgOffset;
2297
2298 static const uint16_t GPR[] = {
2299 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2300 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2301 };
2302
2303 static const uint16_t *FPR = GetFPR();
2304
2305 static const uint16_t VR[] = {
2306 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2307 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2308 };
2309
2310 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2311 const unsigned Num_FPR_Regs = 13;
2312 const unsigned Num_VR_Regs = array_lengthof(VR);
2313
2314 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2315
2316 // Add DAG nodes to load the arguments or copy them out of registers. On
2317 // entry to a function on PPC, the arguments start after the linkage area,
2318 // although the first ones are often in registers.
2319
2320 SmallVector<SDValue, 8> MemOps;
2321 unsigned nAltivecParamsAtEnd = 0;
2322 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002323 unsigned CurArgIdx = 0;
2324 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002325 SDValue ArgVal;
2326 bool needsLoad = false;
2327 EVT ObjectVT = Ins[ArgNo].VT;
Hal Finkel940ab932014-02-28 00:27:01 +00002328 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002329 unsigned ArgSize = ObjSize;
2330 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt6631e942013-02-20 17:31:41 +00002331 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2332 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002333
2334 unsigned CurArgOffset = ArgOffset;
2335
2336 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2337 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2338 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
2339 if (isVarArg) {
2340 MinReservedArea = ((MinReservedArea+15)/16)*16;
2341 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2342 Flags,
2343 PtrByteSize);
2344 } else
2345 nAltivecParamsAtEnd++;
2346 } else
2347 // Calculate min reserved area.
2348 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2349 Flags,
2350 PtrByteSize);
2351
2352 // FIXME the codegen can be much improved in some cases.
2353 // We do not have to keep everything in memory.
2354 if (Flags.isByVal()) {
2355 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2356 ObjSize = Flags.getByValSize();
2357 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002358 // Empty aggregate parameters do not take up registers. Examples:
2359 // struct { } a;
2360 // union { } b;
2361 // int c[0];
2362 // etc. However, we have to provide a place-holder in InVals, so
2363 // pretend we have an 8-byte item at the current address for that
2364 // purpose.
2365 if (!ObjSize) {
2366 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2367 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2368 InVals.push_back(FIN);
2369 continue;
2370 }
Hal Finkel262a2242013-09-12 23:20:06 +00002371
2372 unsigned BVAlign = Flags.getByValAlign();
2373 if (BVAlign > 8) {
2374 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
2375 CurArgOffset = ArgOffset;
2376 }
2377
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002378 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt48081ca2012-10-16 13:30:53 +00002379 if (ObjSize < PtrByteSize)
2380 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002381 // The value of the object is its address.
2382 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2383 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2384 InVals.push_back(FIN);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002385
2386 if (ObjSize < 8) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002387 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002388 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002389 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002390 SDValue Store;
2391
2392 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2393 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2394 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2395 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002396 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002397 ObjType, false, false, 0);
2398 } else {
2399 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2400 // store the whole register as-is to the parameter save area
2401 // slot. The address of the parameter was already calculated
2402 // above (InVals.push_back(FIN)) to be the right-justified
2403 // offset within the slot. For this store, we need a new
2404 // frame index that points at the beginning of the slot.
2405 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2406 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2407 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002408 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002409 false, false, 0);
2410 }
2411
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002412 MemOps.push_back(Store);
2413 ++GPR_idx;
2414 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002415 // Whether we copied from a register or not, advance the offset
2416 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002417 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002418 continue;
2419 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002420
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002421 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2422 // Store whatever pieces of the object are in registers
2423 // to memory. ArgOffset will be the address of the beginning
2424 // of the object.
2425 if (GPR_idx != Num_GPR_Regs) {
2426 unsigned VReg;
2427 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2428 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2429 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2430 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002431 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002432 MachinePointerInfo(FuncArg, j),
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002433 false, false, 0);
2434 MemOps.push_back(Store);
2435 ++GPR_idx;
2436 ArgOffset += PtrByteSize;
2437 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00002438 ArgOffset += ArgSize - j;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002439 break;
2440 }
2441 }
2442 continue;
2443 }
2444
2445 switch (ObjectVT.getSimpleVT().SimpleTy) {
2446 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00002447 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002448 case MVT::i32:
2449 case MVT::i64:
2450 if (GPR_idx != Num_GPR_Regs) {
2451 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2452 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2453
Hal Finkel940ab932014-02-28 00:27:01 +00002454 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002455 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2456 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002457 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002458
2459 ++GPR_idx;
2460 } else {
2461 needsLoad = true;
2462 ArgSize = PtrByteSize;
2463 }
2464 ArgOffset += 8;
2465 break;
2466
2467 case MVT::f32:
2468 case MVT::f64:
2469 // Every 8 bytes of argument space consumes one of the GPRs available for
2470 // argument passing.
2471 if (GPR_idx != Num_GPR_Regs) {
2472 ++GPR_idx;
2473 }
2474 if (FPR_idx != Num_FPR_Regs) {
2475 unsigned VReg;
2476
2477 if (ObjectVT == MVT::f32)
2478 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2479 else
2480 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
2481
2482 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2483 ++FPR_idx;
2484 } else {
2485 needsLoad = true;
Bill Schmidt22162472012-10-11 15:38:20 +00002486 ArgSize = PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002487 }
2488
2489 ArgOffset += 8;
2490 break;
2491 case MVT::v4f32:
2492 case MVT::v4i32:
2493 case MVT::v8i16:
2494 case MVT::v16i8:
2495 // Note that vector arguments in registers don't reserve stack space,
2496 // except in varargs functions.
2497 if (VR_idx != Num_VR_Regs) {
2498 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
2499 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2500 if (isVarArg) {
2501 while ((ArgOffset % 16) != 0) {
2502 ArgOffset += PtrByteSize;
2503 if (GPR_idx != Num_GPR_Regs)
2504 GPR_idx++;
2505 }
2506 ArgOffset += 16;
2507 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2508 }
2509 ++VR_idx;
2510 } else {
2511 // Vectors are aligned.
2512 ArgOffset = ((ArgOffset+15)/16)*16;
2513 CurArgOffset = ArgOffset;
2514 ArgOffset += 16;
2515 needsLoad = true;
2516 }
2517 break;
2518 }
2519
2520 // We need to load the argument to a virtual register if we determined
2521 // above that we ran out of physical registers of the appropriate type.
2522 if (needsLoad) {
2523 int FI = MFI->CreateFixedObject(ObjSize,
2524 CurArgOffset + (ArgSize - ObjSize),
2525 isImmutable);
2526 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2527 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2528 false, false, false, 0);
2529 }
2530
2531 InVals.push_back(ArgVal);
2532 }
2533
2534 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002535 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002536 // taking the difference between two stack areas will result in an aligned
2537 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002538 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002539
2540 // If the function takes variable number of arguments, make a frame index for
2541 // the start of the first vararg value... for expansion of llvm.va_start.
2542 if (isVarArg) {
2543 int Depth = ArgOffset;
2544
2545 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00002546 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002547 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2548
2549 // If this function is vararg, store any remaining integer argument regs
2550 // to their spots on the stack so that they may be loaded by deferencing the
2551 // result of va_next.
2552 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2553 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2554 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2555 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2556 MachinePointerInfo(), false, false, 0);
2557 MemOps.push_back(Store);
2558 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00002559 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002560 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2561 }
2562 }
2563
2564 if (!MemOps.empty())
2565 Chain = DAG.getNode(ISD::TokenFactor, dl,
2566 MVT::Other, &MemOps[0], MemOps.size());
2567
2568 return Chain;
2569}
2570
2571SDValue
2572PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002573 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002574 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002575 const SmallVectorImpl<ISD::InputArg>
2576 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002577 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002578 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002579 // TODO: add description of PPC stack frame format, or at least some docs.
2580 //
2581 MachineFunction &MF = DAG.getMachineFunction();
2582 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002583 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002584
Owen Anderson53aa7a92009-08-10 22:56:29 +00002585 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002586 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002587 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002588 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2589 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00002590 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey48850c12006-11-16 22:43:37 +00002591
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002592 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002593 // Area that is at least reserved in caller of this function.
2594 unsigned MinReservedArea = ArgOffset;
2595
Craig Topperca658c22012-03-11 07:16:55 +00002596 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002597 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2598 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2599 };
Craig Topperca658c22012-03-11 07:16:55 +00002600 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00002601 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2602 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2603 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00002604
Craig Topperca658c22012-03-11 07:16:55 +00002605 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002606
Craig Topperca658c22012-03-11 07:16:55 +00002607 static const uint16_t VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002608 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2609 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2610 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00002611
Owen Andersone2f23a32007-09-07 04:06:50 +00002612 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002613 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00002614 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00002615
2616 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002617
Craig Topperca658c22012-03-11 07:16:55 +00002618 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002619
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002620 // In 32-bit non-varargs functions, the stack space for vectors is after the
2621 // stack space for non-vectors. We do not use this space unless we have
2622 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00002623 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002624 // that out...for the pathological case, compute VecArgOffset as the
2625 // start of the vector parameter area. Computing VecArgOffset is the
2626 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002627 unsigned VecArgOffset = ArgOffset;
2628 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002629 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002630 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002631 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002632 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002633
Duncan Sandsd97eea32008-03-21 09:14:45 +00002634 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002635 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00002636 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002637 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002638 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2639 VecArgOffset += ArgSize;
2640 continue;
2641 }
2642
Owen Anderson9f944592009-08-11 20:47:22 +00002643 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002644 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002645 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002646 case MVT::i32:
2647 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002648 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002649 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002650 case MVT::i64: // PPC64
2651 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002652 // FIXME: We are guaranteed to be !isPPC64 at this point.
2653 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002654 VecArgOffset += 8;
2655 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002656 case MVT::v4f32:
2657 case MVT::v4i32:
2658 case MVT::v8i16:
2659 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002660 // Nothing to do, we're only looking at Nonvector args here.
2661 break;
2662 }
2663 }
2664 }
2665 // We've found where the vector parameter area in memory is. Skip the
2666 // first 12 parameters; these don't use that memory.
2667 VecArgOffset = ((VecArgOffset+15)/16)*16;
2668 VecArgOffset += 12*16;
2669
Chris Lattner4302e8f2006-05-16 18:18:50 +00002670 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00002671 // entry to a function on PPC, the arguments start after the linkage area,
2672 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00002673
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002674 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002675 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00002676 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002677 unsigned CurArgIdx = 0;
2678 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002679 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002680 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002681 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00002682 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00002683 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002684 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002685 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2686 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002687
Chris Lattner318f0d22006-05-16 18:51:52 +00002688 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002689
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002690 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002691 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2692 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002693 if (isVarArg || isPPC64) {
2694 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002695 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002696 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002697 PtrByteSize);
2698 } else nAltivecParamsAtEnd++;
2699 } else
2700 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002701 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002702 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002703 PtrByteSize);
2704
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002705 // FIXME the codegen can be much improved in some cases.
2706 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002707 if (Flags.isByVal()) {
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002708 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002709 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002710 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002711 // Objects of size 1 and 2 are right justified, everything else is
2712 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00002713 if (ObjSize==1 || ObjSize==2) {
2714 CurArgOffset = CurArgOffset + (4 - ObjSize);
2715 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002716 // The value of the object is its address.
Evan Cheng0664a672010-07-03 00:40:23 +00002717 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002718 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002719 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002720 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00002721 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002722 unsigned VReg;
2723 if (isPPC64)
2724 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2725 else
2726 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002727 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002728 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002729 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002730 MachinePointerInfo(FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002731 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00002732 MemOps.push_back(Store);
2733 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00002734 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002735
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002736 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00002737
Dale Johannesen21a8f142008-03-08 01:41:42 +00002738 continue;
2739 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002740 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2741 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002742 // to memory. ArgOffset will be the address of the beginning
2743 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002744 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002745 unsigned VReg;
2746 if (isPPC64)
2747 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2748 else
2749 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00002750 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002751 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002752 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002753 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002754 MachinePointerInfo(FuncArg, j),
David Greene87a5abe2010-02-15 16:56:53 +00002755 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002756 MemOps.push_back(Store);
2757 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002758 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002759 } else {
2760 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2761 break;
2762 }
2763 }
2764 continue;
2765 }
2766
Owen Anderson9f944592009-08-11 20:47:22 +00002767 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002768 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002769 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002770 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00002771 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00002772 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002773 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002774 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Bill Wendling968f32c2008-03-07 20:49:02 +00002775 ++GPR_idx;
2776 } else {
2777 needsLoad = true;
2778 ArgSize = PtrByteSize;
2779 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002780 // All int arguments reserve stack space in the Darwin ABI.
2781 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00002782 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002783 }
Bill Wendling968f32c2008-03-07 20:49:02 +00002784 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00002785 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00002786 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002787 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002788 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00002789
Hal Finkel940ab932014-02-28 00:27:01 +00002790 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00002791 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00002792 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002793 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00002794
Chris Lattnerec78cad2006-06-26 22:48:35 +00002795 ++GPR_idx;
2796 } else {
2797 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00002798 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002799 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002800 // All int arguments reserve stack space in the Darwin ABI.
2801 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002802 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002803
Owen Anderson9f944592009-08-11 20:47:22 +00002804 case MVT::f32:
2805 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00002806 // Every 4 bytes of argument space consumes one of the GPRs available for
2807 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002808 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002809 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00002810 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002811 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00002812 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002813 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002814 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002815
Owen Anderson9f944592009-08-11 20:47:22 +00002816 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00002817 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002818 else
Devang Patelf3292b22011-02-21 23:21:26 +00002819 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002820
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002821 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002822 ++FPR_idx;
2823 } else {
2824 needsLoad = true;
2825 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002826
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002827 // All FP arguments reserve stack space in the Darwin ABI.
2828 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002829 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002830 case MVT::v4f32:
2831 case MVT::v4i32:
2832 case MVT::v8i16:
2833 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00002834 // Note that vector arguments in registers don't reserve stack space,
2835 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002836 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002837 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002838 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00002839 if (isVarArg) {
2840 while ((ArgOffset % 16) != 0) {
2841 ArgOffset += PtrByteSize;
2842 if (GPR_idx != Num_GPR_Regs)
2843 GPR_idx++;
2844 }
2845 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002846 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00002847 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002848 ++VR_idx;
2849 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002850 if (!isVarArg && !isPPC64) {
2851 // Vectors go after all the nonvectors.
2852 CurArgOffset = VecArgOffset;
2853 VecArgOffset += 16;
2854 } else {
2855 // Vectors are aligned.
2856 ArgOffset = ((ArgOffset+15)/16)*16;
2857 CurArgOffset = ArgOffset;
2858 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00002859 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002860 needsLoad = true;
2861 }
2862 break;
2863 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002864
Chris Lattner4302e8f2006-05-16 18:18:50 +00002865 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002866 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002867 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002868 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002869 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00002870 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002871 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002872 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002873 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002874 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002875
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002876 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002877 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002878
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002879 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002880 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002881 // taking the difference between two stack areas will result in an aligned
2882 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002883 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002884
Chris Lattner4302e8f2006-05-16 18:18:50 +00002885 // If the function takes variable number of arguments, make a frame index for
2886 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002887 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002888 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002889
Dan Gohman31ae5862010-04-17 14:41:14 +00002890 FuncInfo->setVarArgsFrameIndex(
2891 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002892 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00002893 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002894
Chris Lattner4302e8f2006-05-16 18:18:50 +00002895 // If this function is vararg, store any remaining integer argument regs
2896 // to their spots on the stack so that they may be loaded by deferencing the
2897 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002898 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00002899 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00002900
Chris Lattner2cca3852006-11-18 01:57:19 +00002901 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00002902 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00002903 else
Devang Patelf3292b22011-02-21 23:21:26 +00002904 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00002905
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002906 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002907 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2908 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002909 MemOps.push_back(Store);
2910 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002911 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00002912 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002913 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002914 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002915
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002916 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002917 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002918 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002919
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002920 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002921}
2922
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002923/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
2924/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002925static unsigned
2926CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2927 bool isPPC64,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002928 bool isVarArg,
2929 unsigned CC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002930 const SmallVectorImpl<ISD::OutputArg>
2931 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002932 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002933 unsigned &nAltivecParamsAtEnd) {
2934 // Count how many bytes are to be pushed on the stack, including the linkage
2935 // area, and parameter passing area. We start with 24/48 bytes, which is
2936 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002937 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002938 unsigned NumOps = Outs.size();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002939 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2940
2941 // Add up all the space actually used.
2942 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2943 // they all go in registers, but we must reserve stack space for them for
2944 // possible use by the caller. In varargs or 64-bit calls, parameters are
2945 // assigned stack space in order, with padding so Altivec parameters are
2946 // 16-byte aligned.
2947 nAltivecParamsAtEnd = 0;
2948 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002949 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002950 EVT ArgVT = Outs[i].VT;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002951 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002952 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2953 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002954 if (!isVarArg && !isPPC64) {
2955 // Non-varargs Altivec parameters go after all the non-Altivec
2956 // parameters; handle those later so we know how much padding we need.
2957 nAltivecParamsAtEnd++;
2958 continue;
2959 }
2960 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2961 NumBytes = ((NumBytes+15)/16)*16;
2962 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002963 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002964 }
2965
2966 // Allow for Altivec parameters at the end, if needed.
2967 if (nAltivecParamsAtEnd) {
2968 NumBytes = ((NumBytes+15)/16)*16;
2969 NumBytes += 16*nAltivecParamsAtEnd;
2970 }
2971
2972 // The prolog code of the callee may store up to 8 GPR argument registers to
2973 // the stack, allowing va_start to index over them in memory if its varargs.
2974 // Because we cannot tell if this is needed on the caller side, we have to
2975 // conservatively assume that it is needed. As such, make sure we have at
2976 // least enough stack space for the caller to store the 8 GPRs.
2977 NumBytes = std::max(NumBytes,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002978 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002979
2980 // Tail call needs the stack to be aligned.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002981 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
2982 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
2983 getFrameLowering()->getStackAlignment();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002984 unsigned AlignMask = TargetAlign-1;
2985 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2986 }
2987
2988 return NumBytes;
2989}
2990
2991/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00002992/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00002993static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002994 unsigned ParamSize) {
2995
Dale Johannesen86dcae12009-11-24 01:09:07 +00002996 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002997
2998 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
2999 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3000 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3001 // Remember only if the new adjustement is bigger.
3002 if (SPDiff < FI->getTailCallSPDelta())
3003 FI->setTailCallSPDelta(SPDiff);
3004
3005 return SPDiff;
3006}
3007
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003008/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3009/// for tail call optimization. Targets which want to do tail call
3010/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003011bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003012PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003013 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003014 bool isVarArg,
3015 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003016 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003017 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00003018 return false;
3019
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003020 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003021 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003022 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003023
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003024 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00003025 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003026 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
3027 // Functions containing by val parameters are not supported.
3028 for (unsigned i = 0; i != Ins.size(); i++) {
3029 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3030 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003031 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003032
Alp Tokerf907b892013-12-05 05:44:44 +00003033 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003034 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
3035 return true;
3036
3037 // At the moment we can only do local tail calls (in same module, hidden
3038 // or protected) if we are generating PIC.
3039 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
3040 return G->getGlobal()->hasHiddenVisibility()
3041 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003042 }
3043
3044 return false;
3045}
3046
Chris Lattnereb755fc2006-05-17 19:00:46 +00003047/// isCallCompatibleAddress - Return the immediate to use if the specified
3048/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003049static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00003050 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3051 if (!C) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003052
Dan Gohmaneffb8942008-09-12 16:56:44 +00003053 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003054 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00003055 SignExtend32<26>(Addr) != Addr)
Chris Lattnereb755fc2006-05-17 19:00:46 +00003056 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003057
Dan Gohmaneffb8942008-09-12 16:56:44 +00003058 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00003059 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003060}
3061
Dan Gohmand78c4002008-05-13 00:00:25 +00003062namespace {
3063
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003064struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003065 SDValue Arg;
3066 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003067 int FrameIdx;
3068
3069 TailCallArgumentInfo() : FrameIdx(0) {}
3070};
3071
Dan Gohmand78c4002008-05-13 00:00:25 +00003072}
3073
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003074/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3075static void
3076StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003077 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003078 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3079 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003080 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003081 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003082 SDValue Arg = TailCallArgs[i].Arg;
3083 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003084 int FI = TailCallArgs[i].FrameIdx;
3085 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003086 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003087 MachinePointerInfo::getFixedStack(FI),
3088 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003089 }
3090}
3091
3092/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3093/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003094static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003095 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003096 SDValue Chain,
3097 SDValue OldRetAddr,
3098 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003099 int SPDiff,
3100 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003101 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003102 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003103 if (SPDiff) {
3104 // Calculate the new stack slot for the return address.
3105 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003106 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003107 isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003108 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003109 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003110 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003111 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003112 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003113 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003114 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003115
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003116 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3117 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003118 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003119 int NewFPLoc =
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003120 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene1fbe0542009-11-12 20:49:22 +00003121 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003122 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003123 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3124 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003125 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003126 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003127 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003128 }
3129 return Chain;
3130}
3131
3132/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3133/// the position of the argument.
3134static void
3135CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003136 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003137 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003138 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003139 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003140 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003141 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003142 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003143 TailCallArgumentInfo Info;
3144 Info.Arg = Arg;
3145 Info.FrameIdxOp = FIN;
3146 Info.FrameIdx = FI;
3147 TailCallArguments.push_back(Info);
3148}
3149
3150/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3151/// stack slot. Returns the chain as result and the loaded frame pointers in
3152/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003153SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003154 int SPDiff,
3155 SDValue Chain,
3156 SDValue &LROpOut,
3157 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003158 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003159 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003160 if (SPDiff) {
3161 // Load the LR and FP stack slot for later adjusting.
Owen Anderson9f944592009-08-11 20:47:22 +00003162 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003163 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003164 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003165 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003166 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003167
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003168 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3169 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003170 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003171 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003172 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003173 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003174 Chain = SDValue(FPOpOut.getNode(), 1);
3175 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003176 }
3177 return Chain;
3178}
3179
Dale Johannesen85d41a12008-03-04 23:17:14 +00003180/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003181/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003182/// specified by the specific parameter attribute. The copy will be passed as
3183/// a byval function parameter.
3184/// Sometimes what we are copying is the end of a larger object, the part that
3185/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003186static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003187CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003188 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003189 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003190 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003191 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Chris Lattner2510de22010-09-21 05:40:29 +00003192 false, false, MachinePointerInfo(0),
3193 MachinePointerInfo(0));
Dale Johannesen85d41a12008-03-04 23:17:14 +00003194}
Chris Lattner43df5b32007-02-25 05:34:32 +00003195
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003196/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3197/// tail calls.
3198static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003199LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3200 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003201 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003202 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3203 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003204 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003205 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003206 if (!isTailCall) {
3207 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003208 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003209 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003210 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003211 else
Owen Anderson9f944592009-08-11 20:47:22 +00003212 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003213 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003214 DAG.getConstant(ArgOffset, PtrVT));
3215 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003216 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3217 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003218 // Calculate and remember argument location.
3219 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3220 TailCallArguments);
3221}
3222
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003223static
3224void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003225 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003226 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003227 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003228 MachineFunction &MF = DAG.getMachineFunction();
3229
3230 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3231 // might overwrite each other in case of tail call optimization.
3232 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003233 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003234 InFlag = SDValue();
3235 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3236 MemOpChains2, dl);
3237 if (!MemOpChains2.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003238 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003239 &MemOpChains2[0], MemOpChains2.size());
3240
3241 // Store the return address to the appropriate stack slot.
3242 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3243 isPPC64, isDarwinABI, dl);
3244
3245 // Emit callseq_end just before tailcall node.
3246 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003247 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003248 InFlag = Chain.getValue(1);
3249}
3250
3251static
3252unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003253 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003254 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3255 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003256 const PPCSubtarget &PPCSubTarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003257
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003258 bool isPPC64 = PPCSubTarget.isPPC64();
3259 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3260
Owen Anderson53aa7a92009-08-10 22:56:29 +00003261 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003262 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003263 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003264
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003265 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003266
Torok Edwin31e90d22010-08-04 20:47:44 +00003267 bool needIndirectCall = true;
3268 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003269 // If this is an absolute destination address, use the munged value.
3270 Callee = SDValue(Dest, 0);
Torok Edwin31e90d22010-08-04 20:47:44 +00003271 needIndirectCall = false;
3272 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003273
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003274 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3275 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3276 // Use indirect calls for ALL functions calls in JIT mode, since the
3277 // far-call stubs may be outside relocation limits for a BL instruction.
3278 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3279 unsigned OpFlags = 0;
3280 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003281 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003282 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003283 (G->getGlobal()->isDeclaration() ||
3284 G->getGlobal()->isWeakForLinker())) {
3285 // PC-relative references to external symbols should go through $stub,
3286 // unless we're building with the leopard linker or later, which
3287 // automatically synthesizes these stubs.
3288 OpFlags = PPCII::MO_DARWIN_STUB;
3289 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003290
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003291 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3292 // every direct call is) turn it into a TargetGlobalAddress /
3293 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin31e90d22010-08-04 20:47:44 +00003294 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003295 Callee.getValueType(),
3296 0, OpFlags);
Torok Edwin31e90d22010-08-04 20:47:44 +00003297 needIndirectCall = false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003298 }
Torok Edwin31e90d22010-08-04 20:47:44 +00003299 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003300
Torok Edwin31e90d22010-08-04 20:47:44 +00003301 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003302 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003303
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003304 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003305 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003306 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003307 // PC-relative references to external symbols should go through $stub,
3308 // unless we're building with the leopard linker or later, which
3309 // automatically synthesizes these stubs.
3310 OpFlags = PPCII::MO_DARWIN_STUB;
3311 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003312
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003313 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3314 OpFlags);
3315 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003316 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003317
Torok Edwin31e90d22010-08-04 20:47:44 +00003318 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003319 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3320 // to do the call, we can't use PPCISD::CALL.
3321 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003322
3323 if (isSVR4ABI && isPPC64) {
3324 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3325 // entry point, but to the function descriptor (the function entry point
3326 // address is part of the function descriptor though).
3327 // The function descriptor is a three doubleword structure with the
3328 // following fields: function entry point, TOC base address and
3329 // environment pointer.
3330 // Thus for a call through a function pointer, the following actions need
3331 // to be performed:
3332 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003333 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003334 // 2. Load the address of the function entry point from the function
3335 // descriptor.
3336 // 3. Load the TOC of the callee from the function descriptor into r2.
3337 // 4. Load the environment pointer from the function descriptor into
3338 // r11.
3339 // 5. Branch to the function entry point address.
3340 // 6. On return of the callee, the TOC of the caller needs to be
3341 // restored (this is done in FinishCall()).
3342 //
3343 // All those operations are flagged together to ensure that no other
3344 // operations can be scheduled in between. E.g. without flagging the
3345 // operations together, a TOC access in the caller could be scheduled
3346 // between the load of the callee TOC and the branch to the callee, which
3347 // results in the TOC access going through the TOC of the callee instead
3348 // of going through the TOC of the caller, which leads to incorrect code.
3349
3350 // Load the address of the function entry point from the function
3351 // descriptor.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003352 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003353 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, MTCTROps,
3354 InFlag.getNode() ? 3 : 2);
3355 Chain = LoadFuncPtr.getValue(1);
3356 InFlag = LoadFuncPtr.getValue(2);
3357
3358 // Load environment pointer into r11.
3359 // Offset of the environment pointer within the function descriptor.
3360 SDValue PtrOff = DAG.getIntPtrConstant(16);
3361
3362 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3363 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3364 InFlag);
3365 Chain = LoadEnvPtr.getValue(1);
3366 InFlag = LoadEnvPtr.getValue(2);
3367
3368 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3369 InFlag);
3370 Chain = EnvVal.getValue(0);
3371 InFlag = EnvVal.getValue(1);
3372
3373 // Load TOC of the callee into r2. We are using a target-specific load
3374 // with r2 hard coded, because the result of a target-independent load
3375 // would never go directly into r2, since r2 is a reserved register (which
3376 // prevents the register allocator from allocating it), resulting in an
3377 // additional register being allocated and an unnecessary move instruction
3378 // being generated.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003379 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003380 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3381 Callee, InFlag);
3382 Chain = LoadTOCPtr.getValue(0);
3383 InFlag = LoadTOCPtr.getValue(1);
3384
3385 MTCTROps[0] = Chain;
3386 MTCTROps[1] = LoadFuncPtr;
3387 MTCTROps[2] = InFlag;
3388 }
3389
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003390 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
3391 2 + (InFlag.getNode() != 0));
3392 InFlag = Chain.getValue(1);
3393
3394 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00003395 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003396 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003397 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003398 CallOpc = PPCISD::BCTRL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003399 Callee.setNode(0);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003400 // Add use of X11 (holding environment pointer)
3401 if (isSVR4ABI && isPPC64)
3402 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003403 // Add CTR register as callee so a bctr can be emitted later.
3404 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00003405 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003406 }
3407
3408 // If this is a direct call, pass the chain and the callee.
3409 if (Callee.getNode()) {
3410 Ops.push_back(Chain);
3411 Ops.push_back(Callee);
3412 }
3413 // If this is a tail call add stack pointer delta.
3414 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00003415 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003416
3417 // Add argument registers to the end of the list so that they are known live
3418 // into the call.
3419 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3420 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3421 RegsToPass[i].second.getValueType()));
3422
3423 return CallOpc;
3424}
3425
Roman Divacky76293062012-09-18 16:47:58 +00003426static
3427bool isLocalCall(const SDValue &Callee)
3428{
3429 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00003430 return !G->getGlobal()->isDeclaration() &&
3431 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00003432 return false;
3433}
3434
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003435SDValue
3436PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003437 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003438 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003439 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003440 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003441
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003442 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003443 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003444 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003445 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003446
3447 // Copy all of the result registers out of their specified physreg.
3448 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3449 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003450 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00003451
3452 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3453 VA.getLocReg(), VA.getLocVT(), InFlag);
3454 Chain = Val.getValue(1);
3455 InFlag = Val.getValue(2);
3456
3457 switch (VA.getLocInfo()) {
3458 default: llvm_unreachable("Unknown loc info!");
3459 case CCValAssign::Full: break;
3460 case CCValAssign::AExt:
3461 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3462 break;
3463 case CCValAssign::ZExt:
3464 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3465 DAG.getValueType(VA.getValVT()));
3466 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3467 break;
3468 case CCValAssign::SExt:
3469 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3470 DAG.getValueType(VA.getValVT()));
3471 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3472 break;
3473 }
3474
3475 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003476 }
3477
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003478 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003479}
3480
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003481SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00003482PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003483 bool isTailCall, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003484 SelectionDAG &DAG,
3485 SmallVector<std::pair<unsigned, SDValue>, 8>
3486 &RegsToPass,
3487 SDValue InFlag, SDValue Chain,
3488 SDValue &Callee,
3489 int SPDiff, unsigned NumBytes,
3490 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003491 SmallVectorImpl<SDValue> &InVals) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003492 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003493 SmallVector<SDValue, 8> Ops;
3494 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3495 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003496 PPCSubTarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003497
Hal Finkel5ab37802012-08-28 02:10:27 +00003498 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3499 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3500 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3501
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003502 // When performing tail call optimization the callee pops its arguments off
3503 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00003504 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003505 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003506 (CallConv == CallingConv::Fast &&
3507 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003508
Roman Divackyef21be22012-03-06 16:41:49 +00003509 // Add a register mask operand representing the call-preserved registers.
3510 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3511 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3512 assert(Mask && "Missing call preserved mask for calling convention");
3513 Ops.push_back(DAG.getRegisterMask(Mask));
3514
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003515 if (InFlag.getNode())
3516 Ops.push_back(InFlag);
3517
3518 // Emit tail call.
3519 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003520 assert(((Callee.getOpcode() == ISD::Register &&
3521 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3522 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3523 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3524 isa<ConstantSDNode>(Callee)) &&
3525 "Expecting an global address, external symbol, absolute value or register");
3526
Owen Anderson9f944592009-08-11 20:47:22 +00003527 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003528 }
3529
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003530 // Add a NOP immediately after the branch instruction when using the 64-bit
3531 // SVR4 ABI. At link time, if caller and callee are in a different module and
3532 // thus have a different TOC, the call will be replaced with a call to a stub
3533 // function which saves the current TOC, loads the TOC of the callee and
3534 // branches to the callee. The NOP will be replaced with a load instruction
3535 // which restores the TOC of the caller from the TOC save slot of the current
3536 // stack frame. If caller and callee belong to the same module (and have the
3537 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00003538
3539 bool needsTOCRestore = false;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003540 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003541 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003542 // This is a call through a function pointer.
3543 // Restore the caller TOC from the save area into R2.
3544 // See PrepareCall() for more information about calls through function
3545 // pointers in the 64-bit SVR4 ABI.
3546 // We are using a target-specific load with r2 hard coded, because the
3547 // result of a target-independent load would never go directly into r2,
3548 // since r2 is a reserved register (which prevents the register allocator
3549 // from allocating it), resulting in an additional register being
3550 // allocated and an unnecessary move instruction being generated.
Hal Finkel51861b42012-03-31 14:45:15 +00003551 needsTOCRestore = true;
Bill Schmidtcea15962013-09-26 17:09:28 +00003552 } else if ((CallOpc == PPCISD::CALL) &&
3553 (!isLocalCall(Callee) ||
3554 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Roman Divacky76293062012-09-18 16:47:58 +00003555 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003556 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller79fef932009-12-18 13:00:15 +00003557 }
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003558 }
3559
Hal Finkel51861b42012-03-31 14:45:15 +00003560 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
3561 InFlag = Chain.getValue(1);
3562
3563 if (needsTOCRestore) {
3564 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3565 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3566 InFlag = Chain.getValue(1);
3567 }
3568
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003569 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3570 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003571 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003572 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003573 InFlag = Chain.getValue(1);
3574
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003575 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3576 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003577}
3578
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003579SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00003580PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003581 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00003582 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00003583 SDLoc &dl = CLI.DL;
3584 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3585 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3586 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00003587 SDValue Chain = CLI.Chain;
3588 SDValue Callee = CLI.Callee;
3589 bool &isTailCall = CLI.IsTailCall;
3590 CallingConv::ID CallConv = CLI.CallConv;
3591 bool isVarArg = CLI.IsVarArg;
3592
Evan Cheng67a69dd2010-01-27 00:07:07 +00003593 if (isTailCall)
3594 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3595 Ins, DAG);
3596
Bill Schmidt57d6de52012-10-23 15:51:16 +00003597 if (PPCSubTarget.isSVR4ABI()) {
3598 if (PPCSubTarget.isPPC64())
3599 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3600 isTailCall, Outs, OutVals, Ins,
3601 dl, DAG, InVals);
3602 else
3603 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3604 isTailCall, Outs, OutVals, Ins,
3605 dl, DAG, InVals);
3606 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003607
Bill Schmidt57d6de52012-10-23 15:51:16 +00003608 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3609 isTailCall, Outs, OutVals, Ins,
3610 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003611}
3612
3613SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003614PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3615 CallingConv::ID CallConv, bool isVarArg,
3616 bool isTailCall,
3617 const SmallVectorImpl<ISD::OutputArg> &Outs,
3618 const SmallVectorImpl<SDValue> &OutVals,
3619 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003620 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003621 SmallVectorImpl<SDValue> &InVals) const {
3622 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003623 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003624
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003625 assert((CallConv == CallingConv::C ||
3626 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003627
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003628 unsigned PtrByteSize = 4;
3629
3630 MachineFunction &MF = DAG.getMachineFunction();
3631
3632 // Mark this function as potentially containing a function that contains a
3633 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3634 // and restoring the callers stack pointer in this functions epilog. This is
3635 // done because by tail calling the called function might overwrite the value
3636 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003637 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3638 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003639 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00003640
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003641 // Count how many bytes are to be pushed on the stack, including the linkage
3642 // area, parameter list area and the part of the local variable space which
3643 // contains copies of aggregates which are passed by value.
3644
3645 // Assign locations to all of the outgoing arguments.
3646 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003647 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003648 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003649
3650 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003651 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003652
3653 if (isVarArg) {
3654 // Handle fixed and variable vector arguments differently.
3655 // Fixed vector arguments go into registers as long as registers are
3656 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003657 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00003658
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003659 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003660 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003661 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003662 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00003663
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003664 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00003665 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3666 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003667 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00003668 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3669 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003670 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003671
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003672 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003673#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00003674 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00003675 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003676#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00003677 llvm_unreachable(0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003678 }
3679 }
3680 } else {
3681 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00003682 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003683 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003684
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003685 // Assign locations to all of the outgoing aggregate by value arguments.
3686 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003687 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003688 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003689
3690 // Reserve stack space for the allocations in CCInfo.
3691 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3692
Bill Schmidtef17c142013-02-06 17:33:58 +00003693 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003694
3695 // Size of the linkage area, parameter list area and the part of the local
3696 // space variable where copies of aggregates which are passed by value are
3697 // stored.
3698 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003699
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003700 // Calculate by how many bytes the stack has to be adjusted in case of tail
3701 // call optimization.
3702 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3703
3704 // Adjust the stack pointer for the new arguments...
3705 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003706 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3707 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003708 SDValue CallSeqStart = Chain;
3709
3710 // Load the return address and frame pointer so it can be moved somewhere else
3711 // later.
3712 SDValue LROp, FPOp;
3713 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3714 dl);
3715
3716 // Set up a copy of the stack pointer for use loading and storing any
3717 // arguments that may not fit in the registers available for argument
3718 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00003719 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00003720
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003721 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3722 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3723 SmallVector<SDValue, 8> MemOpChains;
3724
Roman Divacky71038e72011-08-30 17:04:16 +00003725 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003726 // Walk the register/memloc assignments, inserting copies/loads.
3727 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3728 i != e;
3729 ++i) {
3730 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003731 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003732 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00003733
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003734 if (Flags.isByVal()) {
3735 // Argument is an aggregate which is passed by value, thus we need to
3736 // create a copy of it in the local variable space of the current stack
3737 // frame (which is the stack frame of the caller) and pass the address of
3738 // this copy to the callee.
3739 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3740 CCValAssign &ByValVA = ByValArgLocs[j++];
3741 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00003742
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003743 // Memory reserved in the local variable space of the callers stack frame.
3744 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003745
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003746 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3747 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00003748
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003749 // Create a copy of the argument in the local area of the current
3750 // stack frame.
3751 SDValue MemcpyCall =
3752 CreateCopyOfByValArgument(Arg, PtrOff,
3753 CallSeqStart.getNode()->getOperand(0),
3754 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00003755
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003756 // This must go outside the CALLSEQ_START..END.
3757 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003758 CallSeqStart.getNode()->getOperand(1),
3759 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003760 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3761 NewCallSeqStart.getNode());
3762 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00003763
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003764 // Pass the address of the aggregate copy on the stack either in a
3765 // physical register or in the parameter list area of the current stack
3766 // frame to the callee.
3767 Arg = PtrOff;
3768 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003769
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003770 if (VA.isRegLoc()) {
Roman Divacky71038e72011-08-30 17:04:16 +00003771 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003772 // Put argument in a physical register.
3773 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3774 } else {
3775 // Put argument in the parameter list area of the current stack frame.
3776 assert(VA.isMemLoc());
3777 unsigned LocMemOffset = VA.getLocMemOffset();
3778
3779 if (!isTailCall) {
3780 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3781 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3782
3783 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00003784 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00003785 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003786 } else {
3787 // Calculate and remember argument location.
3788 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3789 TailCallArguments);
3790 }
3791 }
3792 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003793
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003794 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003795 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003796 &MemOpChains[0], MemOpChains.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00003797
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003798 // Build a sequence of copy-to-reg nodes chained together with token chain
3799 // and flag operands which copy the outgoing args into the appropriate regs.
3800 SDValue InFlag;
3801 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3802 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3803 RegsToPass[i].second, InFlag);
3804 InFlag = Chain.getValue(1);
3805 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003806
Hal Finkel5ab37802012-08-28 02:10:27 +00003807 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3808 // registers.
3809 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003810 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3811 SDValue Ops[] = { Chain, InFlag };
3812
Hal Finkel5ab37802012-08-28 02:10:27 +00003813 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003814 dl, VTs, Ops, InFlag.getNode() ? 2 : 1);
3815
Hal Finkel5ab37802012-08-28 02:10:27 +00003816 InFlag = Chain.getValue(1);
3817 }
3818
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003819 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003820 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3821 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003822
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003823 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3824 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3825 Ins, InVals);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003826}
3827
Bill Schmidt57d6de52012-10-23 15:51:16 +00003828// Copy an argument into memory, being careful to do this outside the
3829// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003830SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00003831PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3832 SDValue CallSeqStart,
3833 ISD::ArgFlagsTy Flags,
3834 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003835 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003836 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3837 CallSeqStart.getNode()->getOperand(0),
3838 Flags, DAG, dl);
3839 // The MEMCPY must go outside the CALLSEQ_START..END.
3840 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003841 CallSeqStart.getNode()->getOperand(1),
3842 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00003843 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3844 NewCallSeqStart.getNode());
3845 return NewCallSeqStart;
3846}
3847
3848SDValue
3849PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003850 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003851 bool isTailCall,
3852 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003853 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003854 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003855 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003856 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003857
Bill Schmidt57d6de52012-10-23 15:51:16 +00003858 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003859
Bill Schmidt57d6de52012-10-23 15:51:16 +00003860 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3861 unsigned PtrByteSize = 8;
3862
3863 MachineFunction &MF = DAG.getMachineFunction();
3864
3865 // Mark this function as potentially containing a function that contains a
3866 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3867 // and restoring the callers stack pointer in this functions epilog. This is
3868 // done because by tail calling the called function might overwrite the value
3869 // in this function's (MF) stack pointer stack slot 0(SP).
3870 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3871 CallConv == CallingConv::Fast)
3872 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3873
3874 unsigned nAltivecParamsAtEnd = 0;
3875
3876 // Count how many bytes are to be pushed on the stack, including the linkage
3877 // area, and parameter passing area. We start with at least 48 bytes, which
3878 // is reserved space for [SP][CR][LR][3 x unused].
3879 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
3880 // of this call.
3881 unsigned NumBytes =
3882 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
3883 Outs, OutVals, nAltivecParamsAtEnd);
3884
3885 // Calculate by how many bytes the stack has to be adjusted in case of tail
3886 // call optimization.
3887 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3888
3889 // To protect arguments on the stack from being clobbered in a tail call,
3890 // force all the loads to happen before doing any other lowering.
3891 if (isTailCall)
3892 Chain = DAG.getStackArgumentTokenFactor(Chain);
3893
3894 // Adjust the stack pointer for the new arguments...
3895 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003896 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3897 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003898 SDValue CallSeqStart = Chain;
3899
3900 // Load the return address and frame pointer so it can be move somewhere else
3901 // later.
3902 SDValue LROp, FPOp;
3903 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
3904 dl);
3905
3906 // Set up a copy of the stack pointer for use loading and storing any
3907 // arguments that may not fit in the registers available for argument
3908 // passing.
3909 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
3910
3911 // Figure out which arguments are going to go in registers, and which in
3912 // memory. Also, if this is a vararg function, floating point operations
3913 // must be stored to our stack, and loaded into integer regs as well, if
3914 // any integer regs are available for argument passing.
3915 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
3916 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
3917
3918 static const uint16_t GPR[] = {
3919 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3920 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3921 };
3922 static const uint16_t *FPR = GetFPR();
3923
3924 static const uint16_t VR[] = {
3925 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3926 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3927 };
3928 const unsigned NumGPRs = array_lengthof(GPR);
3929 const unsigned NumFPRs = 13;
3930 const unsigned NumVRs = array_lengthof(VR);
3931
3932 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3933 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3934
3935 SmallVector<SDValue, 8> MemOpChains;
3936 for (unsigned i = 0; i != NumOps; ++i) {
3937 SDValue Arg = OutVals[i];
3938 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3939
3940 // PtrOff will be used to store the current argument to the stack if a
3941 // register cannot be found for it.
3942 SDValue PtrOff;
3943
3944 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
3945
3946 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
3947
3948 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00003949 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003950 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
3951 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3952 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
3953 }
3954
3955 // FIXME memcpy is used way more than necessary. Correctness first.
3956 // Note: "by value" is code for passing a structure by value, not
3957 // basic types.
3958 if (Flags.isByVal()) {
3959 // Note: Size includes alignment padding, so
3960 // struct x { short a; char b; }
3961 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
3962 // These are the proper values we need for right-justifying the
3963 // aggregate in a parameter register.
3964 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00003965
3966 // An empty aggregate parameter takes up no storage and no
3967 // registers.
3968 if (Size == 0)
3969 continue;
3970
Hal Finkel262a2242013-09-12 23:20:06 +00003971 unsigned BVAlign = Flags.getByValAlign();
3972 if (BVAlign > 8) {
3973 if (BVAlign % PtrByteSize != 0)
3974 llvm_unreachable(
3975 "ByVal alignment is not a multiple of the pointer size");
3976
3977 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
3978 }
3979
Bill Schmidt57d6de52012-10-23 15:51:16 +00003980 // All aggregates smaller than 8 bytes must be passed right-justified.
3981 if (Size==1 || Size==2 || Size==4) {
3982 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
3983 if (GPR_idx != NumGPRs) {
3984 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
3985 MachinePointerInfo(), VT,
3986 false, false, 0);
3987 MemOpChains.push_back(Load.getValue(1));
3988 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3989
3990 ArgOffset += PtrByteSize;
3991 continue;
3992 }
3993 }
3994
3995 if (GPR_idx == NumGPRs && Size < 8) {
3996 SDValue Const = DAG.getConstant(PtrByteSize - Size,
3997 PtrOff.getValueType());
3998 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3999 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4000 CallSeqStart,
4001 Flags, DAG, dl);
4002 ArgOffset += PtrByteSize;
4003 continue;
4004 }
4005 // Copy entire object into memory. There are cases where gcc-generated
4006 // code assumes it is there, even if it could be put entirely into
4007 // registers. (This is not what the doc says.)
4008
4009 // FIXME: The above statement is likely due to a misunderstanding of the
4010 // documents. All arguments must be copied into the parameter area BY
4011 // THE CALLEE in the event that the callee takes the address of any
4012 // formal argument. That has not yet been implemented. However, it is
4013 // reasonable to use the stack area as a staging area for the register
4014 // load.
4015
4016 // Skip this for small aggregates, as we will use the same slot for a
4017 // right-justified copy, below.
4018 if (Size >= 8)
4019 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4020 CallSeqStart,
4021 Flags, DAG, dl);
4022
4023 // When a register is available, pass a small aggregate right-justified.
4024 if (Size < 8 && GPR_idx != NumGPRs) {
4025 // The easiest way to get this right-justified in a register
4026 // is to copy the structure into the rightmost portion of a
4027 // local variable slot, then load the whole slot into the
4028 // register.
4029 // FIXME: The memcpy seems to produce pretty awful code for
4030 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00004031 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00004032 // parameter save area instead of a new local variable.
4033 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
4034 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4035 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4036 CallSeqStart,
4037 Flags, DAG, dl);
4038
4039 // Load the slot into the register.
4040 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
4041 MachinePointerInfo(),
4042 false, false, false, 0);
4043 MemOpChains.push_back(Load.getValue(1));
4044 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4045
4046 // Done with this argument.
4047 ArgOffset += PtrByteSize;
4048 continue;
4049 }
4050
4051 // For aggregates larger than PtrByteSize, copy the pieces of the
4052 // object that fit into registers from the parameter save area.
4053 for (unsigned j=0; j<Size; j+=PtrByteSize) {
4054 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
4055 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
4056 if (GPR_idx != NumGPRs) {
4057 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4058 MachinePointerInfo(),
4059 false, false, false, 0);
4060 MemOpChains.push_back(Load.getValue(1));
4061 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4062 ArgOffset += PtrByteSize;
4063 } else {
4064 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4065 break;
4066 }
4067 }
4068 continue;
4069 }
4070
Craig Topper56710102013-08-15 02:33:50 +00004071 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004072 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00004073 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004074 case MVT::i32:
4075 case MVT::i64:
4076 if (GPR_idx != NumGPRs) {
4077 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
4078 } else {
4079 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4080 true, isTailCall, false, MemOpChains,
4081 TailCallArguments, dl);
4082 }
4083 ArgOffset += PtrByteSize;
4084 break;
4085 case MVT::f32:
4086 case MVT::f64:
4087 if (FPR_idx != NumFPRs) {
4088 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4089
4090 if (isVarArg) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004091 // A single float or an aggregate containing only a single float
4092 // must be passed right-justified in the stack doubleword, and
4093 // in the GPR, if one is available.
4094 SDValue StoreOff;
Craig Topper56710102013-08-15 02:33:50 +00004095 if (Arg.getSimpleValueType().SimpleTy == MVT::f32) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004096 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4097 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4098 } else
4099 StoreOff = PtrOff;
4100
4101 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004102 MachinePointerInfo(), false, false, 0);
4103 MemOpChains.push_back(Store);
4104
4105 // Float varargs are always shadowed in available integer registers
4106 if (GPR_idx != NumGPRs) {
4107 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4108 MachinePointerInfo(), false, false,
4109 false, 0);
4110 MemOpChains.push_back(Load.getValue(1));
4111 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4112 }
4113 } else if (GPR_idx != NumGPRs)
4114 // If we have any FPRs remaining, we may also have GPRs remaining.
4115 ++GPR_idx;
4116 } else {
4117 // Single-precision floating-point values are mapped to the
4118 // second (rightmost) word of the stack doubleword.
4119 if (Arg.getValueType() == MVT::f32) {
4120 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4121 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4122 }
4123
4124 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4125 true, isTailCall, false, MemOpChains,
4126 TailCallArguments, dl);
4127 }
4128 ArgOffset += 8;
4129 break;
4130 case MVT::v4f32:
4131 case MVT::v4i32:
4132 case MVT::v8i16:
4133 case MVT::v16i8:
4134 if (isVarArg) {
4135 // These go aligned on the stack, or in the corresponding R registers
4136 // when within range. The Darwin PPC ABI doc claims they also go in
4137 // V registers; in fact gcc does this only for arguments that are
4138 // prototyped, not for those that match the ... We do it for all
4139 // arguments, seems to work.
4140 while (ArgOffset % 16 !=0) {
4141 ArgOffset += PtrByteSize;
4142 if (GPR_idx != NumGPRs)
4143 GPR_idx++;
4144 }
4145 // We could elide this store in the case where the object fits
4146 // entirely in R registers. Maybe later.
4147 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
4148 DAG.getConstant(ArgOffset, PtrVT));
4149 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4150 MachinePointerInfo(), false, false, 0);
4151 MemOpChains.push_back(Store);
4152 if (VR_idx != NumVRs) {
4153 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4154 MachinePointerInfo(),
4155 false, false, false, 0);
4156 MemOpChains.push_back(Load.getValue(1));
4157 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4158 }
4159 ArgOffset += 16;
4160 for (unsigned i=0; i<16; i+=PtrByteSize) {
4161 if (GPR_idx == NumGPRs)
4162 break;
4163 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4164 DAG.getConstant(i, PtrVT));
4165 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4166 false, false, false, 0);
4167 MemOpChains.push_back(Load.getValue(1));
4168 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4169 }
4170 break;
4171 }
4172
4173 // Non-varargs Altivec params generally go in registers, but have
4174 // stack space allocated at the end.
4175 if (VR_idx != NumVRs) {
4176 // Doesn't have GPR space allocated.
4177 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4178 } else {
4179 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4180 true, isTailCall, true, MemOpChains,
4181 TailCallArguments, dl);
4182 ArgOffset += 16;
4183 }
4184 break;
4185 }
4186 }
4187
4188 if (!MemOpChains.empty())
4189 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4190 &MemOpChains[0], MemOpChains.size());
4191
4192 // Check if this is an indirect call (MTCTR/BCTRL).
4193 // See PrepareCall() for more information about calls through function
4194 // pointers in the 64-bit SVR4 ABI.
4195 if (!isTailCall &&
4196 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4197 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4198 !isBLACompatibleAddress(Callee, DAG)) {
4199 // Load r2 into a virtual register and store it to the TOC save area.
4200 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4201 // TOC save area offset.
4202 SDValue PtrOff = DAG.getIntPtrConstant(40);
4203 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4204 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4205 false, false, 0);
4206 // R12 must contain the address of an indirect callee. This does not
4207 // mean the MTCTR instruction must use R12; it's easier to model this
4208 // as an extra parameter, so do that.
4209 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4210 }
4211
4212 // Build a sequence of copy-to-reg nodes chained together with token chain
4213 // and flag operands which copy the outgoing args into the appropriate regs.
4214 SDValue InFlag;
4215 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4216 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4217 RegsToPass[i].second, InFlag);
4218 InFlag = Chain.getValue(1);
4219 }
4220
4221 if (isTailCall)
4222 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4223 FPOp, true, TailCallArguments);
4224
4225 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4226 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4227 Ins, InVals);
4228}
4229
4230SDValue
4231PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4232 CallingConv::ID CallConv, bool isVarArg,
4233 bool isTailCall,
4234 const SmallVectorImpl<ISD::OutputArg> &Outs,
4235 const SmallVectorImpl<SDValue> &OutVals,
4236 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004237 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004238 SmallVectorImpl<SDValue> &InVals) const {
4239
4240 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004241
Owen Anderson53aa7a92009-08-10 22:56:29 +00004242 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00004243 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004244 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004245
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004246 MachineFunction &MF = DAG.getMachineFunction();
4247
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004248 // Mark this function as potentially containing a function that contains a
4249 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4250 // and restoring the callers stack pointer in this functions epilog. This is
4251 // done because by tail calling the called function might overwrite the value
4252 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004253 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4254 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004255 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4256
4257 unsigned nAltivecParamsAtEnd = 0;
4258
Chris Lattneraa40ec12006-05-16 22:56:08 +00004259 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00004260 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00004261 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004262 unsigned NumBytes =
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004263 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004264 Outs, OutVals,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004265 nAltivecParamsAtEnd);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004266
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004267 // Calculate by how many bytes the stack has to be adjusted in case of tail
4268 // call optimization.
4269 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004270
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004271 // To protect arguments on the stack from being clobbered in a tail call,
4272 // force all the loads to happen before doing any other lowering.
4273 if (isTailCall)
4274 Chain = DAG.getStackArgumentTokenFactor(Chain);
4275
Chris Lattnerb7552a82006-05-17 00:15:40 +00004276 // Adjust the stack pointer for the new arguments...
4277 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004278 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4279 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004280 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004281
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004282 // Load the return address and frame pointer so it can be move somewhere else
4283 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004284 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004285 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4286 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004287
Chris Lattnerb7552a82006-05-17 00:15:40 +00004288 // Set up a copy of the stack pointer for use loading and storing any
4289 // arguments that may not fit in the registers available for argument
4290 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004291 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004292 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004293 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004294 else
Owen Anderson9f944592009-08-11 20:47:22 +00004295 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004296
Chris Lattnerb7552a82006-05-17 00:15:40 +00004297 // Figure out which arguments are going to go in registers, and which in
4298 // memory. Also, if this is a vararg function, floating point operations
4299 // must be stored to our stack, and loaded into integer regs as well, if
4300 // any integer regs are available for argument passing.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004301 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004302 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004303
Craig Topperca658c22012-03-11 07:16:55 +00004304 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004305 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4306 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4307 };
Craig Topperca658c22012-03-11 07:16:55 +00004308 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00004309 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4310 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4311 };
Craig Topperca658c22012-03-11 07:16:55 +00004312 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004313
Craig Topperca658c22012-03-11 07:16:55 +00004314 static const uint16_t VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004315 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4316 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4317 };
Owen Andersone2f23a32007-09-07 04:06:50 +00004318 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004319 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00004320 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004321
Craig Topperca658c22012-03-11 07:16:55 +00004322 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004323
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004324 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004325 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4326
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004327 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00004328 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004329 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004330 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004331
Chris Lattnerb7552a82006-05-17 00:15:40 +00004332 // PtrOff will be used to store the current argument to the stack if a
4333 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004334 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004335
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004336 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004337
Dale Johannesen679073b2009-02-04 02:34:38 +00004338 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004339
4340 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00004341 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00004342 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4343 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00004344 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004345 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00004346
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004347 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004348 // Note: "by value" is code for passing a structure by value, not
4349 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00004350 if (Flags.isByVal()) {
4351 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004352 // Very small objects are passed right-justified. Everything else is
4353 // passed left-justified.
4354 if (Size==1 || Size==2) {
4355 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004356 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00004357 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00004358 MachinePointerInfo(), VT,
4359 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004360 MemOpChains.push_back(Load.getValue(1));
4361 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004362
4363 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004364 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00004365 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4366 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004367 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004368 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4369 CallSeqStart,
4370 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004371 ArgOffset += PtrByteSize;
4372 }
4373 continue;
4374 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004375 // Copy entire object into memory. There are cases where gcc-generated
4376 // code assumes it is there, even if it could be put entirely into
4377 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004378 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4379 CallSeqStart,
4380 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004381
4382 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4383 // copy the pieces of the object that fit into registers from the
4384 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00004385 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004386 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004387 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00004388 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004389 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4390 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004391 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00004392 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00004393 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004394 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004395 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004396 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004397 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004398 }
4399 }
4400 continue;
4401 }
4402
Craig Topper56710102013-08-15 02:33:50 +00004403 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004404 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00004405 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00004406 case MVT::i32:
4407 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004408 if (GPR_idx != NumGPRs) {
4409 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004410 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004411 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4412 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004413 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00004414 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004415 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004416 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004417 case MVT::f32:
4418 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004419 if (FPR_idx != NumFPRs) {
4420 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4421
Chris Lattnerb7552a82006-05-17 00:15:40 +00004422 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00004423 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4424 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004425 MemOpChains.push_back(Store);
4426
Chris Lattnerb7552a82006-05-17 00:15:40 +00004427 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004428 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004429 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004430 MachinePointerInfo(), false, false,
4431 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004432 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004433 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004434 }
Owen Anderson9f944592009-08-11 20:47:22 +00004435 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004436 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004437 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00004438 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4439 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004440 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004441 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004442 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00004443 }
4444 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00004445 // If we have any FPRs remaining, we may also have GPRs remaining.
4446 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4447 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004448 if (GPR_idx != NumGPRs)
4449 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00004450 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004451 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4452 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004453 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004454 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004455 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4456 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004457 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004458 if (isPPC64)
4459 ArgOffset += 8;
4460 else
Owen Anderson9f944592009-08-11 20:47:22 +00004461 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004462 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004463 case MVT::v4f32:
4464 case MVT::v4i32:
4465 case MVT::v8i16:
4466 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00004467 if (isVarArg) {
4468 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00004469 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00004470 // V registers; in fact gcc does this only for arguments that are
4471 // prototyped, not for those that match the ... We do it for all
4472 // arguments, seems to work.
4473 while (ArgOffset % 16 !=0) {
4474 ArgOffset += PtrByteSize;
4475 if (GPR_idx != NumGPRs)
4476 GPR_idx++;
4477 }
4478 // We could elide this store in the case where the object fits
4479 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004480 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004481 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00004482 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4483 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004484 MemOpChains.push_back(Store);
4485 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004486 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00004487 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004488 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004489 MemOpChains.push_back(Load.getValue(1));
4490 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4491 }
4492 ArgOffset += 16;
4493 for (unsigned i=0; i<16; i+=PtrByteSize) {
4494 if (GPR_idx == NumGPRs)
4495 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00004496 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004497 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00004498 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004499 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004500 MemOpChains.push_back(Load.getValue(1));
4501 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4502 }
4503 break;
4504 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004505
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004506 // Non-varargs Altivec params generally go in registers, but have
4507 // stack space allocated at the end.
4508 if (VR_idx != NumVRs) {
4509 // Doesn't have GPR space allocated.
4510 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4511 } else if (nAltivecParamsAtEnd==0) {
4512 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004513 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4514 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004515 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004516 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00004517 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00004518 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004519 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00004520 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004521 // If all Altivec parameters fit in registers, as they usually do,
4522 // they get stack space following the non-Altivec parameters. We
4523 // don't track this here because nobody below needs it.
4524 // If there are more Altivec parameters than fit in registers emit
4525 // the stores here.
4526 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4527 unsigned j = 0;
4528 // Offset is aligned; skip 1st 12 params which go in V registers.
4529 ArgOffset = ((ArgOffset+15)/16)*16;
4530 ArgOffset += 12*16;
4531 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004532 SDValue Arg = OutVals[i];
4533 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00004534 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4535 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004536 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004537 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004538 // We are emitting Altivec params in order.
4539 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4540 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004541 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004542 ArgOffset += 16;
4543 }
4544 }
4545 }
4546 }
4547
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004548 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00004549 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnered728e82006-08-11 17:38:39 +00004550 &MemOpChains[0], MemOpChains.size());
Scott Michelcf0da6c2009-02-17 22:15:04 +00004551
Dale Johannesen90eab672010-03-09 20:15:42 +00004552 // On Darwin, R12 must contain the address of an indirect callee. This does
4553 // not mean the MTCTR instruction must use R12; it's easier to model this as
4554 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004555 if (!isTailCall &&
Dale Johannesen90eab672010-03-09 20:15:42 +00004556 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4557 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4558 !isBLACompatibleAddress(Callee, DAG))
4559 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4560 PPC::R12), Callee));
4561
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004562 // Build a sequence of copy-to-reg nodes chained together with token chain
4563 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004564 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004565 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00004566 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00004567 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004568 InFlag = Chain.getValue(1);
4569 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004570
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004571 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004572 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4573 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004574
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004575 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4576 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4577 Ins, InVals);
Chris Lattneraa40ec12006-05-16 22:56:08 +00004578}
4579
Hal Finkel450128a2011-10-14 19:51:36 +00004580bool
4581PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4582 MachineFunction &MF, bool isVarArg,
4583 const SmallVectorImpl<ISD::OutputArg> &Outs,
4584 LLVMContext &Context) const {
4585 SmallVector<CCValAssign, 16> RVLocs;
4586 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4587 RVLocs, Context);
4588 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4589}
4590
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004591SDValue
4592PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004593 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004594 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004595 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004596 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004597
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004598 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004599 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00004600 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004601 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004602
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004603 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004604 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004605
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004606 // Copy the result values into the output registers.
4607 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4608 CCValAssign &VA = RVLocs[i];
4609 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004610
4611 SDValue Arg = OutVals[i];
4612
4613 switch (VA.getLocInfo()) {
4614 default: llvm_unreachable("Unknown loc info!");
4615 case CCValAssign::Full: break;
4616 case CCValAssign::AExt:
4617 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4618 break;
4619 case CCValAssign::ZExt:
4620 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4621 break;
4622 case CCValAssign::SExt:
4623 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4624 break;
4625 }
4626
4627 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004628 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004629 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004630 }
4631
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004632 RetOps[0] = Chain; // Update chain.
4633
4634 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00004635 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004636 RetOps.push_back(Flag);
4637
4638 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other,
4639 &RetOps[0], RetOps.size());
Chris Lattner4211ca92006-04-14 06:01:58 +00004640}
4641
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004642SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004643 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00004644 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004645 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004646
Jim Laskeye4f4d042006-12-04 22:04:42 +00004647 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004648 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00004649
4650 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00004651 bool isPPC64 = Subtarget.isPPC64();
4652 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004653 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004654
4655 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004656 SDValue Chain = Op.getOperand(0);
4657 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004658
Jim Laskeye4f4d042006-12-04 22:04:42 +00004659 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00004660 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4661 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004662 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004663
Jim Laskeye4f4d042006-12-04 22:04:42 +00004664 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00004665 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004666
Jim Laskeye4f4d042006-12-04 22:04:42 +00004667 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00004668 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004669 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004670}
4671
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004672
4673
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004674SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004675PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004676 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004677 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004678 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004679 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004680
4681 // Get current frame pointer save index. The users of this index will be
4682 // primarily DYNALLOC instructions.
4683 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4684 int RASI = FI->getReturnAddrSaveIndex();
4685
4686 // If the frame pointer save index hasn't been defined yet.
4687 if (!RASI) {
4688 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004689 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004690 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004691 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004692 // Save the result.
4693 FI->setReturnAddrSaveIndex(RASI);
4694 }
4695 return DAG.getFrameIndex(RASI, PtrVT);
4696}
4697
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004698SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004699PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4700 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004701 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004702 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004703 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004704
4705 // Get current frame pointer save index. The users of this index will be
4706 // primarily DYNALLOC instructions.
4707 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4708 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004709
Jim Laskey48850c12006-11-16 22:43:37 +00004710 // If the frame pointer save index hasn't been defined yet.
4711 if (!FPSI) {
4712 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004713 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004714 isDarwinABI);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004715
Jim Laskey48850c12006-11-16 22:43:37 +00004716 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004717 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00004718 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004719 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00004720 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004721 return DAG.getFrameIndex(FPSI, PtrVT);
4722}
Jim Laskey48850c12006-11-16 22:43:37 +00004723
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004724SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004725 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004726 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004727 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004728 SDValue Chain = Op.getOperand(0);
4729 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004730 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004731
Jim Laskey48850c12006-11-16 22:43:37 +00004732 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004733 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004734 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004735 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00004736 DAG.getConstant(0, PtrVT), Size);
4737 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004738 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00004739 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004740 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00004741 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004742 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey48850c12006-11-16 22:43:37 +00004743}
4744
Hal Finkel756810f2013-03-21 21:37:52 +00004745SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
4746 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004747 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004748 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
4749 DAG.getVTList(MVT::i32, MVT::Other),
4750 Op.getOperand(0), Op.getOperand(1));
4751}
4752
4753SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
4754 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004755 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004756 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
4757 Op.getOperand(0), Op.getOperand(1));
4758}
4759
Hal Finkel940ab932014-02-28 00:27:01 +00004760SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
4761 assert(Op.getValueType() == MVT::i1 &&
4762 "Custom lowering only for i1 loads");
4763
4764 // First, load 8 bits into 32 bits, then truncate to 1 bit.
4765
4766 SDLoc dl(Op);
4767 LoadSDNode *LD = cast<LoadSDNode>(Op);
4768
4769 SDValue Chain = LD->getChain();
4770 SDValue BasePtr = LD->getBasePtr();
4771 MachineMemOperand *MMO = LD->getMemOperand();
4772
4773 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(), Chain,
4774 BasePtr, MVT::i8, MMO);
4775 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
4776
4777 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
4778 return DAG.getMergeValues(Ops, 2, dl);
4779}
4780
4781SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
4782 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
4783 "Custom lowering only for i1 stores");
4784
4785 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
4786
4787 SDLoc dl(Op);
4788 StoreSDNode *ST = cast<StoreSDNode>(Op);
4789
4790 SDValue Chain = ST->getChain();
4791 SDValue BasePtr = ST->getBasePtr();
4792 SDValue Value = ST->getValue();
4793 MachineMemOperand *MMO = ST->getMemOperand();
4794
4795 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(), Value);
4796 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
4797}
4798
4799// FIXME: Remove this once the ANDI glue bug is fixed:
4800SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
4801 assert(Op.getValueType() == MVT::i1 &&
4802 "Custom lowering only for i1 results");
4803
4804 SDLoc DL(Op);
4805 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
4806 Op.getOperand(0));
4807}
4808
Chris Lattner4211ca92006-04-14 06:01:58 +00004809/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4810/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004811SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00004812 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00004813 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4814 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00004815 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004816
Hal Finkel81f87992013-04-07 22:11:09 +00004817 // We might be able to do better than this under some circumstances, but in
4818 // general, fsel-based lowering of select is a finite-math-only optimization.
4819 // For more information, see section F.3 of the 2.06 ISA specification.
4820 if (!DAG.getTarget().Options.NoInfsFPMath ||
4821 !DAG.getTarget().Options.NoNaNsFPMath)
4822 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004823
Hal Finkel81f87992013-04-07 22:11:09 +00004824 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004825
Owen Anderson53aa7a92009-08-10 22:56:29 +00004826 EVT ResVT = Op.getValueType();
4827 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004828 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4829 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004830 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004831
Chris Lattner4211ca92006-04-14 06:01:58 +00004832 // If the RHS of the comparison is a 0.0, we don't need to do the
4833 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00004834 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00004835 if (isFloatingPointZero(RHS))
4836 switch (CC) {
4837 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004838 case ISD::SETNE:
4839 std::swap(TV, FV);
4840 case ISD::SETEQ:
4841 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4842 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
4843 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
4844 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4845 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4846 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4847 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004848 case ISD::SETULT:
4849 case ISD::SETLT:
4850 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004851 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004852 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00004853 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4854 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004855 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004856 case ISD::SETUGT:
4857 case ISD::SETGT:
4858 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004859 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004860 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00004861 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4862 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004863 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00004864 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004865 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004866
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004867 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00004868 switch (CC) {
4869 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004870 case ISD::SETNE:
4871 std::swap(TV, FV);
4872 case ISD::SETEQ:
4873 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
4874 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4875 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
4876 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
4877 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4878 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4879 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4880 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004881 case ISD::SETULT:
4882 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004883 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004884 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4885 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004886 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004887 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004888 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004889 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004890 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4891 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004892 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004893 case ISD::SETUGT:
4894 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004895 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004896 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4897 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004898 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004899 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004900 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004901 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004902 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4903 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004904 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004905 }
Eli Friedman5806e182009-05-28 04:31:08 +00004906 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00004907}
4908
Chris Lattner57ee7c62007-11-28 18:44:47 +00004909// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen37bc85f2009-06-04 20:53:52 +00004910SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004911 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00004912 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004913 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00004914 if (Src.getValueType() == MVT::f32)
4915 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00004916
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004917 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00004918 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004919 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00004920 case MVT::i32:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00004921 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Hal Finkelf6d45f22013-04-01 17:52:07 +00004922 (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ :
4923 PPCISD::FCTIDZ),
Owen Anderson9f944592009-08-11 20:47:22 +00004924 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00004925 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004926 case MVT::i64:
Hal Finkel3f88d082013-04-01 18:42:58 +00004927 assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) &&
4928 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00004929 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
4930 PPCISD::FCTIDUZ,
4931 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00004932 break;
4933 }
Duncan Sands2a287912008-07-19 16:26:02 +00004934
Chris Lattner4211ca92006-04-14 06:01:58 +00004935 // Convert the FP value to an int value through memory.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004936 bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() &&
4937 (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT());
4938 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
4939 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
4940 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00004941
Chris Lattner06a49542007-10-15 20:14:52 +00004942 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004943 SDValue Chain;
4944 if (i32Stack) {
4945 MachineFunction &MF = DAG.getMachineFunction();
4946 MachineMemOperand *MMO =
4947 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
4948 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
4949 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
4950 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
4951 MVT::i32, MMO);
4952 } else
4953 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
4954 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00004955
4956 // Result is a load from the stack slot. If loading 4 bytes, make sure to
4957 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004958 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00004959 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00004960 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkelf6d45f22013-04-01 17:52:07 +00004961 MPI = MachinePointerInfo();
4962 }
4963
4964 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004965 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00004966}
4967
Hal Finkelf6d45f22013-04-01 17:52:07 +00004968SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004969 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004970 SDLoc dl(Op);
Dan Gohmand6819da2008-03-11 01:59:03 +00004971 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00004972 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004973 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00004974
Hal Finkelf6d45f22013-04-01 17:52:07 +00004975 assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) &&
4976 "UINT_TO_FP is supported only with FPCVT");
4977
4978 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00004979 // Otherwise, convert to double-precision and then round.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004980 unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4981 (Op.getOpcode() == ISD::UINT_TO_FP ?
4982 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
4983 (Op.getOpcode() == ISD::UINT_TO_FP ?
4984 PPCISD::FCFIDU : PPCISD::FCFID);
4985 MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4986 MVT::f32 : MVT::f64;
4987
Owen Anderson9f944592009-08-11 20:47:22 +00004988 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00004989 SDValue SINT = Op.getOperand(0);
4990 // When converting to single-precision, we actually need to convert
4991 // to double-precision first and then round to single-precision.
4992 // To avoid double-rounding effects during that operation, we have
4993 // to prepare the input operand. Bits that might be truncated when
4994 // converting to double-precision are replaced by a bit that won't
4995 // be lost at this stage, but is below the single-precision rounding
4996 // position.
4997 //
4998 // However, if -enable-unsafe-fp-math is in effect, accept double
4999 // rounding to avoid the extra overhead.
5000 if (Op.getValueType() == MVT::f32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005001 !PPCSubTarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005002 !DAG.getTarget().Options.UnsafeFPMath) {
5003
5004 // Twiddle input to make sure the low 11 bits are zero. (If this
5005 // is the case, we are guaranteed the value will fit into the 53 bit
5006 // mantissa of an IEEE double-precision value without rounding.)
5007 // If any of those low 11 bits were not zero originally, make sure
5008 // bit 12 (value 2048) is set instead, so that the final rounding
5009 // to single-precision gets the correct result.
5010 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5011 SINT, DAG.getConstant(2047, MVT::i64));
5012 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
5013 Round, DAG.getConstant(2047, MVT::i64));
5014 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
5015 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5016 Round, DAG.getConstant(-2048, MVT::i64));
5017
5018 // However, we cannot use that value unconditionally: if the magnitude
5019 // of the input value is small, the bit-twiddling we did above might
5020 // end up visibly changing the output. Fortunately, in that case, we
5021 // don't need to twiddle bits since the original input will convert
5022 // exactly to double-precision floating-point already. Therefore,
5023 // construct a conditional to use the original value if the top 11
5024 // bits are all sign-bit copies, and use the rounded value computed
5025 // above otherwise.
5026 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
5027 SINT, DAG.getConstant(53, MVT::i32));
5028 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
5029 Cond, DAG.getConstant(1, MVT::i64));
5030 Cond = DAG.getSetCC(dl, MVT::i32,
5031 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
5032
5033 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
5034 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00005035
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005036 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005037 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
5038
5039 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00005040 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005041 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005042 return FP;
5043 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005044
Owen Anderson9f944592009-08-11 20:47:22 +00005045 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005046 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005047 // Since we only generate this in 64-bit mode, we can take advantage of
5048 // 64-bit registers. In particular, sign extend the input value into the
5049 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
5050 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00005051 MachineFunction &MF = DAG.getMachineFunction();
5052 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005053 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005054
Hal Finkelbeb296b2013-03-31 10:12:51 +00005055 SDValue Ld;
Hal Finkelf6d45f22013-04-01 17:52:07 +00005056 if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) {
Hal Finkelbeb296b2013-03-31 10:12:51 +00005057 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
5058 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005059
Hal Finkelbeb296b2013-03-31 10:12:51 +00005060 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
5061 MachinePointerInfo::getFixedStack(FrameIdx),
5062 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00005063
Hal Finkelbeb296b2013-03-31 10:12:51 +00005064 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
5065 "Expected an i32 store");
5066 MachineMemOperand *MMO =
5067 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
5068 MachineMemOperand::MOLoad, 4, 4);
5069 SDValue Ops[] = { Store, FIdx };
Hal Finkelf6d45f22013-04-01 17:52:07 +00005070 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
5071 PPCISD::LFIWZX : PPCISD::LFIWAX,
5072 dl, DAG.getVTList(MVT::f64, MVT::Other),
5073 Ops, 2, MVT::i32, MMO);
Hal Finkelbeb296b2013-03-31 10:12:51 +00005074 } else {
Hal Finkelf6d45f22013-04-01 17:52:07 +00005075 assert(PPCSubTarget.isPPC64() &&
5076 "i32->FP without LFIWAX supported only on PPC64");
5077
Hal Finkelbeb296b2013-03-31 10:12:51 +00005078 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
5079 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
5080
5081 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
5082 Op.getOperand(0));
5083
5084 // STD the extended value into the stack slot.
5085 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
5086 MachinePointerInfo::getFixedStack(FrameIdx),
5087 false, false, 0);
5088
5089 // Load the value as a double.
5090 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
5091 MachinePointerInfo::getFixedStack(FrameIdx),
5092 false, false, false, 0);
5093 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005094
Chris Lattner4211ca92006-04-14 06:01:58 +00005095 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005096 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
5097 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00005098 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005099 return FP;
5100}
5101
Dan Gohman21cea8a2010-04-17 15:26:15 +00005102SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
5103 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005104 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005105 /*
5106 The rounding mode is in bits 30:31 of FPSR, and has the following
5107 settings:
5108 00 Round to nearest
5109 01 Round to 0
5110 10 Round to +inf
5111 11 Round to -inf
5112
5113 FLT_ROUNDS, on the other hand, expects the following:
5114 -1 Undefined
5115 0 Round to 0
5116 1 Round to nearest
5117 2 Round to +inf
5118 3 Round to -inf
5119
5120 To perform the conversion, we do:
5121 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
5122 */
5123
5124 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005125 EVT VT = Op.getValueType();
5126 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005127 SDValue MFFSreg, InFlag;
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005128
5129 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005130 EVT NodeTys[] = {
5131 MVT::f64, // return register
5132 MVT::Glue // unused in this context
5133 };
Dale Johannesen021052a2009-02-04 20:06:27 +00005134 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005135
5136 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00005137 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005138 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005139 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00005140 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005141
5142 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005143 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005144 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00005145 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005146 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005147
5148 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005149 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00005150 DAG.getNode(ISD::AND, dl, MVT::i32,
5151 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005152 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00005153 DAG.getNode(ISD::SRL, dl, MVT::i32,
5154 DAG.getNode(ISD::AND, dl, MVT::i32,
5155 DAG.getNode(ISD::XOR, dl, MVT::i32,
5156 CWD, DAG.getConstant(3, MVT::i32)),
5157 DAG.getConstant(3, MVT::i32)),
5158 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005159
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005160 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00005161 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005162
Duncan Sands13237ac2008-06-06 12:08:01 +00005163 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00005164 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005165}
5166
Dan Gohman21cea8a2010-04-17 15:26:15 +00005167SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005168 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005169 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005170 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00005171 assert(Op.getNumOperands() == 3 &&
5172 VT == Op.getOperand(1).getValueType() &&
5173 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005174
Chris Lattner601b8652006-09-20 03:47:40 +00005175 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005176 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005177 SDValue Lo = Op.getOperand(0);
5178 SDValue Hi = Op.getOperand(1);
5179 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005180 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005181
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005182 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005183 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005184 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5185 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5186 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5187 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005188 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005189 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5190 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5191 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005192 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005193 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005194}
5195
Dan Gohman21cea8a2010-04-17 15:26:15 +00005196SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005197 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005198 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00005199 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005200 assert(Op.getNumOperands() == 3 &&
5201 VT == Op.getOperand(1).getValueType() &&
5202 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005203
Dan Gohman8d2ead22008-03-07 20:36:53 +00005204 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005205 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005206 SDValue Lo = Op.getOperand(0);
5207 SDValue Hi = Op.getOperand(1);
5208 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005209 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005210
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005211 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005212 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005213 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5214 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5215 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5216 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005217 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005218 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5219 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5220 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005221 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005222 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005223}
5224
Dan Gohman21cea8a2010-04-17 15:26:15 +00005225SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005226 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005227 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005228 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005229 assert(Op.getNumOperands() == 3 &&
5230 VT == Op.getOperand(1).getValueType() &&
5231 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005232
Dan Gohman8d2ead22008-03-07 20:36:53 +00005233 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005234 SDValue Lo = Op.getOperand(0);
5235 SDValue Hi = Op.getOperand(1);
5236 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005237 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005238
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005239 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005240 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005241 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5242 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5243 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5244 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005245 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005246 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5247 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5248 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00005249 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005250 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005251 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005252}
5253
5254//===----------------------------------------------------------------------===//
5255// Vector related lowering.
5256//
5257
Chris Lattner2a099c02006-04-17 06:00:21 +00005258/// BuildSplatI - Build a canonical splati of Val with an element size of
5259/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005260static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005261 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00005262 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005263
Owen Anderson53aa7a92009-08-10 22:56:29 +00005264 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00005265 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00005266 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005267
Owen Anderson9f944592009-08-11 20:47:22 +00005268 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005269
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005270 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5271 if (Val == -1)
5272 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005273
Owen Anderson53aa7a92009-08-10 22:56:29 +00005274 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005275
Chris Lattner2a099c02006-04-17 06:00:21 +00005276 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00005277 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005278 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00005279 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga49de9d2009-02-25 22:49:59 +00005280 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
5281 &Ops[0], Ops.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00005282 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005283}
5284
Hal Finkelcf2e9082013-05-24 23:00:14 +00005285/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5286/// specified intrinsic ID.
5287static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005288 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00005289 EVT DestVT = MVT::Other) {
5290 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5291 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5292 DAG.getConstant(IID, MVT::i32), Op);
5293}
5294
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005295/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00005296/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005297static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005298 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005299 EVT DestVT = MVT::Other) {
5300 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005301 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005302 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005303}
5304
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005305/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5306/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005307static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005308 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005309 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00005310 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005311 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005312 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005313}
5314
5315
Chris Lattner264c9082006-04-17 17:55:10 +00005316/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5317/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005318static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005319 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00005320 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00005321 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5322 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005323
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005324 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00005325 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005326 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00005327 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005328 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00005329}
5330
Chris Lattner19e90552006-04-14 05:19:18 +00005331// If this is a case we can't handle, return null and let the default
5332// expansion code take care of it. If we CAN select this case, and if it
5333// selects to a single instruction, return Op. Otherwise, if we can codegen
5334// this case more efficiently than a constant pool load, lower it to the
5335// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005336SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5337 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005338 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005339 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5340 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00005341
Bob Wilson85cefe82009-03-02 23:24:16 +00005342 // Check if this is a splat of a constant value.
5343 APInt APSplatBits, APSplatUndef;
5344 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005345 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00005346 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00005347 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00005348 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00005349
Bob Wilson530e0382009-03-03 19:26:27 +00005350 unsigned SplatBits = APSplatBits.getZExtValue();
5351 unsigned SplatUndef = APSplatUndef.getZExtValue();
5352 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005353
Bob Wilson530e0382009-03-03 19:26:27 +00005354 // First, handle single instruction cases.
5355
5356 // All zeros?
5357 if (SplatBits == 0) {
5358 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00005359 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5360 SDValue Z = DAG.getConstant(0, MVT::i32);
5361 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00005362 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00005363 }
Bob Wilson530e0382009-03-03 19:26:27 +00005364 return Op;
5365 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00005366
Bob Wilson530e0382009-03-03 19:26:27 +00005367 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5368 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5369 (32-SplatBitSize));
5370 if (SextVal >= -16 && SextVal <= 15)
5371 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005372
5373
Bob Wilson530e0382009-03-03 19:26:27 +00005374 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005375
Bob Wilson530e0382009-03-03 19:26:27 +00005376 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00005377 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5378 // If this value is in the range [17,31] and is odd, use:
5379 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5380 // If this value is in the range [-31,-17] and is odd, use:
5381 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5382 // Note the last two are three-instruction sequences.
5383 if (SextVal >= -32 && SextVal <= 31) {
5384 // To avoid having these optimizations undone by constant folding,
5385 // we convert to a pseudo that will be expanded later into one of
5386 // the above forms.
5387 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt51e79512013-02-20 15:50:31 +00005388 EVT VT = Op.getValueType();
5389 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5390 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5391 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilson530e0382009-03-03 19:26:27 +00005392 }
5393
5394 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5395 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5396 // for fneg/fabs.
5397 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5398 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00005399 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005400
5401 // Make the VSLW intrinsic, computing 0x8000_0000.
5402 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5403 OnesV, DAG, dl);
5404
5405 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00005406 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00005407 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005408 }
5409
5410 // Check to see if this is a wide variety of vsplti*, binop self cases.
5411 static const signed char SplatCsts[] = {
5412 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5413 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5414 };
5415
5416 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5417 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5418 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5419 int i = SplatCsts[idx];
5420
5421 // Figure out what shift amount will be used by altivec if shifted by i in
5422 // this splat size.
5423 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5424
5425 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00005426 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005427 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005428 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5429 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5430 Intrinsic::ppc_altivec_vslw
5431 };
5432 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005433 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005434 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005435
Bob Wilson530e0382009-03-03 19:26:27 +00005436 // vsplti + srl self.
5437 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005438 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005439 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5440 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5441 Intrinsic::ppc_altivec_vsrw
5442 };
5443 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005444 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005445 }
5446
Bob Wilson530e0382009-03-03 19:26:27 +00005447 // vsplti + sra self.
5448 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005449 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005450 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5451 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5452 Intrinsic::ppc_altivec_vsraw
5453 };
5454 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005455 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005456 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005457
Bob Wilson530e0382009-03-03 19:26:27 +00005458 // vsplti + rol self.
5459 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5460 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005461 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005462 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5463 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5464 Intrinsic::ppc_altivec_vrlw
5465 };
5466 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005467 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005468 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005469
Bob Wilson530e0382009-03-03 19:26:27 +00005470 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00005471 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005472 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005473 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00005474 }
Bob Wilson530e0382009-03-03 19:26:27 +00005475 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00005476 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005477 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005478 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00005479 }
Bob Wilson530e0382009-03-03 19:26:27 +00005480 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00005481 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005482 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005483 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5484 }
5485 }
5486
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005487 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00005488}
5489
Chris Lattner071ad012006-04-17 05:28:54 +00005490/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5491/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005492static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005493 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005494 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00005495 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00005496 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00005497 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005498
Chris Lattner071ad012006-04-17 05:28:54 +00005499 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00005500 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 +00005501 OP_VMRGHW,
5502 OP_VMRGLW,
5503 OP_VSPLTISW0,
5504 OP_VSPLTISW1,
5505 OP_VSPLTISW2,
5506 OP_VSPLTISW3,
5507 OP_VSLDOI4,
5508 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00005509 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00005510 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00005511
Chris Lattner071ad012006-04-17 05:28:54 +00005512 if (OpNum == OP_COPY) {
5513 if (LHSID == (1*9+2)*9+3) return LHS;
5514 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5515 return RHS;
5516 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005517
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005518 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005519 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5520 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005521
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005522 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00005523 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005524 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00005525 case OP_VMRGHW:
5526 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5527 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5528 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5529 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5530 break;
5531 case OP_VMRGLW:
5532 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5533 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5534 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5535 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5536 break;
5537 case OP_VSPLTISW0:
5538 for (unsigned i = 0; i != 16; ++i)
5539 ShufIdxs[i] = (i&3)+0;
5540 break;
5541 case OP_VSPLTISW1:
5542 for (unsigned i = 0; i != 16; ++i)
5543 ShufIdxs[i] = (i&3)+4;
5544 break;
5545 case OP_VSPLTISW2:
5546 for (unsigned i = 0; i != 16; ++i)
5547 ShufIdxs[i] = (i&3)+8;
5548 break;
5549 case OP_VSPLTISW3:
5550 for (unsigned i = 0; i != 16; ++i)
5551 ShufIdxs[i] = (i&3)+12;
5552 break;
5553 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005554 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005555 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005556 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005557 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005558 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005559 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00005560 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00005561 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5562 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005563 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00005564 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00005565}
5566
Chris Lattner19e90552006-04-14 05:19:18 +00005567/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5568/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5569/// return the code it can be lowered into. Worst case, it can always be
5570/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005571SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005572 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005573 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005574 SDValue V1 = Op.getOperand(0);
5575 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005576 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005577 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005578
Chris Lattner19e90552006-04-14 05:19:18 +00005579 // Cases that are handled by instructions that take permute immediates
5580 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5581 // selected by the instruction selector.
5582 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005583 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5584 PPC::isSplatShuffleMask(SVOp, 2) ||
5585 PPC::isSplatShuffleMask(SVOp, 4) ||
5586 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5587 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5588 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5589 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5590 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5591 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5592 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5593 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5594 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattner19e90552006-04-14 05:19:18 +00005595 return Op;
5596 }
5597 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005598
Chris Lattner19e90552006-04-14 05:19:18 +00005599 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5600 // and produce a fixed permutation. If any of these match, do not lower to
5601 // VPERM.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005602 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5603 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5604 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5605 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5606 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5607 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5608 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5609 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5610 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattner19e90552006-04-14 05:19:18 +00005611 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005612
Chris Lattner071ad012006-04-17 05:28:54 +00005613 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5614 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005615 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00005616
Chris Lattner071ad012006-04-17 05:28:54 +00005617 unsigned PFIndexes[4];
5618 bool isFourElementShuffle = true;
5619 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5620 unsigned EltNo = 8; // Start out undef.
5621 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005622 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00005623 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005624
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005625 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00005626 if ((ByteSource & 3) != j) {
5627 isFourElementShuffle = false;
5628 break;
5629 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005630
Chris Lattner071ad012006-04-17 05:28:54 +00005631 if (EltNo == 8) {
5632 EltNo = ByteSource/4;
5633 } else if (EltNo != ByteSource/4) {
5634 isFourElementShuffle = false;
5635 break;
5636 }
5637 }
5638 PFIndexes[i] = EltNo;
5639 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005640
5641 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00005642 // perfect shuffle vector to determine if it is cost effective to do this as
5643 // discrete instructions, or whether we should use a vperm.
5644 if (isFourElementShuffle) {
5645 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005646 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00005647 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005648
Chris Lattner071ad012006-04-17 05:28:54 +00005649 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5650 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005651
Chris Lattner071ad012006-04-17 05:28:54 +00005652 // Determining when to avoid vperm is tricky. Many things affect the cost
5653 // of vperm, particularly how many times the perm mask needs to be computed.
5654 // For example, if the perm mask can be hoisted out of a loop or is already
5655 // used (perhaps because there are multiple permutes with the same shuffle
5656 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5657 // the loop requires an extra register.
5658 //
5659 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00005660 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00005661 // available, if this block is within a loop, we should avoid using vperm
5662 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005663 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005664 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005665 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005666
Chris Lattner19e90552006-04-14 05:19:18 +00005667 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5668 // vector that will get spilled to the constant pool.
5669 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005670
Chris Lattner19e90552006-04-14 05:19:18 +00005671 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5672 // that it is in input element units, not in bytes. Convert now.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005673 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005674 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005675
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005676 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005677 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5678 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005679
Chris Lattner19e90552006-04-14 05:19:18 +00005680 for (unsigned j = 0; j != BytesPerElement; ++j)
5681 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson9f944592009-08-11 20:47:22 +00005682 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00005683 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005684
Owen Anderson9f944592009-08-11 20:47:22 +00005685 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga49de9d2009-02-25 22:49:59 +00005686 &ResultMask[0], ResultMask.size());
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005687 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00005688}
5689
Chris Lattner9754d142006-04-18 17:59:36 +00005690/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5691/// altivec comparison. If it is, return true and fill in Opc/isDot with
5692/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005693static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner9754d142006-04-18 17:59:36 +00005694 bool &isDot) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005695 unsigned IntrinsicID =
5696 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00005697 CompareOpc = -1;
5698 isDot = false;
5699 switch (IntrinsicID) {
5700 default: return false;
5701 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00005702 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5703 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5704 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5705 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5706 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5707 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5708 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5709 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5710 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5711 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5712 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5713 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5714 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005715
Chris Lattner4211ca92006-04-14 06:01:58 +00005716 // Normal Comparisons.
5717 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5718 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5719 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5720 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5721 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5722 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5723 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5724 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5725 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5726 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5727 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5728 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5729 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5730 }
Chris Lattner9754d142006-04-18 17:59:36 +00005731 return true;
5732}
5733
5734/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5735/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005736SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005737 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00005738 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5739 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005740 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00005741 int CompareOpc;
5742 bool isDot;
5743 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005744 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005745
Chris Lattner9754d142006-04-18 17:59:36 +00005746 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00005747 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00005748 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00005749 Op.getOperand(1), Op.getOperand(2),
5750 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00005751 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00005752 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005753
Chris Lattner4211ca92006-04-14 06:01:58 +00005754 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005755 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005756 Op.getOperand(2), // LHS
5757 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00005758 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005759 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005760 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00005761 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005762
Chris Lattner4211ca92006-04-14 06:01:58 +00005763 // Now that we have the comparison, emit a copy from the CR to a GPR.
5764 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00005765 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00005766 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00005767 CompNode.getValue(1));
5768
Chris Lattner4211ca92006-04-14 06:01:58 +00005769 // Unpack the result based on how the target uses it.
5770 unsigned BitNo; // Bit # of CR6.
5771 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00005772 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00005773 default: // Can't happen, don't crash on invalid number though.
5774 case 0: // Return the value of the EQ bit of CR6.
5775 BitNo = 0; InvertBit = false;
5776 break;
5777 case 1: // Return the inverted value of the EQ bit of CR6.
5778 BitNo = 0; InvertBit = true;
5779 break;
5780 case 2: // Return the value of the LT bit of CR6.
5781 BitNo = 2; InvertBit = false;
5782 break;
5783 case 3: // Return the inverted value of the LT bit of CR6.
5784 BitNo = 2; InvertBit = true;
5785 break;
5786 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005787
Chris Lattner4211ca92006-04-14 06:01:58 +00005788 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00005789 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5790 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005791 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00005792 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5793 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00005794
Chris Lattner4211ca92006-04-14 06:01:58 +00005795 // If we are supposed to, toggle the bit.
5796 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00005797 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5798 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005799 return Flags;
5800}
5801
Scott Michelcf0da6c2009-02-17 22:15:04 +00005802SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005803 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005804 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00005805 // Create a stack slot that is 16-byte aligned.
5806 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00005807 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00005808 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005809 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005810
Chris Lattner4211ca92006-04-14 06:01:58 +00005811 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00005812 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00005813 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005814 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005815 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00005816 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005817 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005818}
5819
Dan Gohman21cea8a2010-04-17 15:26:15 +00005820SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005821 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005822 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005823 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005824
Owen Anderson9f944592009-08-11 20:47:22 +00005825 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
5826 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005827
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005828 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005829 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005830
Chris Lattner7e4398742006-04-18 03:43:48 +00005831 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00005832 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
5833 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
5834 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005835
Chris Lattner7e4398742006-04-18 03:43:48 +00005836 // Low parts multiplied together, generating 32-bit results (we ignore the
5837 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005838 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00005839 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005840
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005841 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00005842 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00005843 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005844 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005845 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005846 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
5847 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005848 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005849
Owen Anderson9f944592009-08-11 20:47:22 +00005850 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00005851
Chris Lattner96d50482006-04-18 04:28:57 +00005852 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005853 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005854 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005855 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005856
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005857 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005858 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00005859 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005860 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005861
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005862 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005863 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00005864 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005865 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005866
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005867 // Merge the results together.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005868 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005869 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005870 Ops[i*2 ] = 2*i+1;
5871 Ops[i*2+1] = 2*i+1+16;
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005872 }
Owen Anderson9f944592009-08-11 20:47:22 +00005873 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00005874 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005875 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00005876 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005877}
5878
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005879/// LowerOperation - Provide custom lowering hooks for some operations.
5880///
Dan Gohman21cea8a2010-04-17 15:26:15 +00005881SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005882 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005883 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005884 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00005885 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005886 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00005887 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00005888 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005889 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00005890 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
5891 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005892 case ISD::VASTART:
Dan Gohman31ae5862010-04-17 14:41:14 +00005893 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005894
5895 case ISD::VAARG:
Dan Gohman31ae5862010-04-17 14:41:14 +00005896 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00005897
Roman Divackyc3825df2013-07-25 21:36:47 +00005898 case ISD::VACOPY:
5899 return LowerVACOPY(Op, DAG, PPCSubTarget);
5900
Jim Laskeye4f4d042006-12-04 22:04:42 +00005901 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00005902 case ISD::DYNAMIC_STACKALLOC:
5903 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng51096af2008-04-19 01:30:48 +00005904
Hal Finkel756810f2013-03-21 21:37:52 +00005905 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
5906 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
5907
Hal Finkel940ab932014-02-28 00:27:01 +00005908 case ISD::LOAD: return LowerLOAD(Op, DAG);
5909 case ISD::STORE: return LowerSTORE(Op, DAG);
5910 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005911 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005912 case ISD::FP_TO_UINT:
5913 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005914 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00005915 case ISD::UINT_TO_FP:
5916 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00005917 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00005918
Chris Lattner4211ca92006-04-14 06:01:58 +00005919 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00005920 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
5921 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
5922 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00005923
Chris Lattner4211ca92006-04-14 06:01:58 +00005924 // Vector-related lowering.
5925 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5926 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5927 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5928 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005929 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005930
Hal Finkel25c19922013-05-15 21:37:41 +00005931 // For counter-based loop handling.
5932 case ISD::INTRINSIC_W_CHAIN: return SDValue();
5933
Chris Lattnerf6a81562007-12-08 06:59:59 +00005934 // Frame & Return address.
5935 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00005936 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00005937 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005938}
5939
Duncan Sands6ed40142008-12-01 11:39:25 +00005940void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
5941 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005942 SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00005943 const TargetMachine &TM = getTargetMachine();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005944 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00005945 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00005946 default:
Craig Toppere55c5562012-02-07 02:50:20 +00005947 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkel25c19922013-05-15 21:37:41 +00005948 case ISD::INTRINSIC_W_CHAIN: {
5949 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
5950 Intrinsic::ppc_is_decremented_ctr_nonzero)
5951 break;
5952
5953 assert(N->getValueType(0) == MVT::i1 &&
5954 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00005955 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00005956 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
5957 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
5958 N->getOperand(1));
5959
5960 Results.push_back(NewInt);
5961 Results.push_back(NewInt.getValue(1));
5962 break;
5963 }
Roman Divacky4394e682011-06-28 15:30:42 +00005964 case ISD::VAARG: {
5965 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
5966 || TM.getSubtarget<PPCSubtarget>().isPPC64())
5967 return;
5968
5969 EVT VT = N->getValueType(0);
5970
5971 if (VT == MVT::i64) {
5972 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
5973
5974 Results.push_back(NewNode);
5975 Results.push_back(NewNode.getValue(1));
5976 }
5977 return;
5978 }
Duncan Sands6ed40142008-12-01 11:39:25 +00005979 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00005980 assert(N->getValueType(0) == MVT::ppcf128);
5981 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005982 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005983 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00005984 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00005985 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005986 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00005987 DAG.getIntPtrConstant(1));
5988
Ulrich Weigand874fc622013-03-26 10:56:22 +00005989 // Add the two halves of the long double in round-to-zero mode.
5990 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00005991
5992 // We know the low half is about to be thrown away, so just use something
5993 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00005994 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00005995 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00005996 return;
Duncan Sands2a287912008-07-19 16:26:02 +00005997 }
Duncan Sands6ed40142008-12-01 11:39:25 +00005998 case ISD::FP_TO_SINT:
Bill Schmidt41221692013-07-09 18:50:20 +00005999 // LowerFP_TO_INT() can only handle f32 and f64.
6000 if (N->getOperand(0).getValueType() == MVT::ppcf128)
6001 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006002 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00006003 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00006004 }
6005}
6006
6007
Chris Lattner4211ca92006-04-14 06:01:58 +00006008//===----------------------------------------------------------------------===//
6009// Other Lowering Code
6010//===----------------------------------------------------------------------===//
6011
Chris Lattner9b577f12005-08-26 21:23:58 +00006012MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00006013PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman747e55b2009-02-07 16:15:20 +00006014 bool is64bit, unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006015 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesend4eb0522008-08-25 22:34:37 +00006016 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6017
6018 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6019 MachineFunction *F = BB->getParent();
6020 MachineFunction::iterator It = BB;
6021 ++It;
6022
6023 unsigned dest = MI->getOperand(0).getReg();
6024 unsigned ptrA = MI->getOperand(1).getReg();
6025 unsigned ptrB = MI->getOperand(2).getReg();
6026 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006027 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00006028
6029 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6030 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6031 F->insert(It, loopMBB);
6032 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006033 exitMBB->splice(exitMBB->begin(), BB,
6034 llvm::next(MachineBasicBlock::iterator(MI)),
6035 BB->end());
6036 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006037
6038 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006039 unsigned TmpReg = (!BinOpcode) ? incr :
6040 RegInfo.createVirtualRegister(
Dale Johannesenbc698292008-09-02 20:30:23 +00006041 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6042 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006043
6044 // thisMBB:
6045 // ...
6046 // fallthrough --> loopMBB
6047 BB->addSuccessor(loopMBB);
6048
6049 // loopMBB:
6050 // l[wd]arx dest, ptr
6051 // add r0, dest, incr
6052 // st[wd]cx. r0, ptr
6053 // bne- loopMBB
6054 // fallthrough --> exitMBB
6055 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006056 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00006057 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006058 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006059 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
6060 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesend4eb0522008-08-25 22:34:37 +00006061 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006062 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006063 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006064 BB->addSuccessor(loopMBB);
6065 BB->addSuccessor(exitMBB);
6066
6067 // exitMBB:
6068 // ...
6069 BB = exitMBB;
6070 return BB;
6071}
6072
6073MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00006074PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00006075 MachineBasicBlock *BB,
6076 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00006077 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006078 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesena32affb2008-08-28 17:53:09 +00006079 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6080 // In 64 bit mode we have to use 64 bits for addresses, even though the
6081 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
6082 // registers without caring whether they're 32 or 64, but here we're
6083 // doing actual arithmetic on the addresses.
6084 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00006085 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00006086
6087 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6088 MachineFunction *F = BB->getParent();
6089 MachineFunction::iterator It = BB;
6090 ++It;
6091
6092 unsigned dest = MI->getOperand(0).getReg();
6093 unsigned ptrA = MI->getOperand(1).getReg();
6094 unsigned ptrB = MI->getOperand(2).getReg();
6095 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006096 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00006097
6098 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6099 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6100 F->insert(It, loopMBB);
6101 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006102 exitMBB->splice(exitMBB->begin(), BB,
6103 llvm::next(MachineBasicBlock::iterator(MI)),
6104 BB->end());
6105 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006106
6107 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006108 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006109 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6110 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00006111 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6112 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6113 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6114 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
6115 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6116 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6117 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6118 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6119 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
6120 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006121 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006122 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006123 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006124
6125 // thisMBB:
6126 // ...
6127 // fallthrough --> loopMBB
6128 BB->addSuccessor(loopMBB);
6129
6130 // The 4-byte load must be aligned, while a char or short may be
6131 // anywhere in the word. Hence all this nasty bookkeeping code.
6132 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6133 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006134 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00006135 // rlwinm ptr, ptr1, 0, 0, 29
6136 // slw incr2, incr, shift
6137 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6138 // slw mask, mask2, shift
6139 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00006140 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006141 // add tmp, tmpDest, incr2
6142 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00006143 // and tmp3, tmp, mask
6144 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00006145 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00006146 // bne- loopMBB
6147 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006148 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006149 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00006150 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006151 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006152 .addReg(ptrA).addReg(ptrB);
6153 } else {
6154 Ptr1Reg = ptrB;
6155 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006156 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006157 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006158 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006159 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6160 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006161 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006162 .addReg(Ptr1Reg).addImm(0).addImm(61);
6163 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006164 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006165 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006166 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006167 .addReg(incr).addReg(ShiftReg);
6168 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006169 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00006170 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006171 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6172 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00006173 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006174 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006175 .addReg(Mask2Reg).addReg(ShiftReg);
6176
6177 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006178 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006179 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006180 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006181 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006182 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006183 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006184 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006185 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006186 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006187 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006188 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00006189 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006190 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006191 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006192 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006193 BB->addSuccessor(loopMBB);
6194 BB->addSuccessor(exitMBB);
6195
6196 // exitMBB:
6197 // ...
6198 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006199 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6200 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00006201 return BB;
6202}
6203
Hal Finkel756810f2013-03-21 21:37:52 +00006204llvm::MachineBasicBlock*
6205PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6206 MachineBasicBlock *MBB) const {
6207 DebugLoc DL = MI->getDebugLoc();
6208 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6209
6210 MachineFunction *MF = MBB->getParent();
6211 MachineRegisterInfo &MRI = MF->getRegInfo();
6212
6213 const BasicBlock *BB = MBB->getBasicBlock();
6214 MachineFunction::iterator I = MBB;
6215 ++I;
6216
6217 // Memory Reference
6218 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6219 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6220
6221 unsigned DstReg = MI->getOperand(0).getReg();
6222 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6223 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6224 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6225 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6226
6227 MVT PVT = getPointerTy();
6228 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6229 "Invalid Pointer Size!");
6230 // For v = setjmp(buf), we generate
6231 //
6232 // thisMBB:
6233 // SjLjSetup mainMBB
6234 // bl mainMBB
6235 // v_restore = 1
6236 // b sinkMBB
6237 //
6238 // mainMBB:
6239 // buf[LabelOffset] = LR
6240 // v_main = 0
6241 //
6242 // sinkMBB:
6243 // v = phi(main, restore)
6244 //
6245
6246 MachineBasicBlock *thisMBB = MBB;
6247 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6248 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6249 MF->insert(I, mainMBB);
6250 MF->insert(I, sinkMBB);
6251
6252 MachineInstrBuilder MIB;
6253
6254 // Transfer the remainder of BB and its successor edges to sinkMBB.
6255 sinkMBB->splice(sinkMBB->begin(), MBB,
6256 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
6257 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6258
6259 // Note that the structure of the jmp_buf used here is not compatible
6260 // with that used by libc, and is not designed to be. Specifically, it
6261 // stores only those 'reserved' registers that LLVM does not otherwise
6262 // understand how to spill. Also, by convention, by the time this
6263 // intrinsic is called, Clang has already stored the frame address in the
6264 // first slot of the buffer and stack address in the third. Following the
6265 // X86 target code, we'll store the jump address in the second slot. We also
6266 // need to save the TOC pointer (R2) to handle jumps between shared
6267 // libraries, and that will be stored in the fourth slot. The thread
6268 // identifier (R13) is not affected.
6269
6270 // thisMBB:
6271 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6272 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006273 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006274
6275 // Prepare IP either in reg.
6276 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6277 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6278 unsigned BufReg = MI->getOperand(1).getReg();
6279
6280 if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) {
6281 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6282 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006283 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006284 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006285 MIB.setMemRefs(MMOBegin, MMOEnd);
6286 }
6287
Hal Finkelf05d6c72013-07-17 23:50:51 +00006288 // Naked functions never have a base pointer, and so we use r1. For all
6289 // other functions, this decision must be delayed until during PEI.
6290 unsigned BaseReg;
6291 if (MF->getFunction()->getAttributes().hasAttribute(
6292 AttributeSet::FunctionIndex, Attribute::Naked))
6293 BaseReg = PPCSubTarget.isPPC64() ? PPC::X1 : PPC::R1;
6294 else
6295 BaseReg = PPCSubTarget.isPPC64() ? PPC::BP8 : PPC::BP;
6296
6297 MIB = BuildMI(*thisMBB, MI, DL,
6298 TII->get(PPCSubTarget.isPPC64() ? PPC::STD : PPC::STW))
6299 .addReg(BaseReg)
6300 .addImm(BPOffset)
6301 .addReg(BufReg);
6302 MIB.setMemRefs(MMOBegin, MMOEnd);
6303
Hal Finkel756810f2013-03-21 21:37:52 +00006304 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00006305 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling5e7656b2013-06-07 07:55:53 +00006306 const PPCRegisterInfo *TRI =
6307 static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo());
6308 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00006309
6310 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6311
6312 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6313 .addMBB(mainMBB);
6314 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6315
6316 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6317 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6318
6319 // mainMBB:
6320 // mainDstReg = 0
6321 MIB = BuildMI(mainMBB, DL,
6322 TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
6323
6324 // Store IP
6325 if (PPCSubTarget.isPPC64()) {
6326 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6327 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006328 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006329 .addReg(BufReg);
6330 } else {
6331 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6332 .addReg(LabelReg)
6333 .addImm(LabelOffset)
6334 .addReg(BufReg);
6335 }
6336
6337 MIB.setMemRefs(MMOBegin, MMOEnd);
6338
6339 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6340 mainMBB->addSuccessor(sinkMBB);
6341
6342 // sinkMBB:
6343 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6344 TII->get(PPC::PHI), DstReg)
6345 .addReg(mainDstReg).addMBB(mainMBB)
6346 .addReg(restoreDstReg).addMBB(thisMBB);
6347
6348 MI->eraseFromParent();
6349 return sinkMBB;
6350}
6351
6352MachineBasicBlock *
6353PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6354 MachineBasicBlock *MBB) const {
6355 DebugLoc DL = MI->getDebugLoc();
6356 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6357
6358 MachineFunction *MF = MBB->getParent();
6359 MachineRegisterInfo &MRI = MF->getRegInfo();
6360
6361 // Memory Reference
6362 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6363 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6364
6365 MVT PVT = getPointerTy();
6366 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6367 "Invalid Pointer Size!");
6368
6369 const TargetRegisterClass *RC =
6370 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6371 unsigned Tmp = MRI.createVirtualRegister(RC);
6372 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6373 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6374 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006375 unsigned BP = (PVT == MVT::i64) ? PPC::X30 : PPC::R30;
Hal Finkel756810f2013-03-21 21:37:52 +00006376
6377 MachineInstrBuilder MIB;
6378
6379 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6380 const int64_t SPOffset = 2 * PVT.getStoreSize();
6381 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006382 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006383
6384 unsigned BufReg = MI->getOperand(0).getReg();
6385
6386 // Reload FP (the jumped-to function may not have had a
6387 // frame pointer, and if so, then its r31 will be restored
6388 // as necessary).
6389 if (PVT == MVT::i64) {
6390 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6391 .addImm(0)
6392 .addReg(BufReg);
6393 } else {
6394 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6395 .addImm(0)
6396 .addReg(BufReg);
6397 }
6398 MIB.setMemRefs(MMOBegin, MMOEnd);
6399
6400 // Reload IP
6401 if (PVT == MVT::i64) {
6402 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006403 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006404 .addReg(BufReg);
6405 } else {
6406 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6407 .addImm(LabelOffset)
6408 .addReg(BufReg);
6409 }
6410 MIB.setMemRefs(MMOBegin, MMOEnd);
6411
6412 // Reload SP
6413 if (PVT == MVT::i64) {
6414 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006415 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006416 .addReg(BufReg);
6417 } else {
6418 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6419 .addImm(SPOffset)
6420 .addReg(BufReg);
6421 }
6422 MIB.setMemRefs(MMOBegin, MMOEnd);
6423
Hal Finkelf05d6c72013-07-17 23:50:51 +00006424 // Reload BP
6425 if (PVT == MVT::i64) {
6426 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
6427 .addImm(BPOffset)
6428 .addReg(BufReg);
6429 } else {
6430 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
6431 .addImm(BPOffset)
6432 .addReg(BufReg);
6433 }
6434 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00006435
6436 // Reload TOC
6437 if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) {
6438 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006439 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006440 .addReg(BufReg);
6441
6442 MIB.setMemRefs(MMOBegin, MMOEnd);
6443 }
6444
6445 // Jump
6446 BuildMI(*MBB, MI, DL,
6447 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6448 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6449
6450 MI->eraseFromParent();
6451 return MBB;
6452}
6453
Dale Johannesena32affb2008-08-28 17:53:09 +00006454MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00006455PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00006456 MachineBasicBlock *BB) const {
Hal Finkel756810f2013-03-21 21:37:52 +00006457 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6458 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6459 return emitEHSjLjSetJmp(MI, BB);
6460 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6461 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6462 return emitEHSjLjLongJmp(MI, BB);
6463 }
6464
Evan Cheng20350c42006-11-27 23:37:22 +00006465 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00006466
6467 // To "insert" these instructions we actually have to insert their
6468 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00006469 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00006470 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00006471 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00006472
Dan Gohman3b460302008-07-07 23:14:23 +00006473 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00006474
Hal Finkel460e94d2012-06-22 23:10:08 +00006475 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006476 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6477 MI->getOpcode() == PPC::SELECT_I4 ||
6478 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00006479 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00006480 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6481 MI->getOpcode() == PPC::SELECT_CC_I8)
6482 Cond.push_back(MI->getOperand(4));
6483 else
6484 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00006485 Cond.push_back(MI->getOperand(1));
6486
Hal Finkel460e94d2012-06-22 23:10:08 +00006487 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00006488 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6489 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
6490 Cond, MI->getOperand(2).getReg(),
6491 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00006492 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6493 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6494 MI->getOpcode() == PPC::SELECT_CC_F4 ||
6495 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006496 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
6497 MI->getOpcode() == PPC::SELECT_I4 ||
6498 MI->getOpcode() == PPC::SELECT_I8 ||
6499 MI->getOpcode() == PPC::SELECT_F4 ||
6500 MI->getOpcode() == PPC::SELECT_F8 ||
6501 MI->getOpcode() == PPC::SELECT_VRRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00006502 // The incoming instruction knows the destination vreg to set, the
6503 // condition code register to branch on, the true/false values to
6504 // select between, and a branch opcode to use.
6505
6506 // thisMBB:
6507 // ...
6508 // TrueVal = ...
6509 // cmpTY ccX, r1, r2
6510 // bCC copy1MBB
6511 // fallthrough --> copy0MBB
6512 MachineBasicBlock *thisMBB = BB;
6513 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6514 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006515 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006516 F->insert(It, copy0MBB);
6517 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006518
6519 // Transfer the remainder of BB and its successor edges to sinkMBB.
6520 sinkMBB->splice(sinkMBB->begin(), BB,
6521 llvm::next(MachineBasicBlock::iterator(MI)),
6522 BB->end());
6523 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6524
Evan Cheng32e376f2008-07-12 02:23:19 +00006525 // Next, add the true and fallthrough blocks as its successors.
6526 BB->addSuccessor(copy0MBB);
6527 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006528
Hal Finkel940ab932014-02-28 00:27:01 +00006529 if (MI->getOpcode() == PPC::SELECT_I4 ||
6530 MI->getOpcode() == PPC::SELECT_I8 ||
6531 MI->getOpcode() == PPC::SELECT_F4 ||
6532 MI->getOpcode() == PPC::SELECT_F8 ||
6533 MI->getOpcode() == PPC::SELECT_VRRC) {
6534 BuildMI(BB, dl, TII->get(PPC::BC))
6535 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6536 } else {
6537 unsigned SelectPred = MI->getOperand(4).getImm();
6538 BuildMI(BB, dl, TII->get(PPC::BCC))
6539 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6540 }
Dan Gohman34396292010-07-06 20:24:04 +00006541
Evan Cheng32e376f2008-07-12 02:23:19 +00006542 // copy0MBB:
6543 // %FalseValue = ...
6544 // # fallthrough to sinkMBB
6545 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006546
Evan Cheng32e376f2008-07-12 02:23:19 +00006547 // Update machine-CFG edges
6548 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006549
Evan Cheng32e376f2008-07-12 02:23:19 +00006550 // sinkMBB:
6551 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6552 // ...
6553 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00006554 BuildMI(*BB, BB->begin(), dl,
6555 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00006556 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
6557 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6558 }
Dale Johannesena32affb2008-08-28 17:53:09 +00006559 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
6560 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
6561 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
6562 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006563 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
6564 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
6565 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
6566 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006567
6568 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
6569 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
6570 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
6571 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006572 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
6573 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
6574 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
6575 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006576
6577 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
6578 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
6579 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
6580 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006581 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
6582 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
6583 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
6584 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006585
6586 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
6587 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
6588 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
6589 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006590 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
6591 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
6592 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
6593 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006594
6595 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006596 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006597 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006598 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006599 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006600 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006601 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006602 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006603
6604 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6605 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6606 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6607 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006608 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6609 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6610 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6611 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006612
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006613 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6614 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6615 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6616 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6617 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6618 BB = EmitAtomicBinary(MI, BB, false, 0);
6619 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6620 BB = EmitAtomicBinary(MI, BB, true, 0);
6621
Evan Cheng32e376f2008-07-12 02:23:19 +00006622 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6623 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6624 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6625
6626 unsigned dest = MI->getOperand(0).getReg();
6627 unsigned ptrA = MI->getOperand(1).getReg();
6628 unsigned ptrB = MI->getOperand(2).getReg();
6629 unsigned oldval = MI->getOperand(3).getReg();
6630 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006631 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006632
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006633 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6634 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6635 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006636 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006637 F->insert(It, loop1MBB);
6638 F->insert(It, loop2MBB);
6639 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006640 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006641 exitMBB->splice(exitMBB->begin(), BB,
6642 llvm::next(MachineBasicBlock::iterator(MI)),
6643 BB->end());
6644 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006645
6646 // thisMBB:
6647 // ...
6648 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006649 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006650
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006651 // loop1MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006652 // l[wd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006653 // cmp[wd] dest, oldval
6654 // bne- midMBB
6655 // loop2MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006656 // st[wd]cx. newval, ptr
6657 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006658 // b exitBB
6659 // midMBB:
6660 // st[wd]cx. dest, ptr
6661 // exitBB:
6662 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006663 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00006664 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006665 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00006666 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006667 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006668 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6669 BB->addSuccessor(loop2MBB);
6670 BB->addSuccessor(midMBB);
6671
6672 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006673 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng32e376f2008-07-12 02:23:19 +00006674 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006675 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006676 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006677 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006678 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006679 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006680
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006681 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006682 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006683 .addReg(dest).addReg(ptrA).addReg(ptrB);
6684 BB->addSuccessor(exitMBB);
6685
Evan Cheng32e376f2008-07-12 02:23:19 +00006686 // exitMBB:
6687 // ...
6688 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00006689 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6690 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6691 // We must use 64-bit registers for addresses when targeting 64-bit,
6692 // since we're actually doing arithmetic on them. Other registers
6693 // can be 32-bit.
6694 bool is64bit = PPCSubTarget.isPPC64();
6695 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6696
6697 unsigned dest = MI->getOperand(0).getReg();
6698 unsigned ptrA = MI->getOperand(1).getReg();
6699 unsigned ptrB = MI->getOperand(2).getReg();
6700 unsigned oldval = MI->getOperand(3).getReg();
6701 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006702 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00006703
6704 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6705 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6706 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6707 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6708 F->insert(It, loop1MBB);
6709 F->insert(It, loop2MBB);
6710 F->insert(It, midMBB);
6711 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006712 exitMBB->splice(exitMBB->begin(), BB,
6713 llvm::next(MachineBasicBlock::iterator(MI)),
6714 BB->end());
6715 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006716
6717 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006718 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006719 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6720 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00006721 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6722 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6723 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6724 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6725 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6726 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6727 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6728 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6729 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6730 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6731 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6732 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6733 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6734 unsigned Ptr1Reg;
6735 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00006736 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00006737 // thisMBB:
6738 // ...
6739 // fallthrough --> loopMBB
6740 BB->addSuccessor(loop1MBB);
6741
6742 // The 4-byte load must be aligned, while a char or short may be
6743 // anywhere in the word. Hence all this nasty bookkeeping code.
6744 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6745 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006746 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00006747 // rlwinm ptr, ptr1, 0, 0, 29
6748 // slw newval2, newval, shift
6749 // slw oldval2, oldval,shift
6750 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6751 // slw mask, mask2, shift
6752 // and newval3, newval2, mask
6753 // and oldval3, oldval2, mask
6754 // loop1MBB:
6755 // lwarx tmpDest, ptr
6756 // and tmp, tmpDest, mask
6757 // cmpw tmp, oldval3
6758 // bne- midMBB
6759 // loop2MBB:
6760 // andc tmp2, tmpDest, mask
6761 // or tmp4, tmp2, newval3
6762 // stwcx. tmp4, ptr
6763 // bne- loop1MBB
6764 // b exitBB
6765 // midMBB:
6766 // stwcx. tmpDest, ptr
6767 // exitBB:
6768 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006769 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00006770 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006771 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006772 .addReg(ptrA).addReg(ptrB);
6773 } else {
6774 Ptr1Reg = ptrB;
6775 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006776 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006777 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006778 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006779 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6780 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006781 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006782 .addReg(Ptr1Reg).addImm(0).addImm(61);
6783 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006784 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006785 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006786 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006787 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006788 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006789 .addReg(oldval).addReg(ShiftReg);
6790 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006791 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00006792 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006793 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6794 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6795 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00006796 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006797 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006798 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006799 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006800 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006801 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006802 .addReg(OldVal2Reg).addReg(MaskReg);
6803
6804 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006805 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006806 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006807 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6808 .addReg(TmpDestReg).addReg(MaskReg);
6809 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00006810 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006811 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006812 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6813 BB->addSuccessor(loop2MBB);
6814 BB->addSuccessor(midMBB);
6815
6816 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006817 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6818 .addReg(TmpDestReg).addReg(MaskReg);
6819 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6820 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6821 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006822 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006823 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006824 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006825 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006826 BB->addSuccessor(loop1MBB);
6827 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006828
Dale Johannesen340d2642008-08-30 00:08:53 +00006829 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006830 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006831 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00006832 BB->addSuccessor(exitMBB);
6833
6834 // exitMBB:
6835 // ...
6836 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006837 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
6838 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00006839 } else if (MI->getOpcode() == PPC::FADDrtz) {
6840 // This pseudo performs an FADD with rounding mode temporarily forced
6841 // to round-to-zero. We emit this via custom inserter since the FPSCR
6842 // is not modeled at the SelectionDAG level.
6843 unsigned Dest = MI->getOperand(0).getReg();
6844 unsigned Src1 = MI->getOperand(1).getReg();
6845 unsigned Src2 = MI->getOperand(2).getReg();
6846 DebugLoc dl = MI->getDebugLoc();
6847
6848 MachineRegisterInfo &RegInfo = F->getRegInfo();
6849 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
6850
6851 // Save FPSCR value.
6852 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
6853
6854 // Set rounding mode to round-to-zero.
6855 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
6856 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
6857
6858 // Perform addition.
6859 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
6860
6861 // Restore FPSCR value.
6862 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00006863 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
6864 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
6865 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
6866 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
6867 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
6868 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
6869 PPC::ANDIo8 : PPC::ANDIo;
6870 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
6871 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
6872
6873 MachineRegisterInfo &RegInfo = F->getRegInfo();
6874 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
6875 &PPC::GPRCRegClass :
6876 &PPC::G8RCRegClass);
6877
6878 DebugLoc dl = MI->getDebugLoc();
6879 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
6880 .addReg(MI->getOperand(1).getReg()).addImm(1);
6881 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
6882 MI->getOperand(0).getReg())
6883 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Dale Johannesen340d2642008-08-30 00:08:53 +00006884 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006885 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00006886 }
Chris Lattner9b577f12005-08-26 21:23:58 +00006887
Dan Gohman34396292010-07-06 20:24:04 +00006888 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00006889 return BB;
6890}
6891
Chris Lattner4211ca92006-04-14 06:01:58 +00006892//===----------------------------------------------------------------------===//
6893// Target Optimization Hooks
6894//===----------------------------------------------------------------------===//
6895
Hal Finkelb0c810f2013-04-03 17:44:56 +00006896SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
6897 DAGCombinerInfo &DCI) const {
Hal Finkel2e103312013-04-03 04:01:11 +00006898 if (DCI.isAfterLegalizeVectorOps())
6899 return SDValue();
6900
Hal Finkelb0c810f2013-04-03 17:44:56 +00006901 EVT VT = Op.getValueType();
6902
6903 if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) ||
6904 (VT == MVT::f64 && PPCSubTarget.hasFRE()) ||
6905 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel2e103312013-04-03 04:01:11 +00006906
6907 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6908 // For the reciprocal, we need to find the zero of the function:
6909 // F(X) = A X - 1 [which has a zero at X = 1/A]
6910 // =>
6911 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
6912 // does not require additional intermediate precision]
6913
6914 // Convergence is quadratic, so we essentially double the number of digits
6915 // correct after every iteration. The minimum architected relative
6916 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6917 // 23 digits and double has 52 digits.
6918 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00006919 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00006920 ++Iterations;
6921
6922 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006923 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006924
6925 SDValue FPOne =
Hal Finkelb0c810f2013-04-03 17:44:56 +00006926 DAG.getConstantFP(1.0, VT.getScalarType());
6927 if (VT.isVector()) {
6928 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00006929 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00006930 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel2e103312013-04-03 04:01:11 +00006931 FPOne, FPOne, FPOne, FPOne);
6932 }
6933
Hal Finkelb0c810f2013-04-03 17:44:56 +00006934 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006935 DCI.AddToWorklist(Est.getNode());
6936
6937 // Newton iterations: Est = Est + Est (1 - Arg * Est)
6938 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00006939 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00006940 DCI.AddToWorklist(NewEst.getNode());
6941
Hal Finkelb0c810f2013-04-03 17:44:56 +00006942 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006943 DCI.AddToWorklist(NewEst.getNode());
6944
Hal Finkelb0c810f2013-04-03 17:44:56 +00006945 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006946 DCI.AddToWorklist(NewEst.getNode());
6947
Hal Finkelb0c810f2013-04-03 17:44:56 +00006948 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006949 DCI.AddToWorklist(Est.getNode());
6950 }
6951
6952 return Est;
6953 }
6954
6955 return SDValue();
6956}
6957
Hal Finkelb0c810f2013-04-03 17:44:56 +00006958SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel2e103312013-04-03 04:01:11 +00006959 DAGCombinerInfo &DCI) const {
6960 if (DCI.isAfterLegalizeVectorOps())
6961 return SDValue();
6962
Hal Finkelb0c810f2013-04-03 17:44:56 +00006963 EVT VT = Op.getValueType();
6964
6965 if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) ||
6966 (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) ||
6967 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel2e103312013-04-03 04:01:11 +00006968
6969 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6970 // For the reciprocal sqrt, we need to find the zero of the function:
6971 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
6972 // =>
6973 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
6974 // As a result, we precompute A/2 prior to the iteration loop.
6975
6976 // Convergence is quadratic, so we essentially double the number of digits
6977 // correct after every iteration. The minimum architected relative
6978 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6979 // 23 digits and double has 52 digits.
6980 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00006981 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00006982 ++Iterations;
6983
6984 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006985 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006986
Hal Finkelb0c810f2013-04-03 17:44:56 +00006987 SDValue FPThreeHalves =
6988 DAG.getConstantFP(1.5, VT.getScalarType());
6989 if (VT.isVector()) {
6990 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00006991 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00006992 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
6993 FPThreeHalves, FPThreeHalves,
6994 FPThreeHalves, FPThreeHalves);
Hal Finkel2e103312013-04-03 04:01:11 +00006995 }
6996
Hal Finkelb0c810f2013-04-03 17:44:56 +00006997 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006998 DCI.AddToWorklist(Est.getNode());
6999
7000 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
7001 // this entire sequence requires only one FP constant.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007002 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007003 DCI.AddToWorklist(HalfArg.getNode());
7004
Hal Finkelb0c810f2013-04-03 17:44:56 +00007005 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007006 DCI.AddToWorklist(HalfArg.getNode());
7007
7008 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
7009 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007010 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00007011 DCI.AddToWorklist(NewEst.getNode());
7012
Hal Finkelb0c810f2013-04-03 17:44:56 +00007013 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007014 DCI.AddToWorklist(NewEst.getNode());
7015
Hal Finkelb0c810f2013-04-03 17:44:56 +00007016 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007017 DCI.AddToWorklist(NewEst.getNode());
7018
Hal Finkelb0c810f2013-04-03 17:44:56 +00007019 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007020 DCI.AddToWorklist(Est.getNode());
7021 }
7022
7023 return Est;
7024 }
7025
7026 return SDValue();
7027}
7028
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007029// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
7030// not enforce equality of the chain operands.
7031static bool isConsecutiveLS(LSBaseSDNode *LS, LSBaseSDNode *Base,
7032 unsigned Bytes, int Dist,
7033 SelectionDAG &DAG) {
7034 EVT VT = LS->getMemoryVT();
7035 if (VT.getSizeInBits() / 8 != Bytes)
7036 return false;
7037
7038 SDValue Loc = LS->getBasePtr();
7039 SDValue BaseLoc = Base->getBasePtr();
7040 if (Loc.getOpcode() == ISD::FrameIndex) {
7041 if (BaseLoc.getOpcode() != ISD::FrameIndex)
7042 return false;
7043 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7044 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
7045 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
7046 int FS = MFI->getObjectSize(FI);
7047 int BFS = MFI->getObjectSize(BFI);
7048 if (FS != BFS || FS != (int)Bytes) return false;
7049 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
7050 }
7051
7052 // Handle X+C
7053 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
7054 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
7055 return true;
7056
7057 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7058 const GlobalValue *GV1 = NULL;
7059 const GlobalValue *GV2 = NULL;
7060 int64_t Offset1 = 0;
7061 int64_t Offset2 = 0;
7062 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
7063 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
7064 if (isGA1 && isGA2 && GV1 == GV2)
7065 return Offset1 == (Offset2 + Dist*Bytes);
7066 return false;
7067}
7068
Hal Finkel7d8a6912013-05-26 18:08:30 +00007069// Return true is there is a nearyby consecutive load to the one provided
7070// (regardless of alignment). We search up and down the chain, looking though
7071// token factors and other loads (but nothing else). As a result, a true
7072// results indicates that it is safe to create a new consecutive load adjacent
7073// to the load provided.
7074static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
7075 SDValue Chain = LD->getChain();
7076 EVT VT = LD->getMemoryVT();
7077
7078 SmallSet<SDNode *, 16> LoadRoots;
7079 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
7080 SmallSet<SDNode *, 16> Visited;
7081
7082 // First, search up the chain, branching to follow all token-factor operands.
7083 // If we find a consecutive load, then we're done, otherwise, record all
7084 // nodes just above the top-level loads and token factors.
7085 while (!Queue.empty()) {
7086 SDNode *ChainNext = Queue.pop_back_val();
7087 if (!Visited.insert(ChainNext))
7088 continue;
7089
7090 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007091 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007092 return true;
7093
7094 if (!Visited.count(ChainLD->getChain().getNode()))
7095 Queue.push_back(ChainLD->getChain().getNode());
7096 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
7097 for (SDNode::op_iterator O = ChainNext->op_begin(),
7098 OE = ChainNext->op_end(); O != OE; ++O)
7099 if (!Visited.count(O->getNode()))
7100 Queue.push_back(O->getNode());
7101 } else
7102 LoadRoots.insert(ChainNext);
7103 }
7104
7105 // Second, search down the chain, starting from the top-level nodes recorded
7106 // in the first phase. These top-level nodes are the nodes just above all
7107 // loads and token factors. Starting with their uses, recursively look though
7108 // all loads (just the chain uses) and token factors to find a consecutive
7109 // load.
7110 Visited.clear();
7111 Queue.clear();
7112
7113 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
7114 IE = LoadRoots.end(); I != IE; ++I) {
7115 Queue.push_back(*I);
7116
7117 while (!Queue.empty()) {
7118 SDNode *LoadRoot = Queue.pop_back_val();
7119 if (!Visited.insert(LoadRoot))
7120 continue;
7121
7122 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007123 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007124 return true;
7125
7126 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
7127 UE = LoadRoot->use_end(); UI != UE; ++UI)
7128 if (((isa<LoadSDNode>(*UI) &&
7129 cast<LoadSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
7130 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
7131 Queue.push_back(*UI);
7132 }
7133 }
7134
7135 return false;
7136}
7137
Hal Finkel940ab932014-02-28 00:27:01 +00007138SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
7139 DAGCombinerInfo &DCI) const {
7140 SelectionDAG &DAG = DCI.DAG;
7141 SDLoc dl(N);
7142
7143 assert(PPCSubTarget.useCRBits() &&
7144 "Expecting to be tracking CR bits");
7145 // If we're tracking CR bits, we need to be careful that we don't have:
7146 // trunc(binary-ops(zext(x), zext(y)))
7147 // or
7148 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
7149 // such that we're unnecessarily moving things into GPRs when it would be
7150 // better to keep them in CR bits.
7151
7152 // Note that trunc here can be an actual i1 trunc, or can be the effective
7153 // truncation that comes from a setcc or select_cc.
7154 if (N->getOpcode() == ISD::TRUNCATE &&
7155 N->getValueType(0) != MVT::i1)
7156 return SDValue();
7157
7158 if (N->getOperand(0).getValueType() != MVT::i32 &&
7159 N->getOperand(0).getValueType() != MVT::i64)
7160 return SDValue();
7161
7162 if (N->getOpcode() == ISD::SETCC ||
7163 N->getOpcode() == ISD::SELECT_CC) {
7164 // If we're looking at a comparison, then we need to make sure that the
7165 // high bits (all except for the first) don't matter the result.
7166 ISD::CondCode CC =
7167 cast<CondCodeSDNode>(N->getOperand(
7168 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
7169 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
7170
7171 if (ISD::isSignedIntSetCC(CC)) {
7172 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
7173 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
7174 return SDValue();
7175 } else if (ISD::isUnsignedIntSetCC(CC)) {
7176 if (!DAG.MaskedValueIsZero(N->getOperand(0),
7177 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
7178 !DAG.MaskedValueIsZero(N->getOperand(1),
7179 APInt::getHighBitsSet(OpBits, OpBits-1)))
7180 return SDValue();
7181 } else {
7182 // This is neither a signed nor an unsigned comparison, just make sure
7183 // that the high bits are equal.
7184 APInt Op1Zero, Op1One;
7185 APInt Op2Zero, Op2One;
7186 DAG.ComputeMaskedBits(N->getOperand(0), Op1Zero, Op1One);
7187 DAG.ComputeMaskedBits(N->getOperand(1), Op2Zero, Op2One);
7188
7189 // We don't really care about what is known about the first bit (if
7190 // anything), so clear it in all masks prior to comparing them.
7191 Op1Zero.clearBit(0); Op1One.clearBit(0);
7192 Op2Zero.clearBit(0); Op2One.clearBit(0);
7193
7194 if (Op1Zero != Op2Zero || Op1One != Op2One)
7195 return SDValue();
7196 }
7197 }
7198
7199 // We now know that the higher-order bits are irrelevant, we just need to
7200 // make sure that all of the intermediate operations are bit operations, and
7201 // all inputs are extensions.
7202 if (N->getOperand(0).getOpcode() != ISD::AND &&
7203 N->getOperand(0).getOpcode() != ISD::OR &&
7204 N->getOperand(0).getOpcode() != ISD::XOR &&
7205 N->getOperand(0).getOpcode() != ISD::SELECT &&
7206 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
7207 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
7208 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
7209 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
7210 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
7211 return SDValue();
7212
7213 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
7214 N->getOperand(1).getOpcode() != ISD::AND &&
7215 N->getOperand(1).getOpcode() != ISD::OR &&
7216 N->getOperand(1).getOpcode() != ISD::XOR &&
7217 N->getOperand(1).getOpcode() != ISD::SELECT &&
7218 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
7219 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
7220 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
7221 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
7222 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
7223 return SDValue();
7224
7225 SmallVector<SDValue, 4> Inputs;
7226 SmallVector<SDValue, 8> BinOps, PromOps;
7227 SmallPtrSet<SDNode *, 16> Visited;
7228
7229 for (unsigned i = 0; i < 2; ++i) {
7230 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7231 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7232 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7233 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7234 isa<ConstantSDNode>(N->getOperand(i)))
7235 Inputs.push_back(N->getOperand(i));
7236 else
7237 BinOps.push_back(N->getOperand(i));
7238
7239 if (N->getOpcode() == ISD::TRUNCATE)
7240 break;
7241 }
7242
7243 // Visit all inputs, collect all binary operations (and, or, xor and
7244 // select) that are all fed by extensions.
7245 while (!BinOps.empty()) {
7246 SDValue BinOp = BinOps.back();
7247 BinOps.pop_back();
7248
7249 if (!Visited.insert(BinOp.getNode()))
7250 continue;
7251
7252 PromOps.push_back(BinOp);
7253
7254 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7255 // The condition of the select is not promoted.
7256 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7257 continue;
7258 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7259 continue;
7260
7261 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7262 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7263 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7264 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7265 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7266 Inputs.push_back(BinOp.getOperand(i));
7267 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7268 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7269 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7270 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7271 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
7272 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7273 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7274 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7275 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
7276 BinOps.push_back(BinOp.getOperand(i));
7277 } else {
7278 // We have an input that is not an extension or another binary
7279 // operation; we'll abort this transformation.
7280 return SDValue();
7281 }
7282 }
7283 }
7284
7285 // Make sure that this is a self-contained cluster of operations (which
7286 // is not quite the same thing as saying that everything has only one
7287 // use).
7288 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7289 if (isa<ConstantSDNode>(Inputs[i]))
7290 continue;
7291
7292 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7293 UE = Inputs[i].getNode()->use_end();
7294 UI != UE; ++UI) {
7295 SDNode *User = *UI;
7296 if (User != N && !Visited.count(User))
7297 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007298
7299 // Make sure that we're not going to promote the non-output-value
7300 // operand(s) or SELECT or SELECT_CC.
7301 // FIXME: Although we could sometimes handle this, and it does occur in
7302 // practice that one of the condition inputs to the select is also one of
7303 // the outputs, we currently can't deal with this.
7304 if (User->getOpcode() == ISD::SELECT) {
7305 if (User->getOperand(0) == Inputs[i])
7306 return SDValue();
7307 } else if (User->getOpcode() == ISD::SELECT_CC) {
7308 if (User->getOperand(0) == Inputs[i] ||
7309 User->getOperand(1) == Inputs[i])
7310 return SDValue();
7311 }
Hal Finkel940ab932014-02-28 00:27:01 +00007312 }
7313 }
7314
7315 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7316 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7317 UE = PromOps[i].getNode()->use_end();
7318 UI != UE; ++UI) {
7319 SDNode *User = *UI;
7320 if (User != N && !Visited.count(User))
7321 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007322
7323 // Make sure that we're not going to promote the non-output-value
7324 // operand(s) or SELECT or SELECT_CC.
7325 // FIXME: Although we could sometimes handle this, and it does occur in
7326 // practice that one of the condition inputs to the select is also one of
7327 // the outputs, we currently can't deal with this.
7328 if (User->getOpcode() == ISD::SELECT) {
7329 if (User->getOperand(0) == PromOps[i])
7330 return SDValue();
7331 } else if (User->getOpcode() == ISD::SELECT_CC) {
7332 if (User->getOperand(0) == PromOps[i] ||
7333 User->getOperand(1) == PromOps[i])
7334 return SDValue();
7335 }
Hal Finkel940ab932014-02-28 00:27:01 +00007336 }
7337 }
7338
7339 // Replace all inputs with the extension operand.
7340 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7341 // Constants may have users outside the cluster of to-be-promoted nodes,
7342 // and so we need to replace those as we do the promotions.
7343 if (isa<ConstantSDNode>(Inputs[i]))
7344 continue;
7345 else
7346 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
7347 }
7348
7349 // Replace all operations (these are all the same, but have a different
7350 // (i1) return type). DAG.getNode will validate that the types of
7351 // a binary operator match, so go through the list in reverse so that
7352 // we've likely promoted both operands first. Any intermediate truncations or
7353 // extensions disappear.
7354 while (!PromOps.empty()) {
7355 SDValue PromOp = PromOps.back();
7356 PromOps.pop_back();
7357
7358 if (PromOp.getOpcode() == ISD::TRUNCATE ||
7359 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
7360 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
7361 PromOp.getOpcode() == ISD::ANY_EXTEND) {
7362 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
7363 PromOp.getOperand(0).getValueType() != MVT::i1) {
7364 // The operand is not yet ready (see comment below).
7365 PromOps.insert(PromOps.begin(), PromOp);
7366 continue;
7367 }
7368
7369 SDValue RepValue = PromOp.getOperand(0);
7370 if (isa<ConstantSDNode>(RepValue))
7371 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
7372
7373 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
7374 continue;
7375 }
7376
7377 unsigned C;
7378 switch (PromOp.getOpcode()) {
7379 default: C = 0; break;
7380 case ISD::SELECT: C = 1; break;
7381 case ISD::SELECT_CC: C = 2; break;
7382 }
7383
7384 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7385 PromOp.getOperand(C).getValueType() != MVT::i1) ||
7386 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7387 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
7388 // The to-be-promoted operands of this node have not yet been
7389 // promoted (this should be rare because we're going through the
7390 // list backward, but if one of the operands has several users in
7391 // this cluster of to-be-promoted nodes, it is possible).
7392 PromOps.insert(PromOps.begin(), PromOp);
7393 continue;
7394 }
7395
7396 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7397 PromOp.getNode()->op_end());
7398
7399 // If there are any constant inputs, make sure they're replaced now.
7400 for (unsigned i = 0; i < 2; ++i)
7401 if (isa<ConstantSDNode>(Ops[C+i]))
7402 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
7403
7404 DAG.ReplaceAllUsesOfValueWith(PromOp,
7405 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1,
7406 Ops.data(), Ops.size()));
7407 }
7408
7409 // Now we're left with the initial truncation itself.
7410 if (N->getOpcode() == ISD::TRUNCATE)
7411 return N->getOperand(0);
7412
7413 // Otherwise, this is a comparison. The operands to be compared have just
7414 // changed type (to i1), but everything else is the same.
7415 return SDValue(N, 0);
7416}
7417
7418SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
7419 DAGCombinerInfo &DCI) const {
7420 SelectionDAG &DAG = DCI.DAG;
7421 SDLoc dl(N);
7422
Hal Finkel940ab932014-02-28 00:27:01 +00007423 // If we're tracking CR bits, we need to be careful that we don't have:
7424 // zext(binary-ops(trunc(x), trunc(y)))
7425 // or
7426 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
7427 // such that we're unnecessarily moving things into CR bits that can more
7428 // efficiently stay in GPRs. Note that if we're not certain that the high
7429 // bits are set as required by the final extension, we still may need to do
7430 // some masking to get the proper behavior.
7431
Hal Finkel46043ed2014-03-01 21:36:57 +00007432 // This same functionality is important on PPC64 when dealing with
7433 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
7434 // the return values of functions. Because it is so similar, it is handled
7435 // here as well.
7436
Hal Finkel940ab932014-02-28 00:27:01 +00007437 if (N->getValueType(0) != MVT::i32 &&
7438 N->getValueType(0) != MVT::i64)
7439 return SDValue();
7440
Hal Finkel46043ed2014-03-01 21:36:57 +00007441 if (!((N->getOperand(0).getValueType() == MVT::i1 &&
7442 PPCSubTarget.useCRBits()) ||
7443 (N->getOperand(0).getValueType() == MVT::i32 &&
7444 PPCSubTarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +00007445 return SDValue();
7446
7447 if (N->getOperand(0).getOpcode() != ISD::AND &&
7448 N->getOperand(0).getOpcode() != ISD::OR &&
7449 N->getOperand(0).getOpcode() != ISD::XOR &&
7450 N->getOperand(0).getOpcode() != ISD::SELECT &&
7451 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
7452 return SDValue();
7453
7454 SmallVector<SDValue, 4> Inputs;
7455 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
7456 SmallPtrSet<SDNode *, 16> Visited;
7457
7458 // Visit all inputs, collect all binary operations (and, or, xor and
7459 // select) that are all fed by truncations.
7460 while (!BinOps.empty()) {
7461 SDValue BinOp = BinOps.back();
7462 BinOps.pop_back();
7463
7464 if (!Visited.insert(BinOp.getNode()))
7465 continue;
7466
7467 PromOps.push_back(BinOp);
7468
7469 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7470 // The condition of the select is not promoted.
7471 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7472 continue;
7473 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7474 continue;
7475
7476 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7477 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7478 Inputs.push_back(BinOp.getOperand(i));
7479 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7480 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7481 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7482 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7483 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
7484 BinOps.push_back(BinOp.getOperand(i));
7485 } else {
7486 // We have an input that is not a truncation or another binary
7487 // operation; we'll abort this transformation.
7488 return SDValue();
7489 }
7490 }
7491 }
7492
7493 // Make sure that this is a self-contained cluster of operations (which
7494 // is not quite the same thing as saying that everything has only one
7495 // use).
7496 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7497 if (isa<ConstantSDNode>(Inputs[i]))
7498 continue;
7499
7500 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7501 UE = Inputs[i].getNode()->use_end();
7502 UI != UE; ++UI) {
7503 SDNode *User = *UI;
7504 if (User != N && !Visited.count(User))
7505 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007506
7507 // Make sure that we're not going to promote the non-output-value
7508 // operand(s) or SELECT or SELECT_CC.
7509 // FIXME: Although we could sometimes handle this, and it does occur in
7510 // practice that one of the condition inputs to the select is also one of
7511 // the outputs, we currently can't deal with this.
7512 if (User->getOpcode() == ISD::SELECT) {
7513 if (User->getOperand(0) == Inputs[i])
7514 return SDValue();
7515 } else if (User->getOpcode() == ISD::SELECT_CC) {
7516 if (User->getOperand(0) == Inputs[i] ||
7517 User->getOperand(1) == Inputs[i])
7518 return SDValue();
7519 }
Hal Finkel940ab932014-02-28 00:27:01 +00007520 }
7521 }
7522
7523 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7524 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7525 UE = PromOps[i].getNode()->use_end();
7526 UI != UE; ++UI) {
7527 SDNode *User = *UI;
7528 if (User != N && !Visited.count(User))
7529 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007530
7531 // Make sure that we're not going to promote the non-output-value
7532 // operand(s) or SELECT or SELECT_CC.
7533 // FIXME: Although we could sometimes handle this, and it does occur in
7534 // practice that one of the condition inputs to the select is also one of
7535 // the outputs, we currently can't deal with this.
7536 if (User->getOpcode() == ISD::SELECT) {
7537 if (User->getOperand(0) == PromOps[i])
7538 return SDValue();
7539 } else if (User->getOpcode() == ISD::SELECT_CC) {
7540 if (User->getOperand(0) == PromOps[i] ||
7541 User->getOperand(1) == PromOps[i])
7542 return SDValue();
7543 }
Hal Finkel940ab932014-02-28 00:27:01 +00007544 }
7545 }
7546
Hal Finkel46043ed2014-03-01 21:36:57 +00007547 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +00007548 bool ReallyNeedsExt = false;
7549 if (N->getOpcode() != ISD::ANY_EXTEND) {
7550 // If all of the inputs are not already sign/zero extended, then
7551 // we'll still need to do that at the end.
7552 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7553 if (isa<ConstantSDNode>(Inputs[i]))
7554 continue;
7555
7556 unsigned OpBits =
7557 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +00007558 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
7559
Hal Finkel940ab932014-02-28 00:27:01 +00007560 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
7561 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00007562 APInt::getHighBitsSet(OpBits,
7563 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +00007564 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +00007565 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
7566 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +00007567 ReallyNeedsExt = true;
7568 break;
7569 }
7570 }
7571 }
7572
7573 // Replace all inputs, either with the truncation operand, or a
7574 // truncation or extension to the final output type.
7575 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7576 // Constant inputs need to be replaced with the to-be-promoted nodes that
7577 // use them because they might have users outside of the cluster of
7578 // promoted nodes.
7579 if (isa<ConstantSDNode>(Inputs[i]))
7580 continue;
7581
7582 SDValue InSrc = Inputs[i].getOperand(0);
7583 if (Inputs[i].getValueType() == N->getValueType(0))
7584 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
7585 else if (N->getOpcode() == ISD::SIGN_EXTEND)
7586 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7587 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
7588 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7589 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7590 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
7591 else
7592 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7593 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
7594 }
7595
7596 // Replace all operations (these are all the same, but have a different
7597 // (promoted) return type). DAG.getNode will validate that the types of
7598 // a binary operator match, so go through the list in reverse so that
7599 // we've likely promoted both operands first.
7600 while (!PromOps.empty()) {
7601 SDValue PromOp = PromOps.back();
7602 PromOps.pop_back();
7603
7604 unsigned C;
7605 switch (PromOp.getOpcode()) {
7606 default: C = 0; break;
7607 case ISD::SELECT: C = 1; break;
7608 case ISD::SELECT_CC: C = 2; break;
7609 }
7610
7611 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7612 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
7613 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7614 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
7615 // The to-be-promoted operands of this node have not yet been
7616 // promoted (this should be rare because we're going through the
7617 // list backward, but if one of the operands has several users in
7618 // this cluster of to-be-promoted nodes, it is possible).
7619 PromOps.insert(PromOps.begin(), PromOp);
7620 continue;
7621 }
7622
7623 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7624 PromOp.getNode()->op_end());
7625
7626 // If this node has constant inputs, then they'll need to be promoted here.
7627 for (unsigned i = 0; i < 2; ++i) {
7628 if (!isa<ConstantSDNode>(Ops[C+i]))
7629 continue;
7630 if (Ops[C+i].getValueType() == N->getValueType(0))
7631 continue;
7632
7633 if (N->getOpcode() == ISD::SIGN_EXTEND)
7634 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7635 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7636 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7637 else
7638 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7639 }
7640
7641 DAG.ReplaceAllUsesOfValueWith(PromOp,
7642 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0),
7643 Ops.data(), Ops.size()));
7644 }
7645
7646 // Now we're left with the initial extension itself.
7647 if (!ReallyNeedsExt)
7648 return N->getOperand(0);
7649
Hal Finkel46043ed2014-03-01 21:36:57 +00007650 // To zero extend, just mask off everything except for the first bit (in the
7651 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +00007652 if (N->getOpcode() == ISD::ZERO_EXTEND)
7653 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00007654 DAG.getConstant(APInt::getLowBitsSet(
7655 N->getValueSizeInBits(0), PromBits),
7656 N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +00007657
7658 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
7659 "Invalid extension type");
7660 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0));
7661 SDValue ShiftCst =
Hal Finkel46043ed2014-03-01 21:36:57 +00007662 DAG.getConstant(N->getValueSizeInBits(0)-PromBits, ShiftAmountTy);
Hal Finkel940ab932014-02-28 00:27:01 +00007663 return DAG.getNode(ISD::SRA, dl, N->getValueType(0),
7664 DAG.getNode(ISD::SHL, dl, N->getValueType(0),
7665 N->getOperand(0), ShiftCst), ShiftCst);
7666}
7667
Duncan Sandsdc2dac12008-11-24 14:53:14 +00007668SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
7669 DAGCombinerInfo &DCI) const {
Dan Gohman57c732b2010-04-21 01:34:56 +00007670 const TargetMachine &TM = getTargetMachine();
Chris Lattnerf4184352006-03-01 04:57:39 +00007671 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007672 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00007673 switch (N->getOpcode()) {
7674 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00007675 case PPCISD::SHL:
7676 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007677 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007678 return N->getOperand(0);
7679 }
7680 break;
7681 case PPCISD::SRL:
7682 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007683 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007684 return N->getOperand(0);
7685 }
7686 break;
7687 case PPCISD::SRA:
7688 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007689 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007690 C->isAllOnesValue()) // -1 >>s V -> -1.
7691 return N->getOperand(0);
7692 }
7693 break;
Hal Finkel940ab932014-02-28 00:27:01 +00007694 case ISD::SIGN_EXTEND:
7695 case ISD::ZERO_EXTEND:
7696 case ISD::ANY_EXTEND:
7697 return DAGCombineExtBoolTrunc(N, DCI);
7698 case ISD::TRUNCATE:
7699 case ISD::SETCC:
7700 case ISD::SELECT_CC:
7701 return DAGCombineTruncBoolExt(N, DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007702 case ISD::FDIV: {
7703 assert(TM.Options.UnsafeFPMath &&
7704 "Reciprocal estimates require UnsafeFPMath");
Scott Michelcf0da6c2009-02-17 22:15:04 +00007705
Hal Finkel2e103312013-04-03 04:01:11 +00007706 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007707 SDValue RV =
7708 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007709 if (RV.getNode() != 0) {
7710 DCI.AddToWorklist(RV.getNode());
7711 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7712 N->getOperand(0), RV);
7713 }
Hal Finkelf96c18e2013-04-04 22:44:12 +00007714 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
7715 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7716 SDValue RV =
7717 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7718 DCI);
7719 if (RV.getNode() != 0) {
7720 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007721 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007722 N->getValueType(0), RV);
7723 DCI.AddToWorklist(RV.getNode());
7724 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7725 N->getOperand(0), RV);
7726 }
7727 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
7728 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7729 SDValue RV =
7730 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7731 DCI);
7732 if (RV.getNode() != 0) {
7733 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007734 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007735 N->getValueType(0), RV,
7736 N->getOperand(1).getOperand(1));
7737 DCI.AddToWorklist(RV.getNode());
7738 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7739 N->getOperand(0), RV);
7740 }
Hal Finkel2e103312013-04-03 04:01:11 +00007741 }
7742
Hal Finkelb0c810f2013-04-03 17:44:56 +00007743 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007744 if (RV.getNode() != 0) {
7745 DCI.AddToWorklist(RV.getNode());
7746 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7747 N->getOperand(0), RV);
7748 }
7749
7750 }
7751 break;
7752 case ISD::FSQRT: {
7753 assert(TM.Options.UnsafeFPMath &&
7754 "Reciprocal estimates require UnsafeFPMath");
7755
7756 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
7757 // reciprocal sqrt.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007758 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007759 if (RV.getNode() != 0) {
7760 DCI.AddToWorklist(RV.getNode());
Hal Finkelb0c810f2013-04-03 17:44:56 +00007761 RV = DAGCombineFastRecip(RV, DCI);
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007762 if (RV.getNode() != 0) {
7763 // Unfortunately, RV is now NaN if the input was exactly 0. Select out
7764 // this case and force the answer to 0.
7765
7766 EVT VT = RV.getValueType();
7767
7768 SDValue Zero = DAG.getConstantFP(0.0, VT.getScalarType());
7769 if (VT.isVector()) {
7770 assert(VT.getVectorNumElements() == 4 && "Unknown vector type");
7771 Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Zero, Zero, Zero, Zero);
7772 }
7773
7774 SDValue ZeroCmp =
7775 DAG.getSetCC(dl, getSetCCResultType(*DAG.getContext(), VT),
7776 N->getOperand(0), Zero, ISD::SETEQ);
7777 DCI.AddToWorklist(ZeroCmp.getNode());
7778 DCI.AddToWorklist(RV.getNode());
7779
7780 RV = DAG.getNode(VT.isVector() ? ISD::VSELECT : ISD::SELECT, dl, VT,
7781 ZeroCmp, Zero, RV);
Hal Finkel2e103312013-04-03 04:01:11 +00007782 return RV;
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007783 }
Hal Finkel2e103312013-04-03 04:01:11 +00007784 }
7785
7786 }
7787 break;
Chris Lattnerf4184352006-03-01 04:57:39 +00007788 case ISD::SINT_TO_FP:
Chris Lattnera35f3062006-06-16 17:34:12 +00007789 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007790 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
7791 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
7792 // We allow the src/dst to be either f32/f64, but the intermediate
7793 // type must be i64.
Owen Anderson9f944592009-08-11 20:47:22 +00007794 if (N->getOperand(0).getValueType() == MVT::i64 &&
7795 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007796 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007797 if (Val.getValueType() == MVT::f32) {
7798 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007799 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007800 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007801
Owen Anderson9f944592009-08-11 20:47:22 +00007802 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007803 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007804 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007805 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007806 if (N->getValueType(0) == MVT::f32) {
7807 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner72733e52008-01-17 07:00:52 +00007808 DAG.getIntPtrConstant(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007809 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007810 }
7811 return Val;
Owen Anderson9f944592009-08-11 20:47:22 +00007812 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007813 // If the intermediate type is i32, we can avoid the load/store here
7814 // too.
Chris Lattnerf4184352006-03-01 04:57:39 +00007815 }
Chris Lattnerf4184352006-03-01 04:57:39 +00007816 }
7817 }
7818 break;
Chris Lattner27f53452006-03-01 05:50:56 +00007819 case ISD::STORE:
7820 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
7821 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnerf5b46f72008-01-18 16:54:56 +00007822 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00007823 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00007824 N->getOperand(1).getValueType() == MVT::i32 &&
7825 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007826 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007827 if (Val.getValueType() == MVT::f32) {
7828 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007829 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007830 }
Owen Anderson9f944592009-08-11 20:47:22 +00007831 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007832 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007833
Hal Finkel60c75102013-04-01 15:37:53 +00007834 SDValue Ops[] = {
7835 N->getOperand(0), Val, N->getOperand(2),
7836 DAG.getValueType(N->getOperand(1).getValueType())
7837 };
7838
7839 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
7840 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
7841 cast<StoreSDNode>(N)->getMemoryVT(),
7842 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00007843 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007844 return Val;
7845 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007846
Chris Lattnera7976d32006-07-10 20:56:58 +00007847 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00007848 if (cast<StoreSDNode>(N)->isUnindexed() &&
7849 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00007850 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00007851 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00007852 N->getOperand(1).getValueType() == MVT::i16 ||
7853 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00007854 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007855 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007856 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00007857 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00007858 if (BSwapOp.getValueType() == MVT::i16)
7859 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00007860
Dan Gohman48b185d2009-09-25 20:36:54 +00007861 SDValue Ops[] = {
7862 N->getOperand(0), BSwapOp, N->getOperand(2),
7863 DAG.getValueType(N->getOperand(1).getValueType())
7864 };
7865 return
7866 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
7867 Ops, array_lengthof(Ops),
7868 cast<StoreSDNode>(N)->getMemoryVT(),
7869 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00007870 }
7871 break;
Hal Finkelcf2e9082013-05-24 23:00:14 +00007872 case ISD::LOAD: {
7873 LoadSDNode *LD = cast<LoadSDNode>(N);
7874 EVT VT = LD->getValueType(0);
7875 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
7876 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
7877 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
7878 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
Hal Finkel40c34782013-09-15 22:09:58 +00007879 (VT == MVT::v16i8 || VT == MVT::v8i16 ||
7880 VT == MVT::v4i32 || VT == MVT::v4f32) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +00007881 LD->getAlignment() < ABIAlignment) {
7882 // This is a type-legal unaligned Altivec load.
7883 SDValue Chain = LD->getChain();
7884 SDValue Ptr = LD->getBasePtr();
7885
7886 // This implements the loading of unaligned vectors as described in
7887 // the venerable Apple Velocity Engine overview. Specifically:
7888 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
7889 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
7890 //
7891 // The general idea is to expand a sequence of one or more unaligned
7892 // loads into a alignment-based permutation-control instruction (lvsl),
7893 // a series of regular vector loads (which always truncate their
7894 // input address to an aligned address), and a series of permutations.
7895 // The results of these permutations are the requested loaded values.
7896 // The trick is that the last "extra" load is not taken from the address
7897 // you might suspect (sizeof(vector) bytes after the last requested
7898 // load), but rather sizeof(vector) - 1 bytes after the last
7899 // requested vector. The point of this is to avoid a page fault if the
Alp Tokercb402912014-01-24 17:20:08 +00007900 // base address happened to be aligned. This works because if the base
Hal Finkelcf2e9082013-05-24 23:00:14 +00007901 // address is aligned, then adding less than a full vector length will
7902 // cause the last vector in the sequence to be (re)loaded. Otherwise,
7903 // the next vector will be fetched as you might suspect was necessary.
7904
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007905 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00007906 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007907 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
7908 // optimization later.
Hal Finkelcf2e9082013-05-24 23:00:14 +00007909 SDValue PermCntl = BuildIntrinsicOp(Intrinsic::ppc_altivec_lvsl, Ptr,
7910 DAG, dl, MVT::v16i8);
7911
7912 // Refine the alignment of the original load (a "new" load created here
7913 // which was identical to the first except for the alignment would be
7914 // merged with the existing node regardless).
7915 MachineFunction &MF = DAG.getMachineFunction();
7916 MachineMemOperand *MMO =
7917 MF.getMachineMemOperand(LD->getPointerInfo(),
7918 LD->getMemOperand()->getFlags(),
7919 LD->getMemoryVT().getStoreSize(),
7920 ABIAlignment);
7921 LD->refineAlignment(MMO);
7922 SDValue BaseLoad = SDValue(LD, 0);
7923
7924 // Note that the value of IncOffset (which is provided to the next
7925 // load's pointer info offset value, and thus used to calculate the
7926 // alignment), and the value of IncValue (which is actually used to
7927 // increment the pointer value) are different! This is because we
7928 // require the next load to appear to be aligned, even though it
7929 // is actually offset from the base pointer by a lesser amount.
7930 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +00007931 int IncValue = IncOffset;
7932
7933 // Walk (both up and down) the chain looking for another load at the real
7934 // (aligned) offset (the alignment of the other load does not matter in
7935 // this case). If found, then do not use the offset reduction trick, as
7936 // that will prevent the loads from being later combined (as they would
7937 // otherwise be duplicates).
7938 if (!findConsecutiveLoad(LD, DAG))
7939 --IncValue;
7940
Hal Finkelcf2e9082013-05-24 23:00:14 +00007941 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
7942 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
7943
Hal Finkelcf2e9082013-05-24 23:00:14 +00007944 SDValue ExtraLoad =
7945 DAG.getLoad(VT, dl, Chain, Ptr,
7946 LD->getPointerInfo().getWithOffset(IncOffset),
7947 LD->isVolatile(), LD->isNonTemporal(),
7948 LD->isInvariant(), ABIAlignment);
7949
7950 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7951 BaseLoad.getValue(1), ExtraLoad.getValue(1));
7952
7953 if (BaseLoad.getValueType() != MVT::v4i32)
7954 BaseLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, BaseLoad);
7955
7956 if (ExtraLoad.getValueType() != MVT::v4i32)
7957 ExtraLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ExtraLoad);
7958
7959 SDValue Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
7960 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
7961
7962 if (VT != MVT::v4i32)
7963 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
7964
7965 // Now we need to be really careful about how we update the users of the
7966 // original load. We cannot just call DCI.CombineTo (or
7967 // DAG.ReplaceAllUsesWith for that matter), because the load still has
7968 // uses created here (the permutation for example) that need to stay.
7969 SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
7970 while (UI != UE) {
7971 SDUse &Use = UI.getUse();
7972 SDNode *User = *UI;
7973 // Note: BaseLoad is checked here because it might not be N, but a
7974 // bitcast of N.
7975 if (User == Perm.getNode() || User == BaseLoad.getNode() ||
7976 User == TF.getNode() || Use.getResNo() > 1) {
7977 ++UI;
7978 continue;
7979 }
7980
7981 SDValue To = Use.getResNo() ? TF : Perm;
7982 ++UI;
7983
7984 SmallVector<SDValue, 8> Ops;
7985 for (SDNode::op_iterator O = User->op_begin(),
7986 OE = User->op_end(); O != OE; ++O) {
7987 if (*O == Use)
7988 Ops.push_back(To);
7989 else
7990 Ops.push_back(*O);
7991 }
7992
7993 DAG.UpdateNodeOperands(User, Ops.data(), Ops.size());
7994 }
7995
7996 return SDValue(N, 0);
7997 }
7998 }
7999 break;
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008000 case ISD::INTRINSIC_WO_CHAIN:
8001 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() ==
8002 Intrinsic::ppc_altivec_lvsl &&
8003 N->getOperand(1)->getOpcode() == ISD::ADD) {
8004 SDValue Add = N->getOperand(1);
8005
8006 if (DAG.MaskedValueIsZero(Add->getOperand(1),
8007 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
8008 Add.getValueType().getScalarType().getSizeInBits()))) {
8009 SDNode *BasePtr = Add->getOperand(0).getNode();
8010 for (SDNode::use_iterator UI = BasePtr->use_begin(),
8011 UE = BasePtr->use_end(); UI != UE; ++UI) {
8012 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8013 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
8014 Intrinsic::ppc_altivec_lvsl) {
8015 // We've found another LVSL, and this address if an aligned
8016 // multiple of that one. The results will be the same, so use the
8017 // one we've just found instead.
8018
8019 return SDValue(*UI, 0);
8020 }
8021 }
8022 }
8023 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +00008024
8025 break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008026 case ISD::BSWAP:
8027 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008028 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +00008029 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008030 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
8031 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008032 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008033 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008034 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00008035 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +00008036 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008037 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +00008038 LD->getChain(), // Chain
8039 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008040 DAG.getValueType(N->getValueType(0)) // VT
8041 };
Dan Gohman48b185d2009-09-25 20:36:54 +00008042 SDValue BSLoad =
8043 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +00008044 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
8045 MVT::i64 : MVT::i32, MVT::Other),
Hal Finkel93492fa2013-03-28 19:43:12 +00008046 Ops, 3, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008047
Scott Michelcf0da6c2009-02-17 22:15:04 +00008048 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008049 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +00008050 if (N->getValueType(0) == MVT::i16)
8051 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008052
Chris Lattnera7976d32006-07-10 20:56:58 +00008053 // First, combine the bswap away. This makes the value produced by the
8054 // load dead.
8055 DCI.CombineTo(N, ResVal);
8056
8057 // Next, combine the load away, we give it a bogus result value but a real
8058 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008059 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00008060
Chris Lattnera7976d32006-07-10 20:56:58 +00008061 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008062 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008063 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008064
Chris Lattner27f53452006-03-01 05:50:56 +00008065 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008066 case PPCISD::VCMP: {
8067 // If a VCMPo node already exists with exactly the same operands as this
8068 // node, use its result instead of this node (VCMPo computes both a CR6 and
8069 // a normal output).
8070 //
8071 if (!N->getOperand(0).hasOneUse() &&
8072 !N->getOperand(1).hasOneUse() &&
8073 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00008074
Chris Lattnerd4058a52006-03-31 06:02:07 +00008075 // Scan all of the users of the LHS, looking for VCMPo's that match.
8076 SDNode *VCMPoNode = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008077
Gabor Greiff304a7a2008-08-28 21:40:38 +00008078 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +00008079 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
8080 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008081 if (UI->getOpcode() == PPCISD::VCMPo &&
8082 UI->getOperand(1) == N->getOperand(1) &&
8083 UI->getOperand(2) == N->getOperand(2) &&
8084 UI->getOperand(0) == N->getOperand(0)) {
8085 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008086 break;
8087 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008088
Chris Lattner518834c2006-04-18 18:28:22 +00008089 // If there is no VCMPo node, or if the flag value has a single use, don't
8090 // transform this.
8091 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
8092 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008093
8094 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +00008095 // chain, this transformation is more complex. Note that multiple things
8096 // could use the value result, which we should ignore.
8097 SDNode *FlagUser = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008098 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner518834c2006-04-18 18:28:22 +00008099 FlagUser == 0; ++UI) {
8100 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008101 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +00008102 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008103 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +00008104 FlagUser = User;
8105 break;
8106 }
8107 }
8108 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008109
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008110 // If the user is a MFOCRF instruction, we know this is safe.
8111 // Otherwise we give up for right now.
8112 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008113 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +00008114 }
8115 break;
8116 }
Hal Finkel940ab932014-02-28 00:27:01 +00008117 case ISD::BRCOND: {
8118 SDValue Cond = N->getOperand(1);
8119 SDValue Target = N->getOperand(2);
8120
8121 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8122 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
8123 Intrinsic::ppc_is_decremented_ctr_nonzero) {
8124
8125 // We now need to make the intrinsic dead (it cannot be instruction
8126 // selected).
8127 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
8128 assert(Cond.getNode()->hasOneUse() &&
8129 "Counter decrement has more than one use");
8130
8131 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
8132 N->getOperand(0), Target);
8133 }
8134 }
8135 break;
Chris Lattner9754d142006-04-18 17:59:36 +00008136 case ISD::BR_CC: {
8137 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008138 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +00008139 // lowering is done pre-legalize, because the legalizer lowers the predicate
8140 // compare down to code that is difficult to reassemble.
8141 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008142 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +00008143
8144 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
8145 // value. If so, pass-through the AND to get to the intrinsic.
8146 if (LHS.getOpcode() == ISD::AND &&
8147 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8148 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
8149 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8150 isa<ConstantSDNode>(LHS.getOperand(1)) &&
8151 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
8152 isZero())
8153 LHS = LHS.getOperand(0);
8154
8155 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8156 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
8157 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8158 isa<ConstantSDNode>(RHS)) {
8159 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
8160 "Counter decrement comparison is not EQ or NE");
8161
8162 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
8163 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
8164 (CC == ISD::SETNE && !Val);
8165
8166 // We now need to make the intrinsic dead (it cannot be instruction
8167 // selected).
8168 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
8169 assert(LHS.getNode()->hasOneUse() &&
8170 "Counter decrement has more than one use");
8171
8172 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
8173 N->getOperand(0), N->getOperand(4));
8174 }
8175
Chris Lattner9754d142006-04-18 17:59:36 +00008176 int CompareOpc;
8177 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008178
Chris Lattner9754d142006-04-18 17:59:36 +00008179 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8180 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
8181 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
8182 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00008183
Chris Lattner9754d142006-04-18 17:59:36 +00008184 // If this is a comparison against something other than 0/1, then we know
8185 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008186 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00008187 if (Val != 0 && Val != 1) {
8188 if (CC == ISD::SETEQ) // Cond never true, remove branch.
8189 return N->getOperand(0);
8190 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +00008191 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +00008192 N->getOperand(0), N->getOperand(4));
8193 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008194
Chris Lattner9754d142006-04-18 17:59:36 +00008195 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008196
Chris Lattner9754d142006-04-18 17:59:36 +00008197 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008198 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008199 LHS.getOperand(2), // LHS of compare
8200 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +00008201 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008202 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00008203 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00008204 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008205
Chris Lattner9754d142006-04-18 17:59:36 +00008206 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008207 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +00008208 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +00008209 default: // Can't happen, don't crash on invalid number though.
8210 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008211 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +00008212 break;
8213 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008214 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +00008215 break;
8216 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008217 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +00008218 break;
8219 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008220 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +00008221 break;
8222 }
8223
Owen Anderson9f944592009-08-11 20:47:22 +00008224 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
8225 DAG.getConstant(CompOpc, MVT::i32),
8226 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +00008227 N->getOperand(4), CompNode.getValue(1));
8228 }
8229 break;
8230 }
Chris Lattnerf4184352006-03-01 04:57:39 +00008231 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008232
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008233 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +00008234}
8235
Chris Lattner4211ca92006-04-14 06:01:58 +00008236//===----------------------------------------------------------------------===//
8237// Inline Assembly Support
8238//===----------------------------------------------------------------------===//
8239
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008240void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Scott Michelcf0da6c2009-02-17 22:15:04 +00008241 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +00008242 APInt &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +00008243 const SelectionDAG &DAG,
Chris Lattnerc5287c02006-04-02 06:26:07 +00008244 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00008245 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +00008246 switch (Op.getOpcode()) {
8247 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008248 case PPCISD::LBRX: {
8249 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +00008250 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +00008251 KnownZero = 0xFFFF0000;
8252 break;
8253 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008254 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00008255 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +00008256 default: break;
8257 case Intrinsic::ppc_altivec_vcmpbfp_p:
8258 case Intrinsic::ppc_altivec_vcmpeqfp_p:
8259 case Intrinsic::ppc_altivec_vcmpequb_p:
8260 case Intrinsic::ppc_altivec_vcmpequh_p:
8261 case Intrinsic::ppc_altivec_vcmpequw_p:
8262 case Intrinsic::ppc_altivec_vcmpgefp_p:
8263 case Intrinsic::ppc_altivec_vcmpgtfp_p:
8264 case Intrinsic::ppc_altivec_vcmpgtsb_p:
8265 case Intrinsic::ppc_altivec_vcmpgtsh_p:
8266 case Intrinsic::ppc_altivec_vcmpgtsw_p:
8267 case Intrinsic::ppc_altivec_vcmpgtub_p:
8268 case Intrinsic::ppc_altivec_vcmpgtuh_p:
8269 case Intrinsic::ppc_altivec_vcmpgtuw_p:
8270 KnownZero = ~1U; // All bits but the low one are known to be zero.
8271 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008272 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008273 }
8274 }
8275}
8276
8277
Chris Lattnerd6855142007-03-25 02:14:49 +00008278/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +00008279/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008280PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00008281PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
8282 if (Constraint.size() == 1) {
8283 switch (Constraint[0]) {
8284 default: break;
8285 case 'b':
8286 case 'r':
8287 case 'f':
8288 case 'v':
8289 case 'y':
8290 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +00008291 case 'Z':
8292 // FIXME: While Z does indicate a memory constraint, it specifically
8293 // indicates an r+r address (used in conjunction with the 'y' modifier
8294 // in the replacement string). Currently, we're forcing the base
8295 // register to be r0 in the asm printer (which is interpreted as zero)
8296 // and forming the complete address in the second register. This is
8297 // suboptimal.
8298 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +00008299 }
8300 }
8301 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +00008302}
8303
John Thompsone8360b72010-10-29 17:29:13 +00008304/// Examine constraint type and operand type and determine a weight value.
8305/// This object must already have been set up with the operand type
8306/// and the current alternative constraint selected.
8307TargetLowering::ConstraintWeight
8308PPCTargetLowering::getSingleConstraintMatchWeight(
8309 AsmOperandInfo &info, const char *constraint) const {
8310 ConstraintWeight weight = CW_Invalid;
8311 Value *CallOperandVal = info.CallOperandVal;
8312 // If we don't have a value, we can't do a match,
8313 // but allow it at the lowest weight.
8314 if (CallOperandVal == NULL)
8315 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00008316 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00008317 // Look at the constraint type.
8318 switch (*constraint) {
8319 default:
8320 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8321 break;
8322 case 'b':
8323 if (type->isIntegerTy())
8324 weight = CW_Register;
8325 break;
8326 case 'f':
8327 if (type->isFloatTy())
8328 weight = CW_Register;
8329 break;
8330 case 'd':
8331 if (type->isDoubleTy())
8332 weight = CW_Register;
8333 break;
8334 case 'v':
8335 if (type->isVectorTy())
8336 weight = CW_Register;
8337 break;
8338 case 'y':
8339 weight = CW_Register;
8340 break;
Hal Finkel4f24c622012-11-05 18:18:42 +00008341 case 'Z':
8342 weight = CW_Memory;
8343 break;
John Thompsone8360b72010-10-29 17:29:13 +00008344 }
8345 return weight;
8346}
8347
Scott Michelcf0da6c2009-02-17 22:15:04 +00008348std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner584a11a2006-11-02 01:44:04 +00008349PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00008350 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +00008351 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +00008352 // GCC RS6000 Constraint Letters
8353 switch (Constraint[0]) {
8354 case 'b': // R1-R31
Hal Finkel638a9fa2013-03-19 18:51:05 +00008355 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
8356 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
8357 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008358 case 'r': // R0-R31
Owen Anderson9f944592009-08-11 20:47:22 +00008359 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +00008360 return std::make_pair(0U, &PPC::G8RCRegClass);
8361 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008362 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008363 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +00008364 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008365 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +00008366 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008367 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008368 case 'v':
Craig Topperabadc662012-04-20 06:31:50 +00008369 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008370 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +00008371 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008372 }
8373 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008374
Hal Finkelb176acb2013-08-03 12:25:10 +00008375 std::pair<unsigned, const TargetRegisterClass*> R =
8376 TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8377
8378 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
8379 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
8380 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
8381 // register.
8382 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
8383 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
8384 if (R.first && VT == MVT::i64 && PPCSubTarget.isPPC64() &&
8385 PPC::GPRCRegClass.contains(R.first)) {
8386 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
8387 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +00008388 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +00008389 &PPC::G8RCRegClass);
8390 }
8391
8392 return R;
Chris Lattner01513612006-01-31 19:20:21 +00008393}
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008394
Chris Lattner584a11a2006-11-02 01:44:04 +00008395
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008396/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +00008397/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +00008398void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00008399 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008400 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00008401 SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008402 SDValue Result(0,0);
Eric Christopher0713a9d2011-06-08 23:55:35 +00008403
Eric Christopherde9399b2011-06-02 23:16:42 +00008404 // Only support length 1 constraints.
8405 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008406
Eric Christopherde9399b2011-06-02 23:16:42 +00008407 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008408 switch (Letter) {
8409 default: break;
8410 case 'I':
8411 case 'J':
8412 case 'K':
8413 case 'L':
8414 case 'M':
8415 case 'N':
8416 case 'O':
8417 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +00008418 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008419 if (!CST) return; // Must be an immediate to match.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008420 unsigned Value = CST->getZExtValue();
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008421 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008422 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008423 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008424 if ((short)Value == (int)Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008425 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008426 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008427 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
8428 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008429 if ((short)Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008430 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008431 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008432 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008433 if ((Value >> 16) == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008434 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008435 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008436 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008437 if (Value > 31)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008438 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008439 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008440 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008441 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008442 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008443 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008444 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008445 if (Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008446 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008447 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008448 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008449 if ((short)-Value == (int)-Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008450 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008451 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008452 }
8453 break;
8454 }
8455 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008456
Gabor Greiff304a7a2008-08-28 21:40:38 +00008457 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008458 Ops.push_back(Result);
8459 return;
8460 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008461
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008462 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +00008463 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008464}
Evan Cheng2dd2c652006-03-13 23:20:37 +00008465
Chris Lattner1eb94d92007-03-30 23:15:24 +00008466// isLegalAddressingMode - Return true if the addressing mode represented
8467// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008468bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00008469 Type *Ty) const {
Chris Lattner1eb94d92007-03-30 23:15:24 +00008470 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelcf0da6c2009-02-17 22:15:04 +00008471
Chris Lattner1eb94d92007-03-30 23:15:24 +00008472 // PPC allows a sign-extended 16-bit immediate field.
8473 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
8474 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008475
Chris Lattner1eb94d92007-03-30 23:15:24 +00008476 // No global is ever allowed as a base.
8477 if (AM.BaseGV)
8478 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008479
8480 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +00008481 switch (AM.Scale) {
8482 case 0: // "r+i" or just "i", depending on HasBaseReg.
8483 break;
8484 case 1:
8485 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
8486 return false;
8487 // Otherwise we have r+r or r+i.
8488 break;
8489 case 2:
8490 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
8491 return false;
8492 // Allow 2*r as r+r.
8493 break;
Chris Lattner19ccd622007-04-09 22:10:05 +00008494 default:
8495 // No other scales are supported.
8496 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +00008497 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008498
Chris Lattner1eb94d92007-03-30 23:15:24 +00008499 return true;
8500}
8501
Dan Gohman21cea8a2010-04-17 15:26:15 +00008502SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
8503 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00008504 MachineFunction &MF = DAG.getMachineFunction();
8505 MachineFrameInfo *MFI = MF.getFrameInfo();
8506 MFI->setReturnAddressIsTaken(true);
8507
Bill Wendling908bf812014-01-06 00:43:20 +00008508 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008509 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008510
Andrew Trickef9de2a2013-05-25 02:42:55 +00008511 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008512 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +00008513
Dale Johannesen81bfca72010-05-03 22:59:34 +00008514 // Make sure the function does not optimize away the store of the RA to
8515 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +00008516 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008517 FuncInfo->setLRStoreRequired();
8518 bool isPPC64 = PPCSubTarget.isPPC64();
8519 bool isDarwinABI = PPCSubTarget.isDarwinABI();
8520
8521 if (Depth > 0) {
8522 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8523 SDValue Offset =
Wesley Peck527da1b2010-11-23 03:31:01 +00008524
Anton Korobeynikov2f931282011-01-10 12:39:04 +00008525 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen81bfca72010-05-03 22:59:34 +00008526 isPPC64? MVT::i64 : MVT::i32);
8527 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
8528 DAG.getNode(ISD::ADD, dl, getPointerTy(),
8529 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008530 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008531 }
Chris Lattnerf6a81562007-12-08 06:59:59 +00008532
Chris Lattnerf6a81562007-12-08 06:59:59 +00008533 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008534 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008535 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008536 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +00008537}
8538
Dan Gohman21cea8a2010-04-17 15:26:15 +00008539SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
8540 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008541 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008542 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00008543
Owen Anderson53aa7a92009-08-10 22:56:29 +00008544 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00008545 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008546
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008547 MachineFunction &MF = DAG.getMachineFunction();
8548 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008549 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +00008550
8551 // Naked functions never have a frame pointer, and so we use r1. For all
8552 // other functions, this decision must be delayed until during PEI.
8553 unsigned FrameReg;
8554 if (MF.getFunction()->getAttributes().hasAttribute(
8555 AttributeSet::FunctionIndex, Attribute::Naked))
8556 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
8557 else
8558 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
8559
Dale Johannesen81bfca72010-05-03 22:59:34 +00008560 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
8561 PtrVT);
8562 while (Depth--)
8563 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008564 FrameAddr, MachinePointerInfo(), false, false,
8565 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008566 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008567}
Dan Gohmanc14e5222008-10-21 03:41:46 +00008568
8569bool
8570PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8571 // The PowerPC target isn't yet aware of offsets.
8572 return false;
8573}
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008574
Evan Chengd9929f02010-04-01 20:10:42 +00008575/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +00008576/// and store operations as a result of memset, memcpy, and memmove
8577/// lowering. If DstAlign is zero that means it's safe to destination
8578/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
8579/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +00008580/// probably because the source does not need to be loaded. If 'IsMemset' is
8581/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
8582/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
8583/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +00008584/// It returns EVT::Other if the type should be determined using generic
8585/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +00008586EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
8587 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00008588 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00008589 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +00008590 MachineFunction &MF) const {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008591 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +00008592 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008593 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00008594 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008595 }
8596}
Hal Finkel88ed4e32012-04-01 19:23:08 +00008597
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008598bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +00008599 unsigned,
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008600 bool *Fast) const {
8601 if (DisablePPCUnaligned)
8602 return false;
8603
8604 // PowerPC supports unaligned memory access for simple non-vector types.
8605 // Although accessing unaligned addresses is not as efficient as accessing
8606 // aligned addresses, it is generally more efficient than manual expansion,
8607 // and generally only traps for software emulation when crossing page
8608 // boundaries.
8609
8610 if (!VT.isSimple())
8611 return false;
8612
8613 if (VT.getSimpleVT().isVector())
8614 return false;
8615
8616 if (VT == MVT::ppcf128)
8617 return false;
8618
8619 if (Fast)
8620 *Fast = true;
8621
8622 return true;
8623}
8624
Stephen Lin73de7bf2013-07-09 18:16:56 +00008625bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
8626 VT = VT.getScalarType();
8627
Hal Finkel0a479ae2012-06-22 00:49:52 +00008628 if (!VT.isSimple())
8629 return false;
8630
8631 switch (VT.getSimpleVT().SimpleTy) {
8632 case MVT::f32:
8633 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +00008634 return true;
8635 default:
8636 break;
8637 }
8638
8639 return false;
8640}
8641
Hal Finkel88ed4e32012-04-01 19:23:08 +00008642Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel21442b22013-09-11 23:05:25 +00008643 if (DisableILPPref || PPCSubTarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008644 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +00008645
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008646 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +00008647}
8648
Bill Schmidt0cf702f2013-07-30 00:50:39 +00008649// Create a fast isel object.
8650FastISel *
8651PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
8652 const TargetLibraryInfo *LibInfo) const {
8653 return PPC::createFastISel(FuncInfo, LibInfo);
8654}