blob: 630385cc7c0c564ee46fb1c668f504852233031d [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"
Bill Wendlingdf7dd282014-01-05 01:47:20 +000033#include "llvm/IR/LLVMContext.h"
Chris Lattnerce645542006-11-10 02:08:47 +000034#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000035#include "llvm/Support/ErrorHandling.h"
Craig Topperb25fda92012-03-17 18:46:09 +000036#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000037#include "llvm/Support/raw_ostream.h"
Craig Topperb25fda92012-03-17 18:46:09 +000038#include "llvm/Target/TargetOptions.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000039using namespace llvm;
40
Hal Finkel595817e2012-06-04 02:21:00 +000041static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
42cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000043
Hal Finkel4e9f1a82012-06-10 19:32:29 +000044static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
45cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
46
Hal Finkel8d7fbc92013-03-15 15:27:13 +000047static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
48cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
49
Chris Lattner5e693ed2009-07-28 03:13:23 +000050static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) {
51 if (TM.getSubtargetImpl()->isDarwin())
Bill Wendlingbbcaa402010-03-15 21:09:38 +000052 return new TargetLoweringObjectFileMachO();
Bill Wendlingdd3fe942010-03-12 02:00:43 +000053
Bill Schmidt22d40dc2013-05-13 19:34:37 +000054 if (TM.getSubtargetImpl()->isSVR4ABI())
55 return new PPC64LinuxTargetObjectFile();
56
Bruno Cardoso Lopes62e6a8b2009-08-13 23:30:21 +000057 return new TargetLoweringObjectFileELF();
Chris Lattner5e693ed2009-07-28 03:13:23 +000058}
59
Chris Lattner584a11a2006-11-02 01:44:04 +000060PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Chris Lattner5e693ed2009-07-28 03:13:23 +000061 : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) {
Evan Cheng39e90022012-07-02 22:39:56 +000062 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
Scott Michelcf0da6c2009-02-17 22:15:04 +000063
Nate Begeman4dd38312005-10-21 00:02:42 +000064 setPow2DivIsCheap();
Dale Johannesenc31eb202008-07-31 18:13:12 +000065
Chris Lattnera028e7a2005-09-27 22:18:25 +000066 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000067 setUseUnderscoreSetJmp(true);
68 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000069
Chris Lattnerd10babf2010-10-10 18:34:00 +000070 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
71 // arguments are at least 4/8 bytes aligned.
Evan Cheng39e90022012-07-02 22:39:56 +000072 bool isPPC64 = Subtarget->isPPC64();
73 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000074
Chris Lattnerf22556d2005-08-16 17:14:42 +000075 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000076 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
77 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
78 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +000079
Evan Cheng5d9fd972006-10-04 00:56:09 +000080 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson9f944592009-08-11 20:47:22 +000081 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
82 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sands95d46ef2008-01-23 20:39:46 +000083
Owen Anderson9f944592009-08-11 20:47:22 +000084 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000085
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000086 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000087 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
88 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
89 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
90 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
91 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
92 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
93 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
94 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
95 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
96 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +000097
Dale Johannesen666323e2007-10-10 01:01:31 +000098 // This is used in the ppcf128->int sequence. Note it has different semantics
99 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +0000100 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000101
Roman Divacky1faf5b02012-08-16 18:19:29 +0000102 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000103 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
104 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
105 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
106 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
107 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000108 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000109
Chris Lattnerf22556d2005-08-16 17:14:42 +0000110 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000111 setOperationAction(ISD::SREM, MVT::i32, Expand);
112 setOperationAction(ISD::UREM, MVT::i32, Expand);
113 setOperationAction(ISD::SREM, MVT::i64, Expand);
114 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000115
116 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000117 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
118 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
119 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
120 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
121 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
122 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
123 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
124 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000125
Dan Gohman482732a2007-10-11 23:21:31 +0000126 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000127 setOperationAction(ISD::FSIN , MVT::f64, Expand);
128 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000129 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000130 setOperationAction(ISD::FREM , MVT::f64, Expand);
131 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000132 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000133 setOperationAction(ISD::FSIN , MVT::f32, Expand);
134 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000135 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000136 setOperationAction(ISD::FREM , MVT::f32, Expand);
137 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000138 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000139
Owen Anderson9f944592009-08-11 20:47:22 +0000140 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000141
Chris Lattnerf22556d2005-08-16 17:14:42 +0000142 // If we're enabling GP optimizations, use hardware square root
Hal Finkel2e103312013-04-03 04:01:11 +0000143 if (!Subtarget->hasFSQRT() &&
144 !(TM.Options.UnsafeFPMath &&
145 Subtarget->hasFRSQRTE() && Subtarget->hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000146 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000147
148 if (!Subtarget->hasFSQRT() &&
149 !(TM.Options.UnsafeFPMath &&
150 Subtarget->hasFRSQRTES() && Subtarget->hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000151 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000152
Hal Finkeldbc78e12013-08-19 05:01:02 +0000153 if (Subtarget->hasFCPSGN()) {
154 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
155 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
156 } else {
157 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
158 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
159 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000160
Hal Finkelc20a08d2013-03-29 08:57:48 +0000161 if (Subtarget->hasFPRND()) {
162 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
163 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
164 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000165 setOperationAction(ISD::FROUND, MVT::f64, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000166
167 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
168 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
169 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000170 setOperationAction(ISD::FROUND, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000171 }
172
Nate Begeman2fba8a32006-01-14 03:14:10 +0000173 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000174 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000175 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000176 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
177 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000178 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000179 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000180 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
181 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000182
Hal Finkela4d07482013-03-28 13:29:47 +0000183 if (Subtarget->hasPOPCNTD()) {
Hal Finkel290376d2013-04-01 15:58:15 +0000184 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000185 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
186 } else {
187 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
188 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
189 }
190
Nate Begeman1b8121b2006-01-11 21:21:00 +0000191 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000192 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
193 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000194
Chris Lattnerf22556d2005-08-16 17:14:42 +0000195 // PowerPC does not have Select
Owen Anderson9f944592009-08-11 20:47:22 +0000196 setOperationAction(ISD::SELECT, MVT::i32, Expand);
197 setOperationAction(ISD::SELECT, MVT::i64, Expand);
198 setOperationAction(ISD::SELECT, MVT::f32, Expand);
199 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000200
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000201 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000202 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
203 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000204
Nate Begeman7e7f4392006-02-01 07:19:44 +0000205 // PowerPC wants to optimize integer setcc a bit
Owen Anderson9f944592009-08-11 20:47:22 +0000206 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000207
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000208 // PowerPC does not have BRCOND which requires SetCC
Owen Anderson9f944592009-08-11 20:47:22 +0000209 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000210
Owen Anderson9f944592009-08-11 20:47:22 +0000211 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000212
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000213 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000214 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000215
Jim Laskey6267b2c2005-08-17 00:40:22 +0000216 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000217 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
218 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000219
Wesley Peck527da1b2010-11-23 03:31:01 +0000220 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
221 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
222 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
223 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattnerc46fc242005-12-23 05:13:35 +0000224
Chris Lattner84b49d52006-04-28 21:56:10 +0000225 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000226 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000227
Hal Finkel1996f3d2013-03-27 19:10:42 +0000228 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000229 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
230 // support continuation, user-level threading, and etc.. As a result, no
231 // other SjLj exception interfaces are implemented and please don't build
232 // your own exception handling based on them.
233 // LLVM/Clang supports zero-cost DWARF exception handling.
234 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
235 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000236
237 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000238 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000239 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
240 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000241 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000242 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
243 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
244 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
245 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000246 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000247 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
248 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000249
Nate Begemanf69d13b2008-08-11 17:36:31 +0000250 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000251 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000252
253 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000254 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
255 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000256
Nate Begemane74795c2006-01-25 18:21:52 +0000257 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000258 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000259
Evan Cheng39e90022012-07-02 22:39:56 +0000260 if (Subtarget->isSVR4ABI()) {
261 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000262 // VAARG always uses double-word chunks, so promote anything smaller.
263 setOperationAction(ISD::VAARG, MVT::i1, Promote);
264 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
265 setOperationAction(ISD::VAARG, MVT::i8, Promote);
266 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
267 setOperationAction(ISD::VAARG, MVT::i16, Promote);
268 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
269 setOperationAction(ISD::VAARG, MVT::i32, Promote);
270 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
271 setOperationAction(ISD::VAARG, MVT::Other, Expand);
272 } else {
273 // VAARG is custom lowered with the 32-bit SVR4 ABI.
274 setOperationAction(ISD::VAARG, MVT::Other, Custom);
275 setOperationAction(ISD::VAARG, MVT::i64, Custom);
276 }
Roman Divacky4394e682011-06-28 15:30:42 +0000277 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000278 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000279
Roman Divackyc3825df2013-07-25 21:36:47 +0000280 if (Subtarget->isSVR4ABI() && !isPPC64)
281 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
282 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
283 else
284 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
285
Chris Lattner5bd514d2006-01-15 09:02:48 +0000286 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000287 setOperationAction(ISD::VAEND , MVT::Other, Expand);
288 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
289 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
290 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
291 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000292
Chris Lattner6961fc72006-03-26 10:06:40 +0000293 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000294 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000295
Hal Finkel25c19922013-05-15 21:37:41 +0000296 // To handle counter-based loop conditions.
297 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
298
Dale Johannesen160be0f2008-11-07 22:54:33 +0000299 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000300 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
301 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
302 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
303 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
304 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
305 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
306 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
307 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
308 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
309 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
310 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
311 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000312
Evan Cheng39e90022012-07-02 22:39:56 +0000313 if (Subtarget->has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000314 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000315 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
316 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
317 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
318 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000319 // This is just the low 32 bits of a (signed) fp->i64 conversion.
320 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000321 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000322
Hal Finkelf6d45f22013-04-01 17:52:07 +0000323 if (PPCSubTarget.hasLFIWAX() || Subtarget->isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000324 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000325 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000326 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000328 }
329
Hal Finkelf6d45f22013-04-01 17:52:07 +0000330 // With the instructions enabled under FPCVT, we can do everything.
331 if (PPCSubTarget.hasFPCVT()) {
332 if (Subtarget->has64BitSupport()) {
333 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
334 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
335 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
336 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
337 }
338
339 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
340 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
341 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
342 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
343 }
344
Evan Cheng39e90022012-07-02 22:39:56 +0000345 if (Subtarget->use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000346 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000347 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000348 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000349 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000350 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000351 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
352 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
353 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000354 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000355 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000356 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
357 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
358 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000359 }
Evan Cheng19264272006-03-01 01:11:20 +0000360
Evan Cheng39e90022012-07-02 22:39:56 +0000361 if (Subtarget->hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000362 // First set operation action for all vector types to expand. Then we
363 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson9f944592009-08-11 20:47:22 +0000364 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
365 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
366 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands13237ac2008-06-06 12:08:01 +0000367
Chris Lattner06a21ba2006-04-16 01:37:57 +0000368 // add/sub are legal for all supported vector VT's.
Duncan Sands13237ac2008-06-06 12:08:01 +0000369 setOperationAction(ISD::ADD , VT, Legal);
370 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000371
Chris Lattner95c7adc2006-04-04 17:25:31 +0000372 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000373 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000374 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000375
376 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000377 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000378 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000379 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000380 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000381 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000382 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000383 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000384 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000385 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000386 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000387 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000388 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000389
Chris Lattner06a21ba2006-04-16 01:37:57 +0000390 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000391 setOperationAction(ISD::MUL , VT, Expand);
392 setOperationAction(ISD::SDIV, VT, Expand);
393 setOperationAction(ISD::SREM, VT, Expand);
394 setOperationAction(ISD::UDIV, VT, Expand);
395 setOperationAction(ISD::UREM, VT, Expand);
396 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000397 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000398 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000399 setOperationAction(ISD::FSQRT, VT, Expand);
400 setOperationAction(ISD::FLOG, VT, Expand);
401 setOperationAction(ISD::FLOG10, VT, Expand);
402 setOperationAction(ISD::FLOG2, VT, Expand);
403 setOperationAction(ISD::FEXP, VT, Expand);
404 setOperationAction(ISD::FEXP2, VT, Expand);
405 setOperationAction(ISD::FSIN, VT, Expand);
406 setOperationAction(ISD::FCOS, VT, Expand);
407 setOperationAction(ISD::FABS, VT, Expand);
408 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000409 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000410 setOperationAction(ISD::FCEIL, VT, Expand);
411 setOperationAction(ISD::FTRUNC, VT, Expand);
412 setOperationAction(ISD::FRINT, VT, Expand);
413 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000414 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
415 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
416 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
417 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
418 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
419 setOperationAction(ISD::UDIVREM, VT, Expand);
420 setOperationAction(ISD::SDIVREM, VT, Expand);
421 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
422 setOperationAction(ISD::FPOW, VT, Expand);
423 setOperationAction(ISD::CTPOP, VT, Expand);
424 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000425 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000426 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000427 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000428 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000429 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
430
431 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
432 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
433 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
434 setTruncStoreAction(VT, InnerVT, Expand);
435 }
436 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
437 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
438 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000439 }
440
Chris Lattner95c7adc2006-04-04 17:25:31 +0000441 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
442 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000443 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000444
Owen Anderson9f944592009-08-11 20:47:22 +0000445 setOperationAction(ISD::AND , MVT::v4i32, Legal);
446 setOperationAction(ISD::OR , MVT::v4i32, Legal);
447 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
448 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
449 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
450 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000451 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
452 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
453 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
454 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000455 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
456 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
457 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
458 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000459
Craig Topperabadc662012-04-20 06:31:50 +0000460 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
461 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
462 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
463 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000464
Owen Anderson9f944592009-08-11 20:47:22 +0000465 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000466 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000467
468 if (TM.Options.UnsafeFPMath) {
469 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
470 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
471 }
472
Owen Anderson9f944592009-08-11 20:47:22 +0000473 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
474 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
475 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000476
Owen Anderson9f944592009-08-11 20:47:22 +0000477 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
478 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000479
Owen Anderson9f944592009-08-11 20:47:22 +0000480 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
481 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
482 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
483 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000484
485 // Altivec does not contain unordered floating-point compare instructions
486 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
487 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
488 setCondCodeAction(ISD::SETUGT, MVT::v4f32, Expand);
489 setCondCodeAction(ISD::SETUGE, MVT::v4f32, Expand);
490 setCondCodeAction(ISD::SETULT, MVT::v4f32, Expand);
491 setCondCodeAction(ISD::SETULE, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000492
493 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
494 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000495 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000496
Hal Finkel70381a72012-08-04 14:10:46 +0000497 if (Subtarget->has64BitSupport()) {
Hal Finkel322e41a2012-04-01 20:08:17 +0000498 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel70381a72012-08-04 14:10:46 +0000499 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
500 }
Hal Finkel322e41a2012-04-01 20:08:17 +0000501
Eli Friedman7dfa7912011-08-29 18:23:02 +0000502 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
503 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkel1b5ff082012-12-25 17:22:53 +0000504 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
505 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000506
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000507 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidta76bf5a2013-04-23 18:49:44 +0000508 // Altivec instructions set fields to all zeros or all ones.
509 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000510
Evan Cheng39e90022012-07-02 22:39:56 +0000511 if (isPPC64) {
Chris Lattner454436d2006-10-18 01:20:43 +0000512 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000513 setExceptionPointerRegister(PPC::X3);
514 setExceptionSelectorRegister(PPC::X4);
515 } else {
Chris Lattner454436d2006-10-18 01:20:43 +0000516 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000517 setExceptionPointerRegister(PPC::R3);
518 setExceptionSelectorRegister(PPC::R4);
519 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000520
Chris Lattnerf4184352006-03-01 04:57:39 +0000521 // We have target-specific dag combine patterns for the following nodes:
522 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000523 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000524 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000525 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnera7976d32006-07-10 20:56:58 +0000526 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000527 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000528
Hal Finkel2e103312013-04-03 04:01:11 +0000529 // Use reciprocal estimates.
530 if (TM.Options.UnsafeFPMath) {
531 setTargetDAGCombine(ISD::FDIV);
532 setTargetDAGCombine(ISD::FSQRT);
533 }
534
Dale Johannesen10432e52007-10-19 00:59:18 +0000535 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng39e90022012-07-02 22:39:56 +0000536 if (Subtarget->isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000537 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000538 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
539 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000540 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
541 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000542 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
543 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
544 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
545 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
546 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000547 }
548
Hal Finkel65298572011-10-17 18:53:03 +0000549 setMinFunctionAlignment(2);
550 if (PPCSubTarget.isDarwin())
551 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000552
Evan Cheng39e90022012-07-02 22:39:56 +0000553 if (isPPC64 && Subtarget->isJITCodeModel())
554 // Temporary workaround for the inability of PPC64 JIT to handle jump
555 // tables.
556 setSupportJumpTables(false);
557
Eli Friedman30a49e92011-08-03 21:06:02 +0000558 setInsertFencesForAtomic(true);
559
Hal Finkel21442b22013-09-11 23:05:25 +0000560 if (Subtarget->enableMachineScheduler())
561 setSchedulingPreference(Sched::Source);
562 else
563 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000564
Chris Lattnerf22556d2005-08-16 17:14:42 +0000565 computeRegisterProperties();
Hal Finkel742b5352012-08-28 16:12:39 +0000566
567 // The Freescale cores does better with aggressive inlining of memcpy and
568 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
569 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
570 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000571 MaxStoresPerMemset = 32;
572 MaxStoresPerMemsetOptSize = 16;
573 MaxStoresPerMemcpy = 32;
574 MaxStoresPerMemcpyOptSize = 8;
575 MaxStoresPerMemmove = 32;
576 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel742b5352012-08-28 16:12:39 +0000577
578 setPrefFunctionAlignment(4);
Hal Finkel742b5352012-08-28 16:12:39 +0000579 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000580}
581
Hal Finkel262a2242013-09-12 23:20:06 +0000582/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
583/// the desired ByVal argument alignment.
584static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
585 unsigned MaxMaxAlign) {
586 if (MaxAlign == MaxMaxAlign)
587 return;
588 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
589 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
590 MaxAlign = 32;
591 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
592 MaxAlign = 16;
593 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
594 unsigned EltAlign = 0;
595 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
596 if (EltAlign > MaxAlign)
597 MaxAlign = EltAlign;
598 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
599 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
600 unsigned EltAlign = 0;
601 getMaxByValAlign(STy->getElementType(i), EltAlign, MaxMaxAlign);
602 if (EltAlign > MaxAlign)
603 MaxAlign = EltAlign;
604 if (MaxAlign == MaxMaxAlign)
605 break;
606 }
607 }
608}
609
Dale Johannesencbde4c22008-02-28 22:31:51 +0000610/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
611/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000612unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000613 // Darwin passes everything on 4 byte boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000614 if (PPCSubTarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000615 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000616
617 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000618 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000619 unsigned Align = PPCSubTarget.isPPC64() ? 8 : 4;
620 if (PPCSubTarget.hasAltivec() || PPCSubTarget.hasQPX())
621 getMaxByValAlign(Ty, Align, PPCSubTarget.hasQPX() ? 32 : 16);
622 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000623}
624
Chris Lattner347ed8a2006-01-09 23:52:17 +0000625const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
626 switch (Opcode) {
627 default: return 0;
Evan Cheng32e376f2008-07-12 02:23:19 +0000628 case PPCISD::FSEL: return "PPCISD::FSEL";
629 case PPCISD::FCFID: return "PPCISD::FCFID";
630 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
631 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000632 case PPCISD::FRE: return "PPCISD::FRE";
633 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000634 case PPCISD::STFIWX: return "PPCISD::STFIWX";
635 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
636 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
637 case PPCISD::VPERM: return "PPCISD::VPERM";
638 case PPCISD::Hi: return "PPCISD::Hi";
639 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000640 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller79fef932009-12-18 13:00:15 +0000641 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
642 case PPCISD::LOAD: return "PPCISD::LOAD";
643 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000644 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
645 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
646 case PPCISD::SRL: return "PPCISD::SRL";
647 case PPCISD::SRA: return "PPCISD::SRA";
648 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000649 case PPCISD::CALL: return "PPCISD::CALL";
650 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000651 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000652 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng32e376f2008-07-12 02:23:19 +0000653 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel756810f2013-03-21 21:37:52 +0000654 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
655 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000656 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng32e376f2008-07-12 02:23:19 +0000657 case PPCISD::VCMP: return "PPCISD::VCMP";
658 case PPCISD::VCMPo: return "PPCISD::VCMPo";
659 case PPCISD::LBRX: return "PPCISD::LBRX";
660 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng32e376f2008-07-12 02:23:19 +0000661 case PPCISD::LARX: return "PPCISD::LARX";
662 case PPCISD::STCX: return "PPCISD::STCX";
663 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +0000664 case PPCISD::BDNZ: return "PPCISD::BDNZ";
665 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000666 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000667 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000668 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +0000669 case PPCISD::CR6SET: return "PPCISD::CR6SET";
670 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34627e32012-11-27 17:35:46 +0000671 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
672 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
673 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Roman Divacky32143e22013-12-20 18:08:54 +0000674 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000675 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
676 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000677 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000678 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
679 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
680 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000681 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
682 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
683 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
684 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
685 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +0000686 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +0000687 case PPCISD::SC: return "PPCISD::SC";
Chris Lattner347ed8a2006-01-09 23:52:17 +0000688 }
689}
690
Matt Arsenault758659232013-05-18 00:21:46 +0000691EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000692 if (!VT.isVector())
693 return MVT::i32;
694 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000695}
696
Chris Lattner4211ca92006-04-14 06:01:58 +0000697//===----------------------------------------------------------------------===//
698// Node matching predicates, for use by the tblgen matching code.
699//===----------------------------------------------------------------------===//
700
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000701/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000702static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000703 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000704 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000705 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000706 // Maybe this has already been legalized into the constant pool?
707 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000708 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000709 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000710 }
711 return false;
712}
713
Chris Lattnere8b83b42006-04-06 17:23:16 +0000714/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
715/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000716static bool isConstantOrUndef(int Op, int Val) {
717 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000718}
719
720/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
721/// VPKUHUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000722bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000723 if (!isUnary) {
724 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000725 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000726 return false;
727 } else {
728 for (unsigned i = 0; i != 8; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000729 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
730 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000731 return false;
732 }
Chris Lattner1d338192006-04-06 18:26:28 +0000733 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000734}
735
736/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
737/// VPKUWUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000738bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000739 if (!isUnary) {
740 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000741 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
742 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000743 return false;
744 } else {
745 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000746 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
747 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
748 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
749 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000750 return false;
751 }
Chris Lattner1d338192006-04-06 18:26:28 +0000752 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000753}
754
Chris Lattnerf38e0332006-04-06 22:02:42 +0000755/// isVMerge - Common function, used to match vmrg* shuffles.
756///
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000757static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +0000758 unsigned LHSStart, unsigned RHSStart) {
Owen Anderson9f944592009-08-11 20:47:22 +0000759 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000760 "PPC only supports shuffles by bytes!");
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000761 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
762 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000763
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000764 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
765 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000766 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000767 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000768 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000769 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000770 return false;
771 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000772 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +0000773}
774
775/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
776/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000777bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000778 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000779 if (!isUnary)
780 return isVMerge(N, UnitSize, 8, 24);
781 return isVMerge(N, UnitSize, 8, 8);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000782}
783
784/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
785/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000786bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000787 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000788 if (!isUnary)
789 return isVMerge(N, UnitSize, 0, 16);
790 return isVMerge(N, UnitSize, 0, 0);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000791}
792
793
Chris Lattner1d338192006-04-06 18:26:28 +0000794/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
795/// amount, otherwise return -1.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000796int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Owen Anderson9f944592009-08-11 20:47:22 +0000797 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000798 "PPC only supports shuffles by bytes!");
799
800 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +0000801
Chris Lattner1d338192006-04-06 18:26:28 +0000802 // Find the first non-undef value in the shuffle mask.
803 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000804 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +0000805 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000806
Chris Lattner1d338192006-04-06 18:26:28 +0000807 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000808
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000809 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +0000810 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000811 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +0000812 if (ShiftAmt < i) return -1;
813 ShiftAmt -= i;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000814
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000815 if (!isUnary) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000816 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000817 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000818 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000819 return -1;
820 } else {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000821 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000822 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000823 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000824 return -1;
825 }
Chris Lattner1d338192006-04-06 18:26:28 +0000826 return ShiftAmt;
827}
Chris Lattnerffc47562006-03-20 06:33:01 +0000828
829/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
830/// specifies a splat of a single element that is suitable for input to
831/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000832bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +0000833 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +0000834 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000835
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000836 // This is a splat operation if each element of the permute is the same, and
837 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000838 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +0000839
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000840 // FIXME: Handle UNDEF elements too!
841 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +0000842 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000843
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000844 // Check that the indices are consecutive, in the case of a multi-byte element
845 // splatted with a v16i8 mask.
846 for (unsigned i = 1; i != EltSize; ++i)
847 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000848 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000849
Chris Lattner95c7adc2006-04-04 17:25:31 +0000850 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000851 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +0000852 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000853 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000854 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000855 }
Chris Lattner95c7adc2006-04-04 17:25:31 +0000856 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +0000857}
858
Evan Cheng581d2792007-07-30 07:51:22 +0000859/// isAllNegativeZeroVector - Returns true if all elements of build_vector
860/// are -0.0.
861bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000862 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
863
864 APInt APVal, APUndef;
865 unsigned BitSize;
866 bool HasAnyUndefs;
Wesley Peck527da1b2010-11-23 03:31:01 +0000867
Dale Johannesen5f4eecf2009-11-13 01:45:18 +0000868 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000869 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000870 return CFP->getValueAPF().isNegZero();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000871
Evan Cheng581d2792007-07-30 07:51:22 +0000872 return false;
873}
874
Chris Lattnerffc47562006-03-20 06:33:01 +0000875/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
876/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner95c7adc2006-04-04 17:25:31 +0000877unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000878 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
879 assert(isSplatShuffleMask(SVOp, EltSize));
880 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +0000881}
882
Chris Lattner74cf9ff2006-04-12 17:37:20 +0000883/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +0000884/// by using a vspltis[bhw] instruction of the specified element size, return
885/// the constant being splatted. The ByteSize field indicates the number of
886/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000887SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
888 SDValue OpVal(0, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000889
890 // If ByteSize of the splat is bigger than the element size of the
891 // build_vector, then we have a case where we are checking for a splat where
892 // multiple elements of the buildvector are folded together into a single
893 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
894 unsigned EltSize = 16/N->getNumOperands();
895 if (EltSize < ByteSize) {
896 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000897 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000898 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000899
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000900 // See if all of the elements in the buildvector agree across.
901 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
902 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
903 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000904 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000905
Scott Michelcf0da6c2009-02-17 22:15:04 +0000906
Gabor Greiff304a7a2008-08-28 21:40:38 +0000907 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000908 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
909 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000910 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000911 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000912
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000913 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
914 // either constant or undef values that are identical for each chunk. See
915 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000916
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000917 // Check to see if all of the leading entries are either 0 or -1. If
918 // neither, then this won't fit into the immediate field.
919 bool LeadingZero = true;
920 bool LeadingOnes = true;
921 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000922 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000923
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000924 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
925 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
926 }
927 // Finally, check the least significant entry.
928 if (LeadingZero) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000929 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +0000930 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +0000931 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000932 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +0000933 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000934 }
935 if (LeadingOnes) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000936 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +0000937 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +0000938 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000939 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +0000940 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000941 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000942
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000943 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000944 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000945
Chris Lattner2771e2c2006-03-25 06:12:06 +0000946 // Check to see if this buildvec has a single non-undef value in its elements.
947 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
948 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greiff304a7a2008-08-28 21:40:38 +0000949 if (OpVal.getNode() == 0)
Chris Lattner2771e2c2006-03-25 06:12:06 +0000950 OpVal = N->getOperand(i);
951 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000952 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000953 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000954
Gabor Greiff304a7a2008-08-28 21:40:38 +0000955 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000956
Eli Friedman9c6ab1a2009-05-24 02:03:36 +0000957 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +0000958 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +0000959 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000960 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000961 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +0000962 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000963 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +0000964 }
965
966 // If the splat value is larger than the element value, then we can never do
967 // this splat. The only case that we could fit the replicated bits into our
968 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000969 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000970
Chris Lattner2771e2c2006-03-25 06:12:06 +0000971 // If the element value is larger than the splat value, cut it in half and
972 // check to see if the two halves are equal. Continue doing this until we
973 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
974 while (ValSizeInBytes > ByteSize) {
975 ValSizeInBytes >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000976
Chris Lattner2771e2c2006-03-25 06:12:06 +0000977 // If the top half equals the bottom half, we're still ok.
Chris Lattner39cc7172006-04-05 17:39:25 +0000978 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
979 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000980 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000981 }
982
983 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +0000984 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000985
Evan Chengb1ddc982006-03-26 09:52:32 +0000986 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000987 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000988
Chris Lattnerd71a1f92006-04-08 06:46:53 +0000989 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +0000990 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +0000991 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000992 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000993}
994
Chris Lattner4211ca92006-04-14 06:01:58 +0000995//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +0000996// Addressing Mode Selection
997//===----------------------------------------------------------------------===//
998
999/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1000/// or 64-bit immediate, and if the value can be accurately represented as a
1001/// sign extension from a 16-bit value. If so, this returns true and the
1002/// immediate.
1003static bool isIntS16Immediate(SDNode *N, short &Imm) {
1004 if (N->getOpcode() != ISD::Constant)
1005 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001006
Dan Gohmaneffb8942008-09-12 16:56:44 +00001007 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001008 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001009 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001010 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001011 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001012}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001013static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001014 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001015}
1016
1017
1018/// SelectAddressRegReg - Given the specified addressed, check to see if it
1019/// can be represented as an indexed [r+r] operation. Returns false if it
1020/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001021bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1022 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001023 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001024 short imm = 0;
1025 if (N.getOpcode() == ISD::ADD) {
1026 if (isIntS16Immediate(N.getOperand(1), imm))
1027 return false; // r+i
1028 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1029 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001030
Chris Lattnera801fced2006-11-08 02:15:41 +00001031 Base = N.getOperand(0);
1032 Index = N.getOperand(1);
1033 return true;
1034 } else if (N.getOpcode() == ISD::OR) {
1035 if (isIntS16Immediate(N.getOperand(1), imm))
1036 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001037
Chris Lattnera801fced2006-11-08 02:15:41 +00001038 // If this is an or of disjoint bitfields, we can codegen this as an add
1039 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1040 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001041 APInt LHSKnownZero, LHSKnownOne;
1042 APInt RHSKnownZero, RHSKnownOne;
1043 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001044 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001045
Dan Gohmanf19609a2008-02-27 01:23:58 +00001046 if (LHSKnownZero.getBoolValue()) {
1047 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001048 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001049 // If all of the bits are known zero on the LHS or RHS, the add won't
1050 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001051 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001052 Base = N.getOperand(0);
1053 Index = N.getOperand(1);
1054 return true;
1055 }
1056 }
1057 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001058
Chris Lattnera801fced2006-11-08 02:15:41 +00001059 return false;
1060}
1061
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001062// If we happen to be doing an i64 load or store into a stack slot that has
1063// less than a 4-byte alignment, then the frame-index elimination may need to
1064// use an indexed load or store instruction (because the offset may not be a
1065// multiple of 4). The extra register needed to hold the offset comes from the
1066// register scavenger, and it is possible that the scavenger will need to use
1067// an emergency spill slot. As a result, we need to make sure that a spill slot
1068// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1069// stack slot.
1070static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1071 // FIXME: This does not handle the LWA case.
1072 if (VT != MVT::i64)
1073 return;
1074
Hal Finkel7ab3db52013-07-10 15:29:01 +00001075 // NOTE: We'll exclude negative FIs here, which come from argument
1076 // lowering, because there are no known test cases triggering this problem
1077 // using packed structures (or similar). We can remove this exclusion if
1078 // we find such a test case. The reason why this is so test-case driven is
1079 // because this entire 'fixup' is only to prevent crashes (from the
1080 // register scavenger) on not-really-valid inputs. For example, if we have:
1081 // %a = alloca i1
1082 // %b = bitcast i1* %a to i64*
1083 // store i64* a, i64 b
1084 // then the store should really be marked as 'align 1', but is not. If it
1085 // were marked as 'align 1' then the indexed form would have been
1086 // instruction-selected initially, and the problem this 'fixup' is preventing
1087 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001088 if (FrameIdx < 0)
1089 return;
1090
1091 MachineFunction &MF = DAG.getMachineFunction();
1092 MachineFrameInfo *MFI = MF.getFrameInfo();
1093
1094 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1095 if (Align >= 4)
1096 return;
1097
1098 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1099 FuncInfo->setHasNonRISpills();
1100}
1101
Chris Lattnera801fced2006-11-08 02:15:41 +00001102/// Returns true if the address N can be represented by a base register plus
1103/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001104/// represented as reg+reg. If Aligned is true, only accept displacements
1105/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001106bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001107 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001108 SelectionDAG &DAG,
1109 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001110 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001111 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001112 // If this can be more profitably realized as r+r, fail.
1113 if (SelectAddressRegReg(N, Disp, Base, DAG))
1114 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001115
Chris Lattnera801fced2006-11-08 02:15:41 +00001116 if (N.getOpcode() == ISD::ADD) {
1117 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001118 if (isIntS16Immediate(N.getOperand(1), imm) &&
1119 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001120 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001121 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1122 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001123 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001124 } else {
1125 Base = N.getOperand(0);
1126 }
1127 return true; // [r+i]
1128 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1129 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001130 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001131 && "Cannot handle constant offsets yet!");
1132 Disp = N.getOperand(1).getOperand(0); // The global address.
1133 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001134 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001135 Disp.getOpcode() == ISD::TargetConstantPool ||
1136 Disp.getOpcode() == ISD::TargetJumpTable);
1137 Base = N.getOperand(0);
1138 return true; // [&g+r]
1139 }
1140 } else if (N.getOpcode() == ISD::OR) {
1141 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001142 if (isIntS16Immediate(N.getOperand(1), imm) &&
1143 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001144 // If this is an or of disjoint bitfields, we can codegen this as an add
1145 // (for better address arithmetic) if the LHS and RHS of the OR are
1146 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001147 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001148 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001149
Dan Gohmanf19609a2008-02-27 01:23:58 +00001150 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001151 // If all of the bits are known zero on the LHS or RHS, the add won't
1152 // carry.
1153 Base = N.getOperand(0);
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001154 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001155 return true;
1156 }
1157 }
1158 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1159 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001160
Chris Lattnera801fced2006-11-08 02:15:41 +00001161 // If this address fits entirely in a 16-bit sext immediate field, codegen
1162 // this as "d, 0"
1163 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001164 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001165 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkelf70c41e2013-03-21 23:45:03 +00001166 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1167 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001168 return true;
1169 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001170
1171 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001172 if ((CN->getValueType(0) == MVT::i32 ||
1173 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1174 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001175 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001176
Chris Lattnera801fced2006-11-08 02:15:41 +00001177 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001178 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001179
Owen Anderson9f944592009-08-11 20:47:22 +00001180 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1181 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001182 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001183 return true;
1184 }
1185 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001186
Chris Lattnera801fced2006-11-08 02:15:41 +00001187 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001188 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001189 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001190 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1191 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001192 Base = N;
1193 return true; // [r+0]
1194}
1195
1196/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1197/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001198bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1199 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001200 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001201 // Check to see if we can easily represent this as an [r+r] address. This
1202 // will fail if it thinks that the address is more profitably represented as
1203 // reg+imm, e.g. where imm = 0.
1204 if (SelectAddressRegReg(N, Base, Index, DAG))
1205 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001206
Chris Lattnera801fced2006-11-08 02:15:41 +00001207 // If the operand is an addition, always emit this as [r+r], since this is
1208 // better (for code size, and execution, as the memop does the add for free)
1209 // than emitting an explicit add.
1210 if (N.getOpcode() == ISD::ADD) {
1211 Base = N.getOperand(0);
1212 Index = N.getOperand(1);
1213 return true;
1214 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001215
Chris Lattnera801fced2006-11-08 02:15:41 +00001216 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkelf70c41e2013-03-21 23:45:03 +00001217 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1218 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001219 Index = N;
1220 return true;
1221}
1222
Chris Lattnera801fced2006-11-08 02:15:41 +00001223/// getPreIndexedAddressParts - returns true by value, base pointer and
1224/// offset pointer and addressing mode by reference if the node's address
1225/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001226bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1227 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001228 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001229 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001230 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001231
Ulrich Weigande90b0222013-03-22 14:58:48 +00001232 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001233 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001234 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001235 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001236 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1237 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001238 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001239 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001240 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001241 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001242 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001243 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001244 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001245 } else
1246 return false;
1247
Chris Lattner68371252006-11-14 01:38:31 +00001248 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands13237ac2008-06-06 12:08:01 +00001249 if (VT.isVector())
Chris Lattner68371252006-11-14 01:38:31 +00001250 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001251
Ulrich Weigande90b0222013-03-22 14:58:48 +00001252 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1253
1254 // Common code will reject creating a pre-inc form if the base pointer
1255 // is a frame index, or if N is a store and the base pointer is either
1256 // the same as or a predecessor of the value being stored. Check for
1257 // those situations here, and try with swapped Base/Offset instead.
1258 bool Swap = false;
1259
1260 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1261 Swap = true;
1262 else if (!isLoad) {
1263 SDValue Val = cast<StoreSDNode>(N)->getValue();
1264 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1265 Swap = true;
1266 }
1267
1268 if (Swap)
1269 std::swap(Base, Offset);
1270
Hal Finkelca542be2012-06-20 15:43:03 +00001271 AM = ISD::PRE_INC;
1272 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001273 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001274
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001275 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001276 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001277 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001278 return false;
1279 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001280 // LDU/STU need an address with at least 4-byte alignment.
1281 if (Alignment < 4)
1282 return false;
1283
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001284 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001285 return false;
1286 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001287
Chris Lattnerb314b152006-11-11 00:08:42 +00001288 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001289 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1290 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001291 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001292 LD->getExtensionType() == ISD::SEXTLOAD &&
1293 isa<ConstantSDNode>(Offset))
1294 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001295 }
1296
Chris Lattnerce645542006-11-10 02:08:47 +00001297 AM = ISD::PRE_INC;
1298 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001299}
1300
1301//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001302// LowerOperation implementation
1303//===----------------------------------------------------------------------===//
1304
Chris Lattneredb9d842010-11-15 02:46:57 +00001305/// GetLabelAccessInfo - Return true if we should reference labels using a
1306/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1307static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Chris Lattnerdd6df842010-11-15 03:13:19 +00001308 unsigned &LoOpFlags, const GlobalValue *GV = 0) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001309 HiOpFlags = PPCII::MO_HA;
1310 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001311
Chris Lattneredb9d842010-11-15 02:46:57 +00001312 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1313 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peck527da1b2010-11-23 03:31:01 +00001314 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattneredb9d842010-11-15 02:46:57 +00001315 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattnerdd6df842010-11-15 03:13:19 +00001316 if (isPIC) {
1317 HiOpFlags |= PPCII::MO_PIC_FLAG;
1318 LoOpFlags |= PPCII::MO_PIC_FLAG;
1319 }
1320
1321 // If this is a reference to a global value that requires a non-lazy-ptr, make
1322 // sure that instruction lowering adds it.
1323 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1324 HiOpFlags |= PPCII::MO_NLP_FLAG;
1325 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001326
Chris Lattnerdd6df842010-11-15 03:13:19 +00001327 if (GV->hasHiddenVisibility()) {
1328 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1329 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1330 }
1331 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001332
Chris Lattneredb9d842010-11-15 02:46:57 +00001333 return isPIC;
1334}
1335
1336static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1337 SelectionDAG &DAG) {
1338 EVT PtrVT = HiPart.getValueType();
1339 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001340 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001341
1342 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1343 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001344
Chris Lattneredb9d842010-11-15 02:46:57 +00001345 // With PIC, the first instruction is actually "GR+hi(&G)".
1346 if (isPIC)
1347 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1348 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001349
Chris Lattneredb9d842010-11-15 02:46:57 +00001350 // Generate non-pic code that has direct accesses to the constant pool.
1351 // The address of the global is just (hi(&g)+lo(&g)).
1352 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1353}
1354
Scott Michelcf0da6c2009-02-17 22:15:04 +00001355SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001356 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001357 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001358 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001359 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001360
Roman Divackyace47072012-08-24 16:26:02 +00001361 // 64-bit SVR4 ABI code is always position-independent.
1362 // The actual address of the GlobalValue is stored in the TOC.
1363 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1364 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001365 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001366 DAG.getRegister(PPC::X2, MVT::i64));
1367 }
1368
Chris Lattneredb9d842010-11-15 02:46:57 +00001369 unsigned MOHiFlag, MOLoFlag;
1370 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1371 SDValue CPIHi =
1372 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1373 SDValue CPILo =
1374 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1375 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001376}
1377
Dan Gohman21cea8a2010-04-17 15:26:15 +00001378SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001379 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001380 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001381
Roman Divackyace47072012-08-24 16:26:02 +00001382 // 64-bit SVR4 ABI code is always position-independent.
1383 // The actual address of the GlobalValue is stored in the TOC.
1384 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1385 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001386 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001387 DAG.getRegister(PPC::X2, MVT::i64));
1388 }
1389
Chris Lattneredb9d842010-11-15 02:46:57 +00001390 unsigned MOHiFlag, MOLoFlag;
1391 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1392 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1393 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1394 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001395}
1396
Dan Gohman21cea8a2010-04-17 15:26:15 +00001397SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1398 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001399 EVT PtrVT = Op.getValueType();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001400
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001401 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peck527da1b2010-11-23 03:31:01 +00001402
Chris Lattneredb9d842010-11-15 02:46:57 +00001403 unsigned MOHiFlag, MOLoFlag;
1404 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001405 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1406 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001407 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1408}
1409
Roman Divackye3f15c982012-06-04 17:36:38 +00001410SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1411 SelectionDAG &DAG) const {
1412
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001413 // FIXME: TLS addresses currently use medium model code sequences,
1414 // which is the most useful form. Eventually support for small and
1415 // large models could be added if users need it, at the cost of
1416 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001417 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001418 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001419 const GlobalValue *GV = GA->getGlobal();
1420 EVT PtrVT = getPointerTy();
1421 bool is64bit = PPCSubTarget.isPPC64();
1422
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001423 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001424
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001425 if (Model == TLSModel::LocalExec) {
1426 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001427 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001428 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001429 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001430 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1431 is64bit ? MVT::i64 : MVT::i32);
1432 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1433 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1434 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001435
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001436 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001437 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001438 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1439 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00001440 SDValue GOTPtr;
1441 if (is64bit) {
1442 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1443 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1444 PtrVT, GOTReg, TGA);
1445 } else
1446 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001447 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00001448 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001449 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001450 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001451
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001452 if (Model == TLSModel::GeneralDynamic) {
1453 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1454 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1455 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1456 GOTReg, TGA);
1457 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1458 GOTEntryHi, TGA);
1459
1460 // We need a chain node, and don't have one handy. The underlying
1461 // call has no side effects, so using the function entry node
1462 // suffices.
1463 SDValue Chain = DAG.getEntryNode();
1464 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1465 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1466 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1467 PtrVT, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001468 // The return value from GET_TLS_ADDR really is in X3 already, but
1469 // some hacks are needed here to tie everything together. The extra
1470 // copies dissolve during subsequent transforms.
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001471 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1472 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1473 }
1474
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001475 if (Model == TLSModel::LocalDynamic) {
1476 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1477 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1478 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1479 GOTReg, TGA);
1480 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1481 GOTEntryHi, TGA);
1482
1483 // We need a chain node, and don't have one handy. The underlying
1484 // call has no side effects, so using the function entry node
1485 // suffices.
1486 SDValue Chain = DAG.getEntryNode();
1487 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1488 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1489 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1490 PtrVT, ParmReg, TGA);
1491 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1492 // some hacks are needed here to tie everything together. The extra
1493 // copies dissolve during subsequent transforms.
1494 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1495 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt9ed4dbc2012-12-13 20:57:10 +00001496 Chain, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001497 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1498 }
1499
1500 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00001501}
1502
Chris Lattneredb9d842010-11-15 02:46:57 +00001503SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1504 SelectionDAG &DAG) const {
1505 EVT PtrVT = Op.getValueType();
1506 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001507 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00001508 const GlobalValue *GV = GSDN->getGlobal();
1509
Chris Lattneredb9d842010-11-15 02:46:57 +00001510 // 64-bit SVR4 ABI code is always position-independent.
1511 // The actual address of the GlobalValue is stored in the TOC.
1512 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1513 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1514 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1515 DAG.getRegister(PPC::X2, MVT::i64));
1516 }
1517
Chris Lattnerdd6df842010-11-15 03:13:19 +00001518 unsigned MOHiFlag, MOLoFlag;
1519 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00001520
Chris Lattnerdd6df842010-11-15 03:13:19 +00001521 SDValue GAHi =
1522 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1523 SDValue GALo =
1524 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00001525
Chris Lattnerdd6df842010-11-15 03:13:19 +00001526 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00001527
Chris Lattnerdd6df842010-11-15 03:13:19 +00001528 // If the global reference is actually to a non-lazy-pointer, we have to do an
1529 // extra load to get the address of the global.
1530 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1531 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001532 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00001533 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00001534}
1535
Dan Gohman21cea8a2010-04-17 15:26:15 +00001536SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00001537 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001538 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001539
Chris Lattner4211ca92006-04-14 06:01:58 +00001540 // If we're comparing for equality to zero, expose the fact that this is
1541 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1542 // fold the new nodes.
1543 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1544 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001545 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001546 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001547 if (VT.bitsLT(MVT::i32)) {
1548 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001549 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001550 }
Duncan Sands13237ac2008-06-06 12:08:01 +00001551 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001552 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1553 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00001554 DAG.getConstant(Log2b, MVT::i32));
1555 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00001556 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001557 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00001558 // optimized. FIXME: revisit this when we can custom lower all setcc
1559 // optimizations.
1560 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001561 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001562 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001563
Chris Lattner4211ca92006-04-14 06:01:58 +00001564 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00001565 // by xor'ing the rhs with the lhs, which is faster than setting a
1566 // condition register, reading it back out, and masking the correct bit. The
1567 // normal approach here uses sub to do this instead of xor. Using xor exposes
1568 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001569 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00001570 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001571 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001572 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00001573 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001574 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00001575 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001576 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001577}
1578
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001579SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001580 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00001581 SDNode *Node = Op.getNode();
1582 EVT VT = Node->getValueType(0);
1583 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1584 SDValue InChain = Node->getOperand(0);
1585 SDValue VAListPtr = Node->getOperand(1);
1586 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001587 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001588
Roman Divacky4394e682011-06-28 15:30:42 +00001589 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1590
1591 // gpr_index
1592 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1593 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1594 false, false, 0);
1595 InChain = GprIndex.getValue(1);
1596
1597 if (VT == MVT::i64) {
1598 // Check if GprIndex is even
1599 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1600 DAG.getConstant(1, MVT::i32));
1601 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1602 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1603 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1604 DAG.getConstant(1, MVT::i32));
1605 // Align GprIndex to be even if it isn't
1606 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1607 GprIndex);
1608 }
1609
1610 // fpr index is 1 byte after gpr
1611 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1612 DAG.getConstant(1, MVT::i32));
1613
1614 // fpr
1615 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1616 FprPtr, MachinePointerInfo(SV), MVT::i8,
1617 false, false, 0);
1618 InChain = FprIndex.getValue(1);
1619
1620 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1621 DAG.getConstant(8, MVT::i32));
1622
1623 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1624 DAG.getConstant(4, MVT::i32));
1625
1626 // areas
1627 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001628 MachinePointerInfo(), false, false,
1629 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001630 InChain = OverflowArea.getValue(1);
1631
1632 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001633 MachinePointerInfo(), false, false,
1634 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001635 InChain = RegSaveArea.getValue(1);
1636
1637 // select overflow_area if index > 8
1638 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1639 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1640
Roman Divacky4394e682011-06-28 15:30:42 +00001641 // adjustment constant gpr_index * 4/8
1642 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1643 VT.isInteger() ? GprIndex : FprIndex,
1644 DAG.getConstant(VT.isInteger() ? 4 : 8,
1645 MVT::i32));
1646
1647 // OurReg = RegSaveArea + RegConstant
1648 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1649 RegConstant);
1650
1651 // Floating types are 32 bytes into RegSaveArea
1652 if (VT.isFloatingPoint())
1653 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1654 DAG.getConstant(32, MVT::i32));
1655
1656 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1657 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1658 VT.isInteger() ? GprIndex : FprIndex,
1659 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1660 MVT::i32));
1661
1662 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1663 VT.isInteger() ? VAListPtr : FprPtr,
1664 MachinePointerInfo(SV),
1665 MVT::i8, false, false, 0);
1666
1667 // determine if we should load from reg_save_area or overflow_area
1668 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1669
1670 // increase overflow_area by 4/8 if gpr/fpr > 8
1671 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1672 DAG.getConstant(VT.isInteger() ? 4 : 8,
1673 MVT::i32));
1674
1675 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1676 OverflowAreaPlusN);
1677
1678 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1679 OverflowAreaPtr,
1680 MachinePointerInfo(),
1681 MVT::i32, false, false, 0);
1682
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00001683 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001684 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001685}
1686
Roman Divackyc3825df2013-07-25 21:36:47 +00001687SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
1688 const PPCSubtarget &Subtarget) const {
1689 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
1690
1691 // We have to copy the entire va_list struct:
1692 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
1693 return DAG.getMemcpy(Op.getOperand(0), Op,
1694 Op.getOperand(1), Op.getOperand(2),
1695 DAG.getConstant(12, MVT::i32), 8, false, true,
1696 MachinePointerInfo(), MachinePointerInfo());
1697}
1698
Duncan Sandsa0984362011-09-06 13:37:06 +00001699SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1700 SelectionDAG &DAG) const {
1701 return Op.getOperand(0);
1702}
1703
1704SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1705 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00001706 SDValue Chain = Op.getOperand(0);
1707 SDValue Trmp = Op.getOperand(1); // trampoline
1708 SDValue FPtr = Op.getOperand(2); // nested function
1709 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00001710 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00001711
Owen Anderson53aa7a92009-08-10 22:56:29 +00001712 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00001713 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00001714 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001715 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00001716 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00001717
Scott Michelcf0da6c2009-02-17 22:15:04 +00001718 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00001719 TargetLowering::ArgListEntry Entry;
1720
1721 Entry.Ty = IntPtrTy;
1722 Entry.Node = Trmp; Args.push_back(Entry);
1723
1724 // TrampSize == (isPPC64 ? 48 : 40);
1725 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00001726 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00001727 Args.push_back(Entry);
1728
1729 Entry.Node = FPtr; Args.push_back(Entry);
1730 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001731
Bill Wendling95e1af22008-09-17 00:30:57 +00001732 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Justin Holewinskiaa583972012-05-25 16:35:28 +00001733 TargetLowering::CallLoweringInfo CLI(Chain,
1734 Type::getVoidTy(*DAG.getContext()),
1735 false, false, false, false, 0,
1736 CallingConv::C,
Evan Cheng65f9d192012-02-28 18:51:51 +00001737 /*isTailCall=*/false,
Justin Holewinskiaa583972012-05-25 16:35:28 +00001738 /*doesNotRet=*/false,
1739 /*isReturnValueUsed=*/true,
Bill Wendling95e1af22008-09-17 00:30:57 +00001740 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00001741 Args, DAG, dl);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001742 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bill Wendling95e1af22008-09-17 00:30:57 +00001743
Duncan Sandsa0984362011-09-06 13:37:06 +00001744 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00001745}
1746
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001747SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001748 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001749 MachineFunction &MF = DAG.getMachineFunction();
1750 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1751
Andrew Trickef9de2a2013-05-25 02:42:55 +00001752 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001753
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001754 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001755 // vastart just stores the address of the VarArgsFrameIndex slot into the
1756 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001757 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00001758 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001759 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00001760 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1761 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00001762 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001763 }
1764
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001765 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001766 // We suppose the given va_list is already allocated.
1767 //
1768 // typedef struct {
1769 // char gpr; /* index into the array of 8 GPRs
1770 // * stored in the register save area
1771 // * gpr=0 corresponds to r3,
1772 // * gpr=1 to r4, etc.
1773 // */
1774 // char fpr; /* index into the array of 8 FPRs
1775 // * stored in the register save area
1776 // * fpr=0 corresponds to f1,
1777 // * fpr=1 to f2, etc.
1778 // */
1779 // char *overflow_arg_area;
1780 // /* location on stack that holds
1781 // * the next overflow argument
1782 // */
1783 // char *reg_save_area;
1784 // /* where r3:r10 and f1:f8 (if saved)
1785 // * are stored
1786 // */
1787 // } va_list[1];
1788
1789
Dan Gohman31ae5862010-04-17 14:41:14 +00001790 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1791 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001792
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001793
Owen Anderson53aa7a92009-08-10 22:56:29 +00001794 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001795
Dan Gohman31ae5862010-04-17 14:41:14 +00001796 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1797 PtrVT);
1798 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1799 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001800
Duncan Sands13237ac2008-06-06 12:08:01 +00001801 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001802 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001803
Duncan Sands13237ac2008-06-06 12:08:01 +00001804 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001805 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001806
1807 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001808 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001809
Dan Gohman2d489b52008-02-06 22:27:42 +00001810 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001811
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001812 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001813 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00001814 Op.getOperand(1),
1815 MachinePointerInfo(SV),
1816 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001817 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001818 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001819 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001820
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001821 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001822 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00001823 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1824 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00001825 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001826 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001827 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001828
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001829 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001830 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00001831 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1832 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001833 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001834 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001835 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001836
1837 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00001838 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1839 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001840 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001841
Chris Lattner4211ca92006-04-14 06:01:58 +00001842}
1843
Chris Lattner4f2e4e02007-03-06 00:59:59 +00001844#include "PPCGenCallingConv.inc"
1845
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001846// Function whose sole purpose is to kill compiler warnings
1847// stemming from unused functions included from PPCGenCallingConv.inc.
1848CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001849 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00001850}
1851
Bill Schmidt230b4512013-06-12 16:39:22 +00001852bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1853 CCValAssign::LocInfo &LocInfo,
1854 ISD::ArgFlagsTy &ArgFlags,
1855 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001856 return true;
1857}
1858
Bill Schmidt230b4512013-06-12 16:39:22 +00001859bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
1860 MVT &LocVT,
1861 CCValAssign::LocInfo &LocInfo,
1862 ISD::ArgFlagsTy &ArgFlags,
1863 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00001864 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001865 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1866 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1867 };
1868 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00001869
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001870 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1871
1872 // Skip one register if the first unallocated register has an even register
1873 // number and there are still argument registers available which have not been
1874 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1875 // need to skip a register if RegNum is odd.
1876 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1877 State.AllocateReg(ArgRegs[RegNum]);
1878 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001879
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001880 // Always return false here, as this function only makes sure that the first
1881 // unallocated register has an odd register number and does not actually
1882 // allocate a register for the current argument.
1883 return false;
1884}
1885
Bill Schmidt230b4512013-06-12 16:39:22 +00001886bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
1887 MVT &LocVT,
1888 CCValAssign::LocInfo &LocInfo,
1889 ISD::ArgFlagsTy &ArgFlags,
1890 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00001891 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001892 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1893 PPC::F8
1894 };
1895
1896 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00001897
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001898 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1899
1900 // If there is only one Floating-point register left we need to put both f64
1901 // values of a split ppc_fp128 value on the stack.
1902 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1903 State.AllocateReg(ArgRegs[RegNum]);
1904 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001905
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001906 // Always return false here, as this function only makes sure that the two f64
1907 // values a ppc_fp128 value is split into are both passed in registers or both
1908 // passed on the stack and does not actually allocate a register for the
1909 // current argument.
1910 return false;
1911}
1912
Chris Lattner43df5b32007-02-25 05:34:32 +00001913/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001914/// on Darwin.
Craig Topperca658c22012-03-11 07:16:55 +00001915static const uint16_t *GetFPR() {
1916 static const uint16_t FPR[] = {
Chris Lattner43df5b32007-02-25 05:34:32 +00001917 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001918 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner43df5b32007-02-25 05:34:32 +00001919 };
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001920
Chris Lattner43df5b32007-02-25 05:34:32 +00001921 return FPR;
1922}
1923
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00001924/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1925/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001926static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00001927 unsigned PtrByteSize) {
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00001928 unsigned ArgSize = ArgVT.getSizeInBits()/8;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00001929 if (Flags.isByVal())
1930 ArgSize = Flags.getByValSize();
1931 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1932
1933 return ArgSize;
1934}
1935
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001936SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001937PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001938 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001939 const SmallVectorImpl<ISD::InputArg>
1940 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001941 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001942 SmallVectorImpl<SDValue> &InVals)
1943 const {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00001944 if (PPCSubTarget.isSVR4ABI()) {
1945 if (PPCSubTarget.isPPC64())
1946 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
1947 dl, DAG, InVals);
1948 else
1949 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
1950 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00001951 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00001952 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
1953 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001954 }
1955}
1956
1957SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00001958PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001959 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001960 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001961 const SmallVectorImpl<ISD::InputArg>
1962 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001963 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001964 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001965
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001966 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001967 // +-----------------------------------+
1968 // +--> | Back chain |
1969 // | +-----------------------------------+
1970 // | | Floating-point register save area |
1971 // | +-----------------------------------+
1972 // | | General register save area |
1973 // | +-----------------------------------+
1974 // | | CR save word |
1975 // | +-----------------------------------+
1976 // | | VRSAVE save word |
1977 // | +-----------------------------------+
1978 // | | Alignment padding |
1979 // | +-----------------------------------+
1980 // | | Vector register save area |
1981 // | +-----------------------------------+
1982 // | | Local variable space |
1983 // | +-----------------------------------+
1984 // | | Parameter list area |
1985 // | +-----------------------------------+
1986 // | | LR save word |
1987 // | +-----------------------------------+
1988 // SP--> +--- | Back chain |
1989 // +-----------------------------------+
1990 //
1991 // Specifications:
1992 // System V Application Binary Interface PowerPC Processor Supplement
1993 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00001994
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001995 MachineFunction &MF = DAG.getMachineFunction();
1996 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00001997 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001998
Owen Anderson53aa7a92009-08-10 22:56:29 +00001999 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002000 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002001 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2002 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002003 unsigned PtrByteSize = 4;
2004
2005 // Assign locations to all of the incoming arguments.
2006 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002007 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002008 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002009
2010 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002011 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002012
Bill Schmidtef17c142013-02-06 17:33:58 +00002013 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002014
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002015 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2016 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002017
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002018 // Arguments stored in registers.
2019 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002020 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002021 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002022
Owen Anderson9f944592009-08-11 20:47:22 +00002023 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002024 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002025 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Owen Anderson9f944592009-08-11 20:47:22 +00002026 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002027 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002028 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002029 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00002030 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002031 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002032 case MVT::f64:
Craig Topperabadc662012-04-20 06:31:50 +00002033 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002034 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002035 case MVT::v16i8:
2036 case MVT::v8i16:
2037 case MVT::v4i32:
2038 case MVT::v4f32:
Craig Topperabadc662012-04-20 06:31:50 +00002039 RC = &PPC::VRRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002040 break;
2041 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002042
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002043 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002044 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002045 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, ValVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002046
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002047 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002048 } else {
2049 // Argument stored in memory.
2050 assert(VA.isMemLoc());
2051
2052 unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8;
2053 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002054 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002055
2056 // Create load nodes to retrieve arguments from the stack.
2057 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002058 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2059 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002060 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002061 }
2062 }
2063
2064 // Assign locations to all of the incoming aggregate by value arguments.
2065 // Aggregates passed by value are stored in the local variable space of the
2066 // caller's stack frame, right above the parameter list area.
2067 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002068 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002069 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002070
2071 // Reserve stack space for the allocations in CCInfo.
2072 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2073
Bill Schmidtef17c142013-02-06 17:33:58 +00002074 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002075
2076 // Area that is at least reserved in the caller of this function.
2077 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00002078
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002079 // Set the size that is at least reserved in caller of this function. Tail
2080 // call optimized function's reserved stack space needs to be aligned so that
2081 // taking the difference between two stack areas will result in an aligned
2082 // stack.
2083 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2084
2085 MinReservedArea =
2086 std::max(MinReservedArea,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002087 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peck527da1b2010-11-23 03:31:01 +00002088
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002089 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002090 getStackAlignment();
2091 unsigned AlignMask = TargetAlign-1;
2092 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peck527da1b2010-11-23 03:31:01 +00002093
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002094 FI->setMinReservedArea(MinReservedArea);
2095
2096 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002097
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002098 // If the function takes variable number of arguments, make a frame index for
2099 // the start of the first vararg value... for expansion of llvm.va_start.
2100 if (isVarArg) {
Craig Topperbef78fc2012-03-11 07:57:25 +00002101 static const uint16_t GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002102 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2103 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2104 };
2105 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2106
Craig Topperbef78fc2012-03-11 07:57:25 +00002107 static const uint16_t FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002108 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2109 PPC::F8
2110 };
2111 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2112
Dan Gohman31ae5862010-04-17 14:41:14 +00002113 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2114 NumGPArgRegs));
2115 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2116 NumFPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002117
2118 // Make room for NumGPArgRegs and NumFPArgRegs.
2119 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson9f944592009-08-11 20:47:22 +00002120 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002121
Dan Gohman31ae5862010-04-17 14:41:14 +00002122 FuncInfo->setVarArgsStackOffset(
2123 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002124 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002125
Dan Gohman31ae5862010-04-17 14:41:14 +00002126 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2127 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002128
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002129 // The fixed integer arguments of a variadic function are stored to the
2130 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2131 // the result of va_next.
2132 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2133 // Get an existing live-in vreg, or add a new one.
2134 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2135 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002136 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002137
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002138 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002139 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2140 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002141 MemOps.push_back(Store);
2142 // Increment the address by four for the next argument to store
2143 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2144 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2145 }
2146
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002147 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2148 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002149 // The double arguments are stored to the VarArgsFrameIndex
2150 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002151 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2152 // Get an existing live-in vreg, or add a new one.
2153 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2154 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002155 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002156
Owen Anderson9f944592009-08-11 20:47:22 +00002157 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002158 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2159 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002160 MemOps.push_back(Store);
2161 // Increment the address by eight for the next argument to store
Owen Anderson9f944592009-08-11 20:47:22 +00002162 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002163 PtrVT);
2164 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2165 }
2166 }
2167
2168 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002169 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002170 MVT::Other, &MemOps[0], MemOps.size());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002171
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002172 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002173}
2174
Bill Schmidt57d6de52012-10-23 15:51:16 +00002175// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2176// value to MVT::i64 and then truncate to the correct register size.
2177SDValue
2178PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2179 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002180 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002181 if (Flags.isSExt())
2182 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2183 DAG.getValueType(ObjectVT));
2184 else if (Flags.isZExt())
2185 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2186 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002187
Bill Schmidt57d6de52012-10-23 15:51:16 +00002188 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
2189}
2190
2191// Set the size that is at least reserved in caller of this function. Tail
2192// call optimized functions' reserved stack space needs to be aligned so that
2193// taking the difference between two stack areas will result in an aligned
2194// stack.
2195void
2196PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2197 unsigned nAltivecParamsAtEnd,
2198 unsigned MinReservedArea,
2199 bool isPPC64) const {
2200 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2201 // Add the Altivec parameters at the end, if needed.
2202 if (nAltivecParamsAtEnd) {
2203 MinReservedArea = ((MinReservedArea+15)/16)*16;
2204 MinReservedArea += 16*nAltivecParamsAtEnd;
2205 }
2206 MinReservedArea =
2207 std::max(MinReservedArea,
2208 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2209 unsigned TargetAlign
2210 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2211 getStackAlignment();
2212 unsigned AlignMask = TargetAlign-1;
2213 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2214 FI->setMinReservedArea(MinReservedArea);
2215}
2216
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002217SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002218PPCTargetLowering::LowerFormalArguments_64SVR4(
2219 SDValue Chain,
2220 CallingConv::ID CallConv, bool isVarArg,
2221 const SmallVectorImpl<ISD::InputArg>
2222 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002223 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002224 SmallVectorImpl<SDValue> &InVals) const {
2225 // TODO: add description of PPC stack frame format, or at least some docs.
2226 //
2227 MachineFunction &MF = DAG.getMachineFunction();
2228 MachineFrameInfo *MFI = MF.getFrameInfo();
2229 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2230
2231 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2232 // Potential tail calls could cause overwriting of argument stack slots.
2233 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2234 (CallConv == CallingConv::Fast));
2235 unsigned PtrByteSize = 8;
2236
2237 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2238 // Area that is at least reserved in caller of this function.
2239 unsigned MinReservedArea = ArgOffset;
2240
2241 static const uint16_t GPR[] = {
2242 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2243 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2244 };
2245
2246 static const uint16_t *FPR = GetFPR();
2247
2248 static const uint16_t VR[] = {
2249 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2250 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2251 };
2252
2253 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2254 const unsigned Num_FPR_Regs = 13;
2255 const unsigned Num_VR_Regs = array_lengthof(VR);
2256
2257 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2258
2259 // Add DAG nodes to load the arguments or copy them out of registers. On
2260 // entry to a function on PPC, the arguments start after the linkage area,
2261 // although the first ones are often in registers.
2262
2263 SmallVector<SDValue, 8> MemOps;
2264 unsigned nAltivecParamsAtEnd = 0;
2265 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002266 unsigned CurArgIdx = 0;
2267 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002268 SDValue ArgVal;
2269 bool needsLoad = false;
2270 EVT ObjectVT = Ins[ArgNo].VT;
2271 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
2272 unsigned ArgSize = ObjSize;
2273 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt6631e942013-02-20 17:31:41 +00002274 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2275 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002276
2277 unsigned CurArgOffset = ArgOffset;
2278
2279 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2280 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2281 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
2282 if (isVarArg) {
2283 MinReservedArea = ((MinReservedArea+15)/16)*16;
2284 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2285 Flags,
2286 PtrByteSize);
2287 } else
2288 nAltivecParamsAtEnd++;
2289 } else
2290 // Calculate min reserved area.
2291 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2292 Flags,
2293 PtrByteSize);
2294
2295 // FIXME the codegen can be much improved in some cases.
2296 // We do not have to keep everything in memory.
2297 if (Flags.isByVal()) {
2298 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2299 ObjSize = Flags.getByValSize();
2300 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002301 // Empty aggregate parameters do not take up registers. Examples:
2302 // struct { } a;
2303 // union { } b;
2304 // int c[0];
2305 // etc. However, we have to provide a place-holder in InVals, so
2306 // pretend we have an 8-byte item at the current address for that
2307 // purpose.
2308 if (!ObjSize) {
2309 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2310 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2311 InVals.push_back(FIN);
2312 continue;
2313 }
Hal Finkel262a2242013-09-12 23:20:06 +00002314
2315 unsigned BVAlign = Flags.getByValAlign();
2316 if (BVAlign > 8) {
2317 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
2318 CurArgOffset = ArgOffset;
2319 }
2320
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002321 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt48081ca2012-10-16 13:30:53 +00002322 if (ObjSize < PtrByteSize)
2323 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002324 // The value of the object is its address.
2325 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2326 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2327 InVals.push_back(FIN);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002328
2329 if (ObjSize < 8) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002330 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002331 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002332 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002333 SDValue Store;
2334
2335 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2336 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2337 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2338 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
2339 MachinePointerInfo(FuncArg, CurArgOffset),
2340 ObjType, false, false, 0);
2341 } else {
2342 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2343 // store the whole register as-is to the parameter save area
2344 // slot. The address of the parameter was already calculated
2345 // above (InVals.push_back(FIN)) to be the right-justified
2346 // offset within the slot. For this store, we need a new
2347 // frame index that points at the beginning of the slot.
2348 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2349 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2350 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2351 MachinePointerInfo(FuncArg, ArgOffset),
2352 false, false, 0);
2353 }
2354
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002355 MemOps.push_back(Store);
2356 ++GPR_idx;
2357 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002358 // Whether we copied from a register or not, advance the offset
2359 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002360 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002361 continue;
2362 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002363
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002364 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2365 // Store whatever pieces of the object are in registers
2366 // to memory. ArgOffset will be the address of the beginning
2367 // of the object.
2368 if (GPR_idx != Num_GPR_Regs) {
2369 unsigned VReg;
2370 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2371 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2372 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2373 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002374 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002375 MachinePointerInfo(FuncArg, ArgOffset),
2376 false, false, 0);
2377 MemOps.push_back(Store);
2378 ++GPR_idx;
2379 ArgOffset += PtrByteSize;
2380 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00002381 ArgOffset += ArgSize - j;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002382 break;
2383 }
2384 }
2385 continue;
2386 }
2387
2388 switch (ObjectVT.getSimpleVT().SimpleTy) {
2389 default: llvm_unreachable("Unhandled argument type!");
2390 case MVT::i32:
2391 case MVT::i64:
2392 if (GPR_idx != Num_GPR_Regs) {
2393 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2394 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2395
Bill Schmidt57d6de52012-10-23 15:51:16 +00002396 if (ObjectVT == MVT::i32)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002397 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2398 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002399 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002400
2401 ++GPR_idx;
2402 } else {
2403 needsLoad = true;
2404 ArgSize = PtrByteSize;
2405 }
2406 ArgOffset += 8;
2407 break;
2408
2409 case MVT::f32:
2410 case MVT::f64:
2411 // Every 8 bytes of argument space consumes one of the GPRs available for
2412 // argument passing.
2413 if (GPR_idx != Num_GPR_Regs) {
2414 ++GPR_idx;
2415 }
2416 if (FPR_idx != Num_FPR_Regs) {
2417 unsigned VReg;
2418
2419 if (ObjectVT == MVT::f32)
2420 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2421 else
2422 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
2423
2424 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2425 ++FPR_idx;
2426 } else {
2427 needsLoad = true;
Bill Schmidt22162472012-10-11 15:38:20 +00002428 ArgSize = PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002429 }
2430
2431 ArgOffset += 8;
2432 break;
2433 case MVT::v4f32:
2434 case MVT::v4i32:
2435 case MVT::v8i16:
2436 case MVT::v16i8:
2437 // Note that vector arguments in registers don't reserve stack space,
2438 // except in varargs functions.
2439 if (VR_idx != Num_VR_Regs) {
2440 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
2441 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2442 if (isVarArg) {
2443 while ((ArgOffset % 16) != 0) {
2444 ArgOffset += PtrByteSize;
2445 if (GPR_idx != Num_GPR_Regs)
2446 GPR_idx++;
2447 }
2448 ArgOffset += 16;
2449 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2450 }
2451 ++VR_idx;
2452 } else {
2453 // Vectors are aligned.
2454 ArgOffset = ((ArgOffset+15)/16)*16;
2455 CurArgOffset = ArgOffset;
2456 ArgOffset += 16;
2457 needsLoad = true;
2458 }
2459 break;
2460 }
2461
2462 // We need to load the argument to a virtual register if we determined
2463 // above that we ran out of physical registers of the appropriate type.
2464 if (needsLoad) {
2465 int FI = MFI->CreateFixedObject(ObjSize,
2466 CurArgOffset + (ArgSize - ObjSize),
2467 isImmutable);
2468 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2469 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2470 false, false, false, 0);
2471 }
2472
2473 InVals.push_back(ArgVal);
2474 }
2475
2476 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002477 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002478 // taking the difference between two stack areas will result in an aligned
2479 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002480 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002481
2482 // If the function takes variable number of arguments, make a frame index for
2483 // the start of the first vararg value... for expansion of llvm.va_start.
2484 if (isVarArg) {
2485 int Depth = ArgOffset;
2486
2487 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00002488 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002489 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2490
2491 // If this function is vararg, store any remaining integer argument regs
2492 // to their spots on the stack so that they may be loaded by deferencing the
2493 // result of va_next.
2494 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2495 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2496 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2497 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2498 MachinePointerInfo(), false, false, 0);
2499 MemOps.push_back(Store);
2500 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00002501 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002502 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2503 }
2504 }
2505
2506 if (!MemOps.empty())
2507 Chain = DAG.getNode(ISD::TokenFactor, dl,
2508 MVT::Other, &MemOps[0], MemOps.size());
2509
2510 return Chain;
2511}
2512
2513SDValue
2514PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002515 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002516 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002517 const SmallVectorImpl<ISD::InputArg>
2518 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002519 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002520 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002521 // TODO: add description of PPC stack frame format, or at least some docs.
2522 //
2523 MachineFunction &MF = DAG.getMachineFunction();
2524 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002525 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002526
Owen Anderson53aa7a92009-08-10 22:56:29 +00002527 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002528 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002529 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002530 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2531 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00002532 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey48850c12006-11-16 22:43:37 +00002533
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002534 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002535 // Area that is at least reserved in caller of this function.
2536 unsigned MinReservedArea = ArgOffset;
2537
Craig Topperca658c22012-03-11 07:16:55 +00002538 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002539 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2540 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2541 };
Craig Topperca658c22012-03-11 07:16:55 +00002542 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00002543 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2544 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2545 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00002546
Craig Topperca658c22012-03-11 07:16:55 +00002547 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002548
Craig Topperca658c22012-03-11 07:16:55 +00002549 static const uint16_t VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002550 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2551 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2552 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00002553
Owen Andersone2f23a32007-09-07 04:06:50 +00002554 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002555 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00002556 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00002557
2558 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002559
Craig Topperca658c22012-03-11 07:16:55 +00002560 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002561
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002562 // In 32-bit non-varargs functions, the stack space for vectors is after the
2563 // stack space for non-vectors. We do not use this space unless we have
2564 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00002565 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002566 // that out...for the pathological case, compute VecArgOffset as the
2567 // start of the vector parameter area. Computing VecArgOffset is the
2568 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002569 unsigned VecArgOffset = ArgOffset;
2570 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002571 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002572 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002573 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002574 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002575
Duncan Sandsd97eea32008-03-21 09:14:45 +00002576 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002577 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00002578 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002579 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002580 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2581 VecArgOffset += ArgSize;
2582 continue;
2583 }
2584
Owen Anderson9f944592009-08-11 20:47:22 +00002585 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002586 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002587 case MVT::i32:
2588 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002589 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002590 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002591 case MVT::i64: // PPC64
2592 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002593 // FIXME: We are guaranteed to be !isPPC64 at this point.
2594 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002595 VecArgOffset += 8;
2596 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002597 case MVT::v4f32:
2598 case MVT::v4i32:
2599 case MVT::v8i16:
2600 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002601 // Nothing to do, we're only looking at Nonvector args here.
2602 break;
2603 }
2604 }
2605 }
2606 // We've found where the vector parameter area in memory is. Skip the
2607 // first 12 parameters; these don't use that memory.
2608 VecArgOffset = ((VecArgOffset+15)/16)*16;
2609 VecArgOffset += 12*16;
2610
Chris Lattner4302e8f2006-05-16 18:18:50 +00002611 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00002612 // entry to a function on PPC, the arguments start after the linkage area,
2613 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00002614
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002615 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002616 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00002617 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002618 unsigned CurArgIdx = 0;
2619 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002620 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002621 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002622 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00002623 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00002624 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002625 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002626 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2627 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002628
Chris Lattner318f0d22006-05-16 18:51:52 +00002629 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002630
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002631 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002632 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2633 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002634 if (isVarArg || isPPC64) {
2635 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002636 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002637 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002638 PtrByteSize);
2639 } else nAltivecParamsAtEnd++;
2640 } else
2641 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002642 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002643 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002644 PtrByteSize);
2645
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002646 // FIXME the codegen can be much improved in some cases.
2647 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002648 if (Flags.isByVal()) {
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002649 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002650 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002651 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002652 // Objects of size 1 and 2 are right justified, everything else is
2653 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00002654 if (ObjSize==1 || ObjSize==2) {
2655 CurArgOffset = CurArgOffset + (4 - ObjSize);
2656 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002657 // The value of the object is its address.
Evan Cheng0664a672010-07-03 00:40:23 +00002658 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002659 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002660 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002661 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00002662 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002663 unsigned VReg;
2664 if (isPPC64)
2665 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2666 else
2667 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002668 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002669 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002670 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Roman Divackyca103892012-09-24 20:47:19 +00002671 MachinePointerInfo(FuncArg,
2672 CurArgOffset),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002673 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00002674 MemOps.push_back(Store);
2675 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00002676 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002677
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002678 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00002679
Dale Johannesen21a8f142008-03-08 01:41:42 +00002680 continue;
2681 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002682 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2683 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002684 // to memory. ArgOffset will be the address of the beginning
2685 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002686 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002687 unsigned VReg;
2688 if (isPPC64)
2689 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2690 else
2691 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00002692 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002693 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002694 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002695 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Roman Divackyca103892012-09-24 20:47:19 +00002696 MachinePointerInfo(FuncArg, ArgOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002697 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002698 MemOps.push_back(Store);
2699 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002700 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002701 } else {
2702 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2703 break;
2704 }
2705 }
2706 continue;
2707 }
2708
Owen Anderson9f944592009-08-11 20:47:22 +00002709 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002710 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002711 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00002712 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00002713 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002714 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002715 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Bill Wendling968f32c2008-03-07 20:49:02 +00002716 ++GPR_idx;
2717 } else {
2718 needsLoad = true;
2719 ArgSize = PtrByteSize;
2720 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002721 // All int arguments reserve stack space in the Darwin ABI.
2722 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00002723 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002724 }
Bill Wendling968f32c2008-03-07 20:49:02 +00002725 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00002726 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00002727 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002728 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002729 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00002730
Bill Schmidt57d6de52012-10-23 15:51:16 +00002731 if (ObjectVT == MVT::i32)
Bill Wendling968f32c2008-03-07 20:49:02 +00002732 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00002733 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002734 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00002735
Chris Lattnerec78cad2006-06-26 22:48:35 +00002736 ++GPR_idx;
2737 } else {
2738 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00002739 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002740 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002741 // All int arguments reserve stack space in the Darwin ABI.
2742 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002743 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002744
Owen Anderson9f944592009-08-11 20:47:22 +00002745 case MVT::f32:
2746 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00002747 // Every 4 bytes of argument space consumes one of the GPRs available for
2748 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002749 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002750 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00002751 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002752 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00002753 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002754 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002755 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002756
Owen Anderson9f944592009-08-11 20:47:22 +00002757 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00002758 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002759 else
Devang Patelf3292b22011-02-21 23:21:26 +00002760 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002761
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002762 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002763 ++FPR_idx;
2764 } else {
2765 needsLoad = true;
2766 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002767
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002768 // All FP arguments reserve stack space in the Darwin ABI.
2769 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002770 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002771 case MVT::v4f32:
2772 case MVT::v4i32:
2773 case MVT::v8i16:
2774 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00002775 // Note that vector arguments in registers don't reserve stack space,
2776 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002777 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002778 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002779 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00002780 if (isVarArg) {
2781 while ((ArgOffset % 16) != 0) {
2782 ArgOffset += PtrByteSize;
2783 if (GPR_idx != Num_GPR_Regs)
2784 GPR_idx++;
2785 }
2786 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002787 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00002788 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002789 ++VR_idx;
2790 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002791 if (!isVarArg && !isPPC64) {
2792 // Vectors go after all the nonvectors.
2793 CurArgOffset = VecArgOffset;
2794 VecArgOffset += 16;
2795 } else {
2796 // Vectors are aligned.
2797 ArgOffset = ((ArgOffset+15)/16)*16;
2798 CurArgOffset = ArgOffset;
2799 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00002800 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002801 needsLoad = true;
2802 }
2803 break;
2804 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002805
Chris Lattner4302e8f2006-05-16 18:18:50 +00002806 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002807 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002808 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002809 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002810 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00002811 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002812 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002813 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002814 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002815 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002816
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002817 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002818 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002819
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002820 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002821 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002822 // taking the difference between two stack areas will result in an aligned
2823 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002824 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002825
Chris Lattner4302e8f2006-05-16 18:18:50 +00002826 // If the function takes variable number of arguments, make a frame index for
2827 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002828 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002829 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002830
Dan Gohman31ae5862010-04-17 14:41:14 +00002831 FuncInfo->setVarArgsFrameIndex(
2832 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002833 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00002834 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002835
Chris Lattner4302e8f2006-05-16 18:18:50 +00002836 // If this function is vararg, store any remaining integer argument regs
2837 // to their spots on the stack so that they may be loaded by deferencing the
2838 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002839 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00002840 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00002841
Chris Lattner2cca3852006-11-18 01:57:19 +00002842 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00002843 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00002844 else
Devang Patelf3292b22011-02-21 23:21:26 +00002845 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00002846
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002847 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002848 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2849 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002850 MemOps.push_back(Store);
2851 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002852 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00002853 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002854 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002855 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002856
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002857 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002858 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002859 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002860
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002861 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002862}
2863
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002864/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
2865/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002866static unsigned
2867CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2868 bool isPPC64,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002869 bool isVarArg,
2870 unsigned CC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002871 const SmallVectorImpl<ISD::OutputArg>
2872 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002873 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002874 unsigned &nAltivecParamsAtEnd) {
2875 // Count how many bytes are to be pushed on the stack, including the linkage
2876 // area, and parameter passing area. We start with 24/48 bytes, which is
2877 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002878 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002879 unsigned NumOps = Outs.size();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002880 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2881
2882 // Add up all the space actually used.
2883 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2884 // they all go in registers, but we must reserve stack space for them for
2885 // possible use by the caller. In varargs or 64-bit calls, parameters are
2886 // assigned stack space in order, with padding so Altivec parameters are
2887 // 16-byte aligned.
2888 nAltivecParamsAtEnd = 0;
2889 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002890 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002891 EVT ArgVT = Outs[i].VT;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002892 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002893 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2894 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002895 if (!isVarArg && !isPPC64) {
2896 // Non-varargs Altivec parameters go after all the non-Altivec
2897 // parameters; handle those later so we know how much padding we need.
2898 nAltivecParamsAtEnd++;
2899 continue;
2900 }
2901 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2902 NumBytes = ((NumBytes+15)/16)*16;
2903 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002904 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002905 }
2906
2907 // Allow for Altivec parameters at the end, if needed.
2908 if (nAltivecParamsAtEnd) {
2909 NumBytes = ((NumBytes+15)/16)*16;
2910 NumBytes += 16*nAltivecParamsAtEnd;
2911 }
2912
2913 // The prolog code of the callee may store up to 8 GPR argument registers to
2914 // the stack, allowing va_start to index over them in memory if its varargs.
2915 // Because we cannot tell if this is needed on the caller side, we have to
2916 // conservatively assume that it is needed. As such, make sure we have at
2917 // least enough stack space for the caller to store the 8 GPRs.
2918 NumBytes = std::max(NumBytes,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002919 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002920
2921 // Tail call needs the stack to be aligned.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002922 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
2923 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
2924 getFrameLowering()->getStackAlignment();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002925 unsigned AlignMask = TargetAlign-1;
2926 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2927 }
2928
2929 return NumBytes;
2930}
2931
2932/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00002933/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00002934static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002935 unsigned ParamSize) {
2936
Dale Johannesen86dcae12009-11-24 01:09:07 +00002937 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002938
2939 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
2940 unsigned CallerMinReservedArea = FI->getMinReservedArea();
2941 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
2942 // Remember only if the new adjustement is bigger.
2943 if (SPDiff < FI->getTailCallSPDelta())
2944 FI->setTailCallSPDelta(SPDiff);
2945
2946 return SPDiff;
2947}
2948
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002949/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2950/// for tail call optimization. Targets which want to do tail call
2951/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002952bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002953PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002954 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002955 bool isVarArg,
2956 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002957 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002958 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00002959 return false;
2960
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002961 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002962 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00002963 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002964
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002965 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00002966 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002967 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
2968 // Functions containing by val parameters are not supported.
2969 for (unsigned i = 0; i != Ins.size(); i++) {
2970 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2971 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002972 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002973
Alp Tokerf907b892013-12-05 05:44:44 +00002974 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002975 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
2976 return true;
2977
2978 // At the moment we can only do local tail calls (in same module, hidden
2979 // or protected) if we are generating PIC.
2980 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2981 return G->getGlobal()->hasHiddenVisibility()
2982 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002983 }
2984
2985 return false;
2986}
2987
Chris Lattnereb755fc2006-05-17 19:00:46 +00002988/// isCallCompatibleAddress - Return the immediate to use if the specified
2989/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002990static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00002991 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2992 if (!C) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002993
Dan Gohmaneffb8942008-09-12 16:56:44 +00002994 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00002995 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00002996 SignExtend32<26>(Addr) != Addr)
Chris Lattnereb755fc2006-05-17 19:00:46 +00002997 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002998
Dan Gohmaneffb8942008-09-12 16:56:44 +00002999 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00003000 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003001}
3002
Dan Gohmand78c4002008-05-13 00:00:25 +00003003namespace {
3004
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003005struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003006 SDValue Arg;
3007 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003008 int FrameIdx;
3009
3010 TailCallArgumentInfo() : FrameIdx(0) {}
3011};
3012
Dan Gohmand78c4002008-05-13 00:00:25 +00003013}
3014
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003015/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3016static void
3017StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003018 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003019 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3020 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003021 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003022 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003023 SDValue Arg = TailCallArgs[i].Arg;
3024 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003025 int FI = TailCallArgs[i].FrameIdx;
3026 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003027 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003028 MachinePointerInfo::getFixedStack(FI),
3029 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003030 }
3031}
3032
3033/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3034/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003035static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003036 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003037 SDValue Chain,
3038 SDValue OldRetAddr,
3039 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003040 int SPDiff,
3041 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003042 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003043 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003044 if (SPDiff) {
3045 // Calculate the new stack slot for the return address.
3046 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003047 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003048 isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003049 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003050 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003051 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003052 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003053 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003054 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003055 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003056
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003057 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3058 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003059 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003060 int NewFPLoc =
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003061 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene1fbe0542009-11-12 20:49:22 +00003062 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003063 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003064 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3065 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003066 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003067 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003068 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003069 }
3070 return Chain;
3071}
3072
3073/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3074/// the position of the argument.
3075static void
3076CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003077 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003078 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003079 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003080 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003081 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003082 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003083 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003084 TailCallArgumentInfo Info;
3085 Info.Arg = Arg;
3086 Info.FrameIdxOp = FIN;
3087 Info.FrameIdx = FI;
3088 TailCallArguments.push_back(Info);
3089}
3090
3091/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3092/// stack slot. Returns the chain as result and the loaded frame pointers in
3093/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003094SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003095 int SPDiff,
3096 SDValue Chain,
3097 SDValue &LROpOut,
3098 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003099 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003100 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003101 if (SPDiff) {
3102 // Load the LR and FP stack slot for later adjusting.
Owen Anderson9f944592009-08-11 20:47:22 +00003103 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003104 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003105 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003106 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003107 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003108
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003109 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3110 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003111 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003112 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003113 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003114 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003115 Chain = SDValue(FPOpOut.getNode(), 1);
3116 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003117 }
3118 return Chain;
3119}
3120
Dale Johannesen85d41a12008-03-04 23:17:14 +00003121/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003122/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003123/// specified by the specific parameter attribute. The copy will be passed as
3124/// a byval function parameter.
3125/// Sometimes what we are copying is the end of a larger object, the part that
3126/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003127static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003128CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003129 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003130 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003131 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003132 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Chris Lattner2510de22010-09-21 05:40:29 +00003133 false, false, MachinePointerInfo(0),
3134 MachinePointerInfo(0));
Dale Johannesen85d41a12008-03-04 23:17:14 +00003135}
Chris Lattner43df5b32007-02-25 05:34:32 +00003136
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003137/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3138/// tail calls.
3139static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003140LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3141 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003142 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003143 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3144 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003145 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003146 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003147 if (!isTailCall) {
3148 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003149 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003150 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003151 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003152 else
Owen Anderson9f944592009-08-11 20:47:22 +00003153 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003154 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003155 DAG.getConstant(ArgOffset, PtrVT));
3156 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003157 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3158 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003159 // Calculate and remember argument location.
3160 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3161 TailCallArguments);
3162}
3163
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003164static
3165void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003166 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003167 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003168 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003169 MachineFunction &MF = DAG.getMachineFunction();
3170
3171 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3172 // might overwrite each other in case of tail call optimization.
3173 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003174 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003175 InFlag = SDValue();
3176 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3177 MemOpChains2, dl);
3178 if (!MemOpChains2.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003179 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003180 &MemOpChains2[0], MemOpChains2.size());
3181
3182 // Store the return address to the appropriate stack slot.
3183 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3184 isPPC64, isDarwinABI, dl);
3185
3186 // Emit callseq_end just before tailcall node.
3187 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003188 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003189 InFlag = Chain.getValue(1);
3190}
3191
3192static
3193unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003194 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003195 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3196 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003197 const PPCSubtarget &PPCSubTarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003198
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003199 bool isPPC64 = PPCSubTarget.isPPC64();
3200 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3201
Owen Anderson53aa7a92009-08-10 22:56:29 +00003202 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003203 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003204 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003205
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003206 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003207
Torok Edwin31e90d22010-08-04 20:47:44 +00003208 bool needIndirectCall = true;
3209 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003210 // If this is an absolute destination address, use the munged value.
3211 Callee = SDValue(Dest, 0);
Torok Edwin31e90d22010-08-04 20:47:44 +00003212 needIndirectCall = false;
3213 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003214
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003215 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3216 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3217 // Use indirect calls for ALL functions calls in JIT mode, since the
3218 // far-call stubs may be outside relocation limits for a BL instruction.
3219 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3220 unsigned OpFlags = 0;
3221 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003222 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003223 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003224 (G->getGlobal()->isDeclaration() ||
3225 G->getGlobal()->isWeakForLinker())) {
3226 // PC-relative references to external symbols should go through $stub,
3227 // unless we're building with the leopard linker or later, which
3228 // automatically synthesizes these stubs.
3229 OpFlags = PPCII::MO_DARWIN_STUB;
3230 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003231
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003232 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3233 // every direct call is) turn it into a TargetGlobalAddress /
3234 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin31e90d22010-08-04 20:47:44 +00003235 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003236 Callee.getValueType(),
3237 0, OpFlags);
Torok Edwin31e90d22010-08-04 20:47:44 +00003238 needIndirectCall = false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003239 }
Torok Edwin31e90d22010-08-04 20:47:44 +00003240 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003241
Torok Edwin31e90d22010-08-04 20:47:44 +00003242 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003243 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003244
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003245 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003246 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003247 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003248 // PC-relative references to external symbols should go through $stub,
3249 // unless we're building with the leopard linker or later, which
3250 // automatically synthesizes these stubs.
3251 OpFlags = PPCII::MO_DARWIN_STUB;
3252 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003253
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003254 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3255 OpFlags);
3256 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003257 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003258
Torok Edwin31e90d22010-08-04 20:47:44 +00003259 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003260 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3261 // to do the call, we can't use PPCISD::CALL.
3262 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003263
3264 if (isSVR4ABI && isPPC64) {
3265 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3266 // entry point, but to the function descriptor (the function entry point
3267 // address is part of the function descriptor though).
3268 // The function descriptor is a three doubleword structure with the
3269 // following fields: function entry point, TOC base address and
3270 // environment pointer.
3271 // Thus for a call through a function pointer, the following actions need
3272 // to be performed:
3273 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003274 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003275 // 2. Load the address of the function entry point from the function
3276 // descriptor.
3277 // 3. Load the TOC of the callee from the function descriptor into r2.
3278 // 4. Load the environment pointer from the function descriptor into
3279 // r11.
3280 // 5. Branch to the function entry point address.
3281 // 6. On return of the callee, the TOC of the caller needs to be
3282 // restored (this is done in FinishCall()).
3283 //
3284 // All those operations are flagged together to ensure that no other
3285 // operations can be scheduled in between. E.g. without flagging the
3286 // operations together, a TOC access in the caller could be scheduled
3287 // between the load of the callee TOC and the branch to the callee, which
3288 // results in the TOC access going through the TOC of the callee instead
3289 // of going through the TOC of the caller, which leads to incorrect code.
3290
3291 // Load the address of the function entry point from the function
3292 // descriptor.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003293 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003294 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, MTCTROps,
3295 InFlag.getNode() ? 3 : 2);
3296 Chain = LoadFuncPtr.getValue(1);
3297 InFlag = LoadFuncPtr.getValue(2);
3298
3299 // Load environment pointer into r11.
3300 // Offset of the environment pointer within the function descriptor.
3301 SDValue PtrOff = DAG.getIntPtrConstant(16);
3302
3303 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3304 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3305 InFlag);
3306 Chain = LoadEnvPtr.getValue(1);
3307 InFlag = LoadEnvPtr.getValue(2);
3308
3309 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3310 InFlag);
3311 Chain = EnvVal.getValue(0);
3312 InFlag = EnvVal.getValue(1);
3313
3314 // Load TOC of the callee into r2. We are using a target-specific load
3315 // with r2 hard coded, because the result of a target-independent load
3316 // would never go directly into r2, since r2 is a reserved register (which
3317 // prevents the register allocator from allocating it), resulting in an
3318 // additional register being allocated and an unnecessary move instruction
3319 // being generated.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003320 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003321 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3322 Callee, InFlag);
3323 Chain = LoadTOCPtr.getValue(0);
3324 InFlag = LoadTOCPtr.getValue(1);
3325
3326 MTCTROps[0] = Chain;
3327 MTCTROps[1] = LoadFuncPtr;
3328 MTCTROps[2] = InFlag;
3329 }
3330
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003331 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
3332 2 + (InFlag.getNode() != 0));
3333 InFlag = Chain.getValue(1);
3334
3335 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00003336 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003337 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003338 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003339 CallOpc = PPCISD::BCTRL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003340 Callee.setNode(0);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003341 // Add use of X11 (holding environment pointer)
3342 if (isSVR4ABI && isPPC64)
3343 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003344 // Add CTR register as callee so a bctr can be emitted later.
3345 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00003346 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003347 }
3348
3349 // If this is a direct call, pass the chain and the callee.
3350 if (Callee.getNode()) {
3351 Ops.push_back(Chain);
3352 Ops.push_back(Callee);
3353 }
3354 // If this is a tail call add stack pointer delta.
3355 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00003356 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003357
3358 // Add argument registers to the end of the list so that they are known live
3359 // into the call.
3360 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3361 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3362 RegsToPass[i].second.getValueType()));
3363
3364 return CallOpc;
3365}
3366
Roman Divacky76293062012-09-18 16:47:58 +00003367static
3368bool isLocalCall(const SDValue &Callee)
3369{
3370 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00003371 return !G->getGlobal()->isDeclaration() &&
3372 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00003373 return false;
3374}
3375
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003376SDValue
3377PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003378 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003379 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003380 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003381 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003382
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003383 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003384 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003385 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003386 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003387
3388 // Copy all of the result registers out of their specified physreg.
3389 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3390 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003391 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00003392
3393 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3394 VA.getLocReg(), VA.getLocVT(), InFlag);
3395 Chain = Val.getValue(1);
3396 InFlag = Val.getValue(2);
3397
3398 switch (VA.getLocInfo()) {
3399 default: llvm_unreachable("Unknown loc info!");
3400 case CCValAssign::Full: break;
3401 case CCValAssign::AExt:
3402 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3403 break;
3404 case CCValAssign::ZExt:
3405 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3406 DAG.getValueType(VA.getValVT()));
3407 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3408 break;
3409 case CCValAssign::SExt:
3410 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3411 DAG.getValueType(VA.getValVT()));
3412 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3413 break;
3414 }
3415
3416 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003417 }
3418
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003419 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003420}
3421
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003422SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00003423PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003424 bool isTailCall, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003425 SelectionDAG &DAG,
3426 SmallVector<std::pair<unsigned, SDValue>, 8>
3427 &RegsToPass,
3428 SDValue InFlag, SDValue Chain,
3429 SDValue &Callee,
3430 int SPDiff, unsigned NumBytes,
3431 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003432 SmallVectorImpl<SDValue> &InVals) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003433 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003434 SmallVector<SDValue, 8> Ops;
3435 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3436 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003437 PPCSubTarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003438
Hal Finkel5ab37802012-08-28 02:10:27 +00003439 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3440 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3441 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3442
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003443 // When performing tail call optimization the callee pops its arguments off
3444 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00003445 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003446 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003447 (CallConv == CallingConv::Fast &&
3448 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003449
Roman Divackyef21be22012-03-06 16:41:49 +00003450 // Add a register mask operand representing the call-preserved registers.
3451 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3452 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3453 assert(Mask && "Missing call preserved mask for calling convention");
3454 Ops.push_back(DAG.getRegisterMask(Mask));
3455
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003456 if (InFlag.getNode())
3457 Ops.push_back(InFlag);
3458
3459 // Emit tail call.
3460 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003461 assert(((Callee.getOpcode() == ISD::Register &&
3462 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3463 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3464 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3465 isa<ConstantSDNode>(Callee)) &&
3466 "Expecting an global address, external symbol, absolute value or register");
3467
Owen Anderson9f944592009-08-11 20:47:22 +00003468 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003469 }
3470
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003471 // Add a NOP immediately after the branch instruction when using the 64-bit
3472 // SVR4 ABI. At link time, if caller and callee are in a different module and
3473 // thus have a different TOC, the call will be replaced with a call to a stub
3474 // function which saves the current TOC, loads the TOC of the callee and
3475 // branches to the callee. The NOP will be replaced with a load instruction
3476 // which restores the TOC of the caller from the TOC save slot of the current
3477 // stack frame. If caller and callee belong to the same module (and have the
3478 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00003479
3480 bool needsTOCRestore = false;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003481 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003482 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003483 // This is a call through a function pointer.
3484 // Restore the caller TOC from the save area into R2.
3485 // See PrepareCall() for more information about calls through function
3486 // pointers in the 64-bit SVR4 ABI.
3487 // We are using a target-specific load with r2 hard coded, because the
3488 // result of a target-independent load would never go directly into r2,
3489 // since r2 is a reserved register (which prevents the register allocator
3490 // from allocating it), resulting in an additional register being
3491 // allocated and an unnecessary move instruction being generated.
Hal Finkel51861b42012-03-31 14:45:15 +00003492 needsTOCRestore = true;
Bill Schmidtcea15962013-09-26 17:09:28 +00003493 } else if ((CallOpc == PPCISD::CALL) &&
3494 (!isLocalCall(Callee) ||
3495 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Roman Divacky76293062012-09-18 16:47:58 +00003496 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003497 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller79fef932009-12-18 13:00:15 +00003498 }
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003499 }
3500
Hal Finkel51861b42012-03-31 14:45:15 +00003501 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
3502 InFlag = Chain.getValue(1);
3503
3504 if (needsTOCRestore) {
3505 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3506 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3507 InFlag = Chain.getValue(1);
3508 }
3509
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003510 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3511 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003512 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003513 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003514 InFlag = Chain.getValue(1);
3515
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003516 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3517 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003518}
3519
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003520SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00003521PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003522 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00003523 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00003524 SDLoc &dl = CLI.DL;
3525 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3526 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3527 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00003528 SDValue Chain = CLI.Chain;
3529 SDValue Callee = CLI.Callee;
3530 bool &isTailCall = CLI.IsTailCall;
3531 CallingConv::ID CallConv = CLI.CallConv;
3532 bool isVarArg = CLI.IsVarArg;
3533
Evan Cheng67a69dd2010-01-27 00:07:07 +00003534 if (isTailCall)
3535 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3536 Ins, DAG);
3537
Bill Schmidt57d6de52012-10-23 15:51:16 +00003538 if (PPCSubTarget.isSVR4ABI()) {
3539 if (PPCSubTarget.isPPC64())
3540 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3541 isTailCall, Outs, OutVals, Ins,
3542 dl, DAG, InVals);
3543 else
3544 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3545 isTailCall, Outs, OutVals, Ins,
3546 dl, DAG, InVals);
3547 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003548
Bill Schmidt57d6de52012-10-23 15:51:16 +00003549 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3550 isTailCall, Outs, OutVals, Ins,
3551 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003552}
3553
3554SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003555PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3556 CallingConv::ID CallConv, bool isVarArg,
3557 bool isTailCall,
3558 const SmallVectorImpl<ISD::OutputArg> &Outs,
3559 const SmallVectorImpl<SDValue> &OutVals,
3560 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003561 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003562 SmallVectorImpl<SDValue> &InVals) const {
3563 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003564 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003565
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003566 assert((CallConv == CallingConv::C ||
3567 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003568
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003569 unsigned PtrByteSize = 4;
3570
3571 MachineFunction &MF = DAG.getMachineFunction();
3572
3573 // Mark this function as potentially containing a function that contains a
3574 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3575 // and restoring the callers stack pointer in this functions epilog. This is
3576 // done because by tail calling the called function might overwrite the value
3577 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003578 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3579 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003580 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00003581
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003582 // Count how many bytes are to be pushed on the stack, including the linkage
3583 // area, parameter list area and the part of the local variable space which
3584 // contains copies of aggregates which are passed by value.
3585
3586 // Assign locations to all of the outgoing arguments.
3587 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003588 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003589 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003590
3591 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003592 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003593
3594 if (isVarArg) {
3595 // Handle fixed and variable vector arguments differently.
3596 // Fixed vector arguments go into registers as long as registers are
3597 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003598 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00003599
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003600 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003601 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003602 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003603 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00003604
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003605 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00003606 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3607 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003608 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00003609 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3610 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003611 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003612
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003613 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003614#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00003615 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00003616 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003617#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00003618 llvm_unreachable(0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003619 }
3620 }
3621 } else {
3622 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00003623 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003624 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003625
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003626 // Assign locations to all of the outgoing aggregate by value arguments.
3627 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003628 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003629 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003630
3631 // Reserve stack space for the allocations in CCInfo.
3632 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3633
Bill Schmidtef17c142013-02-06 17:33:58 +00003634 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003635
3636 // Size of the linkage area, parameter list area and the part of the local
3637 // space variable where copies of aggregates which are passed by value are
3638 // stored.
3639 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003640
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003641 // Calculate by how many bytes the stack has to be adjusted in case of tail
3642 // call optimization.
3643 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3644
3645 // Adjust the stack pointer for the new arguments...
3646 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003647 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3648 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003649 SDValue CallSeqStart = Chain;
3650
3651 // Load the return address and frame pointer so it can be moved somewhere else
3652 // later.
3653 SDValue LROp, FPOp;
3654 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3655 dl);
3656
3657 // Set up a copy of the stack pointer for use loading and storing any
3658 // arguments that may not fit in the registers available for argument
3659 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00003660 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00003661
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003662 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3663 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3664 SmallVector<SDValue, 8> MemOpChains;
3665
Roman Divacky71038e72011-08-30 17:04:16 +00003666 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003667 // Walk the register/memloc assignments, inserting copies/loads.
3668 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3669 i != e;
3670 ++i) {
3671 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003672 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003673 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00003674
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003675 if (Flags.isByVal()) {
3676 // Argument is an aggregate which is passed by value, thus we need to
3677 // create a copy of it in the local variable space of the current stack
3678 // frame (which is the stack frame of the caller) and pass the address of
3679 // this copy to the callee.
3680 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3681 CCValAssign &ByValVA = ByValArgLocs[j++];
3682 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00003683
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003684 // Memory reserved in the local variable space of the callers stack frame.
3685 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003686
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003687 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3688 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00003689
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003690 // Create a copy of the argument in the local area of the current
3691 // stack frame.
3692 SDValue MemcpyCall =
3693 CreateCopyOfByValArgument(Arg, PtrOff,
3694 CallSeqStart.getNode()->getOperand(0),
3695 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00003696
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003697 // This must go outside the CALLSEQ_START..END.
3698 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003699 CallSeqStart.getNode()->getOperand(1),
3700 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003701 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3702 NewCallSeqStart.getNode());
3703 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00003704
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003705 // Pass the address of the aggregate copy on the stack either in a
3706 // physical register or in the parameter list area of the current stack
3707 // frame to the callee.
3708 Arg = PtrOff;
3709 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003710
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003711 if (VA.isRegLoc()) {
Roman Divacky71038e72011-08-30 17:04:16 +00003712 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003713 // Put argument in a physical register.
3714 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3715 } else {
3716 // Put argument in the parameter list area of the current stack frame.
3717 assert(VA.isMemLoc());
3718 unsigned LocMemOffset = VA.getLocMemOffset();
3719
3720 if (!isTailCall) {
3721 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3722 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3723
3724 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00003725 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00003726 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003727 } else {
3728 // Calculate and remember argument location.
3729 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3730 TailCallArguments);
3731 }
3732 }
3733 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003734
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003735 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003736 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003737 &MemOpChains[0], MemOpChains.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00003738
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003739 // Build a sequence of copy-to-reg nodes chained together with token chain
3740 // and flag operands which copy the outgoing args into the appropriate regs.
3741 SDValue InFlag;
3742 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3743 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3744 RegsToPass[i].second, InFlag);
3745 InFlag = Chain.getValue(1);
3746 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003747
Hal Finkel5ab37802012-08-28 02:10:27 +00003748 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3749 // registers.
3750 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003751 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3752 SDValue Ops[] = { Chain, InFlag };
3753
Hal Finkel5ab37802012-08-28 02:10:27 +00003754 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003755 dl, VTs, Ops, InFlag.getNode() ? 2 : 1);
3756
Hal Finkel5ab37802012-08-28 02:10:27 +00003757 InFlag = Chain.getValue(1);
3758 }
3759
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003760 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003761 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3762 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003763
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003764 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3765 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3766 Ins, InVals);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003767}
3768
Bill Schmidt57d6de52012-10-23 15:51:16 +00003769// Copy an argument into memory, being careful to do this outside the
3770// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003771SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00003772PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3773 SDValue CallSeqStart,
3774 ISD::ArgFlagsTy Flags,
3775 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003776 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003777 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3778 CallSeqStart.getNode()->getOperand(0),
3779 Flags, DAG, dl);
3780 // The MEMCPY must go outside the CALLSEQ_START..END.
3781 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003782 CallSeqStart.getNode()->getOperand(1),
3783 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00003784 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3785 NewCallSeqStart.getNode());
3786 return NewCallSeqStart;
3787}
3788
3789SDValue
3790PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003791 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003792 bool isTailCall,
3793 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003794 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003795 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003796 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003797 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003798
Bill Schmidt57d6de52012-10-23 15:51:16 +00003799 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003800
Bill Schmidt57d6de52012-10-23 15:51:16 +00003801 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3802 unsigned PtrByteSize = 8;
3803
3804 MachineFunction &MF = DAG.getMachineFunction();
3805
3806 // Mark this function as potentially containing a function that contains a
3807 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3808 // and restoring the callers stack pointer in this functions epilog. This is
3809 // done because by tail calling the called function might overwrite the value
3810 // in this function's (MF) stack pointer stack slot 0(SP).
3811 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3812 CallConv == CallingConv::Fast)
3813 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3814
3815 unsigned nAltivecParamsAtEnd = 0;
3816
3817 // Count how many bytes are to be pushed on the stack, including the linkage
3818 // area, and parameter passing area. We start with at least 48 bytes, which
3819 // is reserved space for [SP][CR][LR][3 x unused].
3820 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
3821 // of this call.
3822 unsigned NumBytes =
3823 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
3824 Outs, OutVals, nAltivecParamsAtEnd);
3825
3826 // Calculate by how many bytes the stack has to be adjusted in case of tail
3827 // call optimization.
3828 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3829
3830 // To protect arguments on the stack from being clobbered in a tail call,
3831 // force all the loads to happen before doing any other lowering.
3832 if (isTailCall)
3833 Chain = DAG.getStackArgumentTokenFactor(Chain);
3834
3835 // Adjust the stack pointer for the new arguments...
3836 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003837 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3838 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003839 SDValue CallSeqStart = Chain;
3840
3841 // Load the return address and frame pointer so it can be move somewhere else
3842 // later.
3843 SDValue LROp, FPOp;
3844 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
3845 dl);
3846
3847 // Set up a copy of the stack pointer for use loading and storing any
3848 // arguments that may not fit in the registers available for argument
3849 // passing.
3850 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
3851
3852 // Figure out which arguments are going to go in registers, and which in
3853 // memory. Also, if this is a vararg function, floating point operations
3854 // must be stored to our stack, and loaded into integer regs as well, if
3855 // any integer regs are available for argument passing.
3856 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
3857 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
3858
3859 static const uint16_t GPR[] = {
3860 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3861 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3862 };
3863 static const uint16_t *FPR = GetFPR();
3864
3865 static const uint16_t VR[] = {
3866 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3867 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3868 };
3869 const unsigned NumGPRs = array_lengthof(GPR);
3870 const unsigned NumFPRs = 13;
3871 const unsigned NumVRs = array_lengthof(VR);
3872
3873 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3874 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3875
3876 SmallVector<SDValue, 8> MemOpChains;
3877 for (unsigned i = 0; i != NumOps; ++i) {
3878 SDValue Arg = OutVals[i];
3879 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3880
3881 // PtrOff will be used to store the current argument to the stack if a
3882 // register cannot be found for it.
3883 SDValue PtrOff;
3884
3885 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
3886
3887 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
3888
3889 // Promote integers to 64-bit values.
3890 if (Arg.getValueType() == MVT::i32) {
3891 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
3892 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3893 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
3894 }
3895
3896 // FIXME memcpy is used way more than necessary. Correctness first.
3897 // Note: "by value" is code for passing a structure by value, not
3898 // basic types.
3899 if (Flags.isByVal()) {
3900 // Note: Size includes alignment padding, so
3901 // struct x { short a; char b; }
3902 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
3903 // These are the proper values we need for right-justifying the
3904 // aggregate in a parameter register.
3905 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00003906
3907 // An empty aggregate parameter takes up no storage and no
3908 // registers.
3909 if (Size == 0)
3910 continue;
3911
Hal Finkel262a2242013-09-12 23:20:06 +00003912 unsigned BVAlign = Flags.getByValAlign();
3913 if (BVAlign > 8) {
3914 if (BVAlign % PtrByteSize != 0)
3915 llvm_unreachable(
3916 "ByVal alignment is not a multiple of the pointer size");
3917
3918 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
3919 }
3920
Bill Schmidt57d6de52012-10-23 15:51:16 +00003921 // All aggregates smaller than 8 bytes must be passed right-justified.
3922 if (Size==1 || Size==2 || Size==4) {
3923 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
3924 if (GPR_idx != NumGPRs) {
3925 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
3926 MachinePointerInfo(), VT,
3927 false, false, 0);
3928 MemOpChains.push_back(Load.getValue(1));
3929 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3930
3931 ArgOffset += PtrByteSize;
3932 continue;
3933 }
3934 }
3935
3936 if (GPR_idx == NumGPRs && Size < 8) {
3937 SDValue Const = DAG.getConstant(PtrByteSize - Size,
3938 PtrOff.getValueType());
3939 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3940 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3941 CallSeqStart,
3942 Flags, DAG, dl);
3943 ArgOffset += PtrByteSize;
3944 continue;
3945 }
3946 // Copy entire object into memory. There are cases where gcc-generated
3947 // code assumes it is there, even if it could be put entirely into
3948 // registers. (This is not what the doc says.)
3949
3950 // FIXME: The above statement is likely due to a misunderstanding of the
3951 // documents. All arguments must be copied into the parameter area BY
3952 // THE CALLEE in the event that the callee takes the address of any
3953 // formal argument. That has not yet been implemented. However, it is
3954 // reasonable to use the stack area as a staging area for the register
3955 // load.
3956
3957 // Skip this for small aggregates, as we will use the same slot for a
3958 // right-justified copy, below.
3959 if (Size >= 8)
3960 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
3961 CallSeqStart,
3962 Flags, DAG, dl);
3963
3964 // When a register is available, pass a small aggregate right-justified.
3965 if (Size < 8 && GPR_idx != NumGPRs) {
3966 // The easiest way to get this right-justified in a register
3967 // is to copy the structure into the rightmost portion of a
3968 // local variable slot, then load the whole slot into the
3969 // register.
3970 // FIXME: The memcpy seems to produce pretty awful code for
3971 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00003972 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00003973 // parameter save area instead of a new local variable.
3974 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
3975 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3976 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3977 CallSeqStart,
3978 Flags, DAG, dl);
3979
3980 // Load the slot into the register.
3981 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
3982 MachinePointerInfo(),
3983 false, false, false, 0);
3984 MemOpChains.push_back(Load.getValue(1));
3985 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3986
3987 // Done with this argument.
3988 ArgOffset += PtrByteSize;
3989 continue;
3990 }
3991
3992 // For aggregates larger than PtrByteSize, copy the pieces of the
3993 // object that fit into registers from the parameter save area.
3994 for (unsigned j=0; j<Size; j+=PtrByteSize) {
3995 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
3996 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
3997 if (GPR_idx != NumGPRs) {
3998 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
3999 MachinePointerInfo(),
4000 false, false, false, 0);
4001 MemOpChains.push_back(Load.getValue(1));
4002 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4003 ArgOffset += PtrByteSize;
4004 } else {
4005 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4006 break;
4007 }
4008 }
4009 continue;
4010 }
4011
Craig Topper56710102013-08-15 02:33:50 +00004012 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004013 default: llvm_unreachable("Unexpected ValueType for argument!");
4014 case MVT::i32:
4015 case MVT::i64:
4016 if (GPR_idx != NumGPRs) {
4017 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
4018 } else {
4019 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4020 true, isTailCall, false, MemOpChains,
4021 TailCallArguments, dl);
4022 }
4023 ArgOffset += PtrByteSize;
4024 break;
4025 case MVT::f32:
4026 case MVT::f64:
4027 if (FPR_idx != NumFPRs) {
4028 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4029
4030 if (isVarArg) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004031 // A single float or an aggregate containing only a single float
4032 // must be passed right-justified in the stack doubleword, and
4033 // in the GPR, if one is available.
4034 SDValue StoreOff;
Craig Topper56710102013-08-15 02:33:50 +00004035 if (Arg.getSimpleValueType().SimpleTy == MVT::f32) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004036 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4037 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4038 } else
4039 StoreOff = PtrOff;
4040
4041 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004042 MachinePointerInfo(), false, false, 0);
4043 MemOpChains.push_back(Store);
4044
4045 // Float varargs are always shadowed in available integer registers
4046 if (GPR_idx != NumGPRs) {
4047 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4048 MachinePointerInfo(), false, false,
4049 false, 0);
4050 MemOpChains.push_back(Load.getValue(1));
4051 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4052 }
4053 } else if (GPR_idx != NumGPRs)
4054 // If we have any FPRs remaining, we may also have GPRs remaining.
4055 ++GPR_idx;
4056 } else {
4057 // Single-precision floating-point values are mapped to the
4058 // second (rightmost) word of the stack doubleword.
4059 if (Arg.getValueType() == MVT::f32) {
4060 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4061 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4062 }
4063
4064 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4065 true, isTailCall, false, MemOpChains,
4066 TailCallArguments, dl);
4067 }
4068 ArgOffset += 8;
4069 break;
4070 case MVT::v4f32:
4071 case MVT::v4i32:
4072 case MVT::v8i16:
4073 case MVT::v16i8:
4074 if (isVarArg) {
4075 // These go aligned on the stack, or in the corresponding R registers
4076 // when within range. The Darwin PPC ABI doc claims they also go in
4077 // V registers; in fact gcc does this only for arguments that are
4078 // prototyped, not for those that match the ... We do it for all
4079 // arguments, seems to work.
4080 while (ArgOffset % 16 !=0) {
4081 ArgOffset += PtrByteSize;
4082 if (GPR_idx != NumGPRs)
4083 GPR_idx++;
4084 }
4085 // We could elide this store in the case where the object fits
4086 // entirely in R registers. Maybe later.
4087 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
4088 DAG.getConstant(ArgOffset, PtrVT));
4089 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4090 MachinePointerInfo(), false, false, 0);
4091 MemOpChains.push_back(Store);
4092 if (VR_idx != NumVRs) {
4093 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4094 MachinePointerInfo(),
4095 false, false, false, 0);
4096 MemOpChains.push_back(Load.getValue(1));
4097 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4098 }
4099 ArgOffset += 16;
4100 for (unsigned i=0; i<16; i+=PtrByteSize) {
4101 if (GPR_idx == NumGPRs)
4102 break;
4103 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4104 DAG.getConstant(i, PtrVT));
4105 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4106 false, false, false, 0);
4107 MemOpChains.push_back(Load.getValue(1));
4108 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4109 }
4110 break;
4111 }
4112
4113 // Non-varargs Altivec params generally go in registers, but have
4114 // stack space allocated at the end.
4115 if (VR_idx != NumVRs) {
4116 // Doesn't have GPR space allocated.
4117 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4118 } else {
4119 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4120 true, isTailCall, true, MemOpChains,
4121 TailCallArguments, dl);
4122 ArgOffset += 16;
4123 }
4124 break;
4125 }
4126 }
4127
4128 if (!MemOpChains.empty())
4129 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4130 &MemOpChains[0], MemOpChains.size());
4131
4132 // Check if this is an indirect call (MTCTR/BCTRL).
4133 // See PrepareCall() for more information about calls through function
4134 // pointers in the 64-bit SVR4 ABI.
4135 if (!isTailCall &&
4136 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4137 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4138 !isBLACompatibleAddress(Callee, DAG)) {
4139 // Load r2 into a virtual register and store it to the TOC save area.
4140 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4141 // TOC save area offset.
4142 SDValue PtrOff = DAG.getIntPtrConstant(40);
4143 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4144 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4145 false, false, 0);
4146 // R12 must contain the address of an indirect callee. This does not
4147 // mean the MTCTR instruction must use R12; it's easier to model this
4148 // as an extra parameter, so do that.
4149 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4150 }
4151
4152 // Build a sequence of copy-to-reg nodes chained together with token chain
4153 // and flag operands which copy the outgoing args into the appropriate regs.
4154 SDValue InFlag;
4155 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4156 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4157 RegsToPass[i].second, InFlag);
4158 InFlag = Chain.getValue(1);
4159 }
4160
4161 if (isTailCall)
4162 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4163 FPOp, true, TailCallArguments);
4164
4165 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4166 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4167 Ins, InVals);
4168}
4169
4170SDValue
4171PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4172 CallingConv::ID CallConv, bool isVarArg,
4173 bool isTailCall,
4174 const SmallVectorImpl<ISD::OutputArg> &Outs,
4175 const SmallVectorImpl<SDValue> &OutVals,
4176 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004177 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004178 SmallVectorImpl<SDValue> &InVals) const {
4179
4180 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004181
Owen Anderson53aa7a92009-08-10 22:56:29 +00004182 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00004183 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004184 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004185
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004186 MachineFunction &MF = DAG.getMachineFunction();
4187
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004188 // Mark this function as potentially containing a function that contains a
4189 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4190 // and restoring the callers stack pointer in this functions epilog. This is
4191 // done because by tail calling the called function might overwrite the value
4192 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004193 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4194 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004195 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4196
4197 unsigned nAltivecParamsAtEnd = 0;
4198
Chris Lattneraa40ec12006-05-16 22:56:08 +00004199 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00004200 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00004201 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004202 unsigned NumBytes =
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004203 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004204 Outs, OutVals,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004205 nAltivecParamsAtEnd);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004206
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004207 // Calculate by how many bytes the stack has to be adjusted in case of tail
4208 // call optimization.
4209 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004210
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004211 // To protect arguments on the stack from being clobbered in a tail call,
4212 // force all the loads to happen before doing any other lowering.
4213 if (isTailCall)
4214 Chain = DAG.getStackArgumentTokenFactor(Chain);
4215
Chris Lattnerb7552a82006-05-17 00:15:40 +00004216 // Adjust the stack pointer for the new arguments...
4217 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004218 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4219 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004220 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004221
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004222 // Load the return address and frame pointer so it can be move somewhere else
4223 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004224 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004225 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4226 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004227
Chris Lattnerb7552a82006-05-17 00:15:40 +00004228 // Set up a copy of the stack pointer for use loading and storing any
4229 // arguments that may not fit in the registers available for argument
4230 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004231 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004232 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004233 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004234 else
Owen Anderson9f944592009-08-11 20:47:22 +00004235 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004236
Chris Lattnerb7552a82006-05-17 00:15:40 +00004237 // Figure out which arguments are going to go in registers, and which in
4238 // memory. Also, if this is a vararg function, floating point operations
4239 // must be stored to our stack, and loaded into integer regs as well, if
4240 // any integer regs are available for argument passing.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004241 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004242 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004243
Craig Topperca658c22012-03-11 07:16:55 +00004244 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004245 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4246 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4247 };
Craig Topperca658c22012-03-11 07:16:55 +00004248 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00004249 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4250 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4251 };
Craig Topperca658c22012-03-11 07:16:55 +00004252 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004253
Craig Topperca658c22012-03-11 07:16:55 +00004254 static const uint16_t VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004255 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4256 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4257 };
Owen Andersone2f23a32007-09-07 04:06:50 +00004258 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004259 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00004260 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004261
Craig Topperca658c22012-03-11 07:16:55 +00004262 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004263
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004264 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004265 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4266
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004267 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00004268 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004269 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004270 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004271
Chris Lattnerb7552a82006-05-17 00:15:40 +00004272 // PtrOff will be used to store the current argument to the stack if a
4273 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004274 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004275
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004276 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004277
Dale Johannesen679073b2009-02-04 02:34:38 +00004278 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004279
4280 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00004281 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00004282 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4283 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00004284 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004285 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00004286
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004287 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004288 // Note: "by value" is code for passing a structure by value, not
4289 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00004290 if (Flags.isByVal()) {
4291 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004292 // Very small objects are passed right-justified. Everything else is
4293 // passed left-justified.
4294 if (Size==1 || Size==2) {
4295 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004296 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00004297 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00004298 MachinePointerInfo(), VT,
4299 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004300 MemOpChains.push_back(Load.getValue(1));
4301 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004302
4303 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004304 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00004305 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4306 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004307 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004308 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4309 CallSeqStart,
4310 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004311 ArgOffset += PtrByteSize;
4312 }
4313 continue;
4314 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004315 // Copy entire object into memory. There are cases where gcc-generated
4316 // code assumes it is there, even if it could be put entirely into
4317 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004318 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4319 CallSeqStart,
4320 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004321
4322 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4323 // copy the pieces of the object that fit into registers from the
4324 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00004325 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004326 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004327 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00004328 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004329 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4330 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004331 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00004332 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00004333 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004334 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004335 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004336 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004337 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004338 }
4339 }
4340 continue;
4341 }
4342
Craig Topper56710102013-08-15 02:33:50 +00004343 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004344 default: llvm_unreachable("Unexpected ValueType for argument!");
Owen Anderson9f944592009-08-11 20:47:22 +00004345 case MVT::i32:
4346 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004347 if (GPR_idx != NumGPRs) {
4348 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004349 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004350 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4351 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004352 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00004353 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004354 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004355 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004356 case MVT::f32:
4357 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004358 if (FPR_idx != NumFPRs) {
4359 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4360
Chris Lattnerb7552a82006-05-17 00:15:40 +00004361 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00004362 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4363 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004364 MemOpChains.push_back(Store);
4365
Chris Lattnerb7552a82006-05-17 00:15:40 +00004366 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004367 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004368 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004369 MachinePointerInfo(), false, false,
4370 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004371 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004372 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004373 }
Owen Anderson9f944592009-08-11 20:47:22 +00004374 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004375 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004376 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00004377 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4378 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004379 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004380 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004381 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00004382 }
4383 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00004384 // If we have any FPRs remaining, we may also have GPRs remaining.
4385 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4386 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004387 if (GPR_idx != NumGPRs)
4388 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00004389 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004390 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4391 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004392 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004393 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004394 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4395 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004396 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004397 if (isPPC64)
4398 ArgOffset += 8;
4399 else
Owen Anderson9f944592009-08-11 20:47:22 +00004400 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004401 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004402 case MVT::v4f32:
4403 case MVT::v4i32:
4404 case MVT::v8i16:
4405 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00004406 if (isVarArg) {
4407 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00004408 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00004409 // V registers; in fact gcc does this only for arguments that are
4410 // prototyped, not for those that match the ... We do it for all
4411 // arguments, seems to work.
4412 while (ArgOffset % 16 !=0) {
4413 ArgOffset += PtrByteSize;
4414 if (GPR_idx != NumGPRs)
4415 GPR_idx++;
4416 }
4417 // We could elide this store in the case where the object fits
4418 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004419 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004420 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00004421 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4422 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004423 MemOpChains.push_back(Store);
4424 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004425 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00004426 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004427 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004428 MemOpChains.push_back(Load.getValue(1));
4429 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4430 }
4431 ArgOffset += 16;
4432 for (unsigned i=0; i<16; i+=PtrByteSize) {
4433 if (GPR_idx == NumGPRs)
4434 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00004435 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004436 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00004437 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004438 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004439 MemOpChains.push_back(Load.getValue(1));
4440 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4441 }
4442 break;
4443 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004444
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004445 // Non-varargs Altivec params generally go in registers, but have
4446 // stack space allocated at the end.
4447 if (VR_idx != NumVRs) {
4448 // Doesn't have GPR space allocated.
4449 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4450 } else if (nAltivecParamsAtEnd==0) {
4451 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004452 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4453 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004454 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004455 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00004456 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00004457 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004458 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00004459 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004460 // If all Altivec parameters fit in registers, as they usually do,
4461 // they get stack space following the non-Altivec parameters. We
4462 // don't track this here because nobody below needs it.
4463 // If there are more Altivec parameters than fit in registers emit
4464 // the stores here.
4465 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4466 unsigned j = 0;
4467 // Offset is aligned; skip 1st 12 params which go in V registers.
4468 ArgOffset = ((ArgOffset+15)/16)*16;
4469 ArgOffset += 12*16;
4470 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004471 SDValue Arg = OutVals[i];
4472 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00004473 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4474 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004475 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004476 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004477 // We are emitting Altivec params in order.
4478 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4479 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004480 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004481 ArgOffset += 16;
4482 }
4483 }
4484 }
4485 }
4486
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004487 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00004488 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnered728e82006-08-11 17:38:39 +00004489 &MemOpChains[0], MemOpChains.size());
Scott Michelcf0da6c2009-02-17 22:15:04 +00004490
Dale Johannesen90eab672010-03-09 20:15:42 +00004491 // On Darwin, R12 must contain the address of an indirect callee. This does
4492 // not mean the MTCTR instruction must use R12; it's easier to model this as
4493 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004494 if (!isTailCall &&
Dale Johannesen90eab672010-03-09 20:15:42 +00004495 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4496 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4497 !isBLACompatibleAddress(Callee, DAG))
4498 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4499 PPC::R12), Callee));
4500
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004501 // Build a sequence of copy-to-reg nodes chained together with token chain
4502 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004503 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004504 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00004505 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00004506 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004507 InFlag = Chain.getValue(1);
4508 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004509
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004510 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004511 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4512 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004513
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004514 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4515 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4516 Ins, InVals);
Chris Lattneraa40ec12006-05-16 22:56:08 +00004517}
4518
Hal Finkel450128a2011-10-14 19:51:36 +00004519bool
4520PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4521 MachineFunction &MF, bool isVarArg,
4522 const SmallVectorImpl<ISD::OutputArg> &Outs,
4523 LLVMContext &Context) const {
4524 SmallVector<CCValAssign, 16> RVLocs;
4525 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4526 RVLocs, Context);
4527 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4528}
4529
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004530SDValue
4531PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004532 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004533 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004534 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004535 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004536
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004537 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004538 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00004539 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004540 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004541
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004542 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004543 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004544
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004545 // Copy the result values into the output registers.
4546 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4547 CCValAssign &VA = RVLocs[i];
4548 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004549
4550 SDValue Arg = OutVals[i];
4551
4552 switch (VA.getLocInfo()) {
4553 default: llvm_unreachable("Unknown loc info!");
4554 case CCValAssign::Full: break;
4555 case CCValAssign::AExt:
4556 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4557 break;
4558 case CCValAssign::ZExt:
4559 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4560 break;
4561 case CCValAssign::SExt:
4562 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4563 break;
4564 }
4565
4566 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004567 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004568 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004569 }
4570
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004571 RetOps[0] = Chain; // Update chain.
4572
4573 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00004574 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004575 RetOps.push_back(Flag);
4576
4577 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other,
4578 &RetOps[0], RetOps.size());
Chris Lattner4211ca92006-04-14 06:01:58 +00004579}
4580
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004581SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004582 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00004583 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004584 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004585
Jim Laskeye4f4d042006-12-04 22:04:42 +00004586 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004587 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00004588
4589 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00004590 bool isPPC64 = Subtarget.isPPC64();
4591 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004592 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004593
4594 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004595 SDValue Chain = Op.getOperand(0);
4596 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004597
Jim Laskeye4f4d042006-12-04 22:04:42 +00004598 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00004599 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4600 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004601 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004602
Jim Laskeye4f4d042006-12-04 22:04:42 +00004603 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00004604 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004605
Jim Laskeye4f4d042006-12-04 22:04:42 +00004606 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00004607 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004608 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004609}
4610
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004611
4612
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004613SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004614PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004615 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004616 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004617 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004618 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004619
4620 // Get current frame pointer save index. The users of this index will be
4621 // primarily DYNALLOC instructions.
4622 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4623 int RASI = FI->getReturnAddrSaveIndex();
4624
4625 // If the frame pointer save index hasn't been defined yet.
4626 if (!RASI) {
4627 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004628 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004629 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004630 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004631 // Save the result.
4632 FI->setReturnAddrSaveIndex(RASI);
4633 }
4634 return DAG.getFrameIndex(RASI, PtrVT);
4635}
4636
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004637SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004638PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4639 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004640 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004641 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004642 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004643
4644 // Get current frame pointer save index. The users of this index will be
4645 // primarily DYNALLOC instructions.
4646 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4647 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004648
Jim Laskey48850c12006-11-16 22:43:37 +00004649 // If the frame pointer save index hasn't been defined yet.
4650 if (!FPSI) {
4651 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004652 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004653 isDarwinABI);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004654
Jim Laskey48850c12006-11-16 22:43:37 +00004655 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004656 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00004657 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004658 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00004659 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004660 return DAG.getFrameIndex(FPSI, PtrVT);
4661}
Jim Laskey48850c12006-11-16 22:43:37 +00004662
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004663SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004664 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004665 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004666 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004667 SDValue Chain = Op.getOperand(0);
4668 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004669 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004670
Jim Laskey48850c12006-11-16 22:43:37 +00004671 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004672 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004673 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004674 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00004675 DAG.getConstant(0, PtrVT), Size);
4676 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004677 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00004678 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004679 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00004680 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004681 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey48850c12006-11-16 22:43:37 +00004682}
4683
Hal Finkel756810f2013-03-21 21:37:52 +00004684SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
4685 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004686 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004687 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
4688 DAG.getVTList(MVT::i32, MVT::Other),
4689 Op.getOperand(0), Op.getOperand(1));
4690}
4691
4692SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
4693 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004694 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004695 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
4696 Op.getOperand(0), Op.getOperand(1));
4697}
4698
Chris Lattner4211ca92006-04-14 06:01:58 +00004699/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4700/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004701SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00004702 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00004703 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4704 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00004705 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004706
Hal Finkel81f87992013-04-07 22:11:09 +00004707 // We might be able to do better than this under some circumstances, but in
4708 // general, fsel-based lowering of select is a finite-math-only optimization.
4709 // For more information, see section F.3 of the 2.06 ISA specification.
4710 if (!DAG.getTarget().Options.NoInfsFPMath ||
4711 !DAG.getTarget().Options.NoNaNsFPMath)
4712 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004713
Hal Finkel81f87992013-04-07 22:11:09 +00004714 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004715
Owen Anderson53aa7a92009-08-10 22:56:29 +00004716 EVT ResVT = Op.getValueType();
4717 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004718 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4719 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004720 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004721
Chris Lattner4211ca92006-04-14 06:01:58 +00004722 // If the RHS of the comparison is a 0.0, we don't need to do the
4723 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00004724 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00004725 if (isFloatingPointZero(RHS))
4726 switch (CC) {
4727 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004728 case ISD::SETNE:
4729 std::swap(TV, FV);
4730 case ISD::SETEQ:
4731 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4732 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
4733 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
4734 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4735 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4736 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4737 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004738 case ISD::SETULT:
4739 case ISD::SETLT:
4740 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004741 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004742 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00004743 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4744 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004745 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004746 case ISD::SETUGT:
4747 case ISD::SETGT:
4748 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004749 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004750 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00004751 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4752 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004753 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00004754 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004755 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004756
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004757 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00004758 switch (CC) {
4759 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004760 case ISD::SETNE:
4761 std::swap(TV, FV);
4762 case ISD::SETEQ:
4763 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
4764 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4765 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
4766 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
4767 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4768 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4769 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4770 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004771 case ISD::SETULT:
4772 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004773 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004774 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4775 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004776 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004777 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004778 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004779 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004780 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4781 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004782 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004783 case ISD::SETUGT:
4784 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004785 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004786 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4787 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004788 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004789 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004790 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004791 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004792 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4793 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004794 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004795 }
Eli Friedman5806e182009-05-28 04:31:08 +00004796 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00004797}
4798
Chris Lattner57ee7c62007-11-28 18:44:47 +00004799// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen37bc85f2009-06-04 20:53:52 +00004800SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004801 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00004802 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004803 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00004804 if (Src.getValueType() == MVT::f32)
4805 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00004806
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004807 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00004808 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004809 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00004810 case MVT::i32:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00004811 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Hal Finkelf6d45f22013-04-01 17:52:07 +00004812 (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ :
4813 PPCISD::FCTIDZ),
Owen Anderson9f944592009-08-11 20:47:22 +00004814 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00004815 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004816 case MVT::i64:
Hal Finkel3f88d082013-04-01 18:42:58 +00004817 assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) &&
4818 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00004819 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
4820 PPCISD::FCTIDUZ,
4821 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00004822 break;
4823 }
Duncan Sands2a287912008-07-19 16:26:02 +00004824
Chris Lattner4211ca92006-04-14 06:01:58 +00004825 // Convert the FP value to an int value through memory.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004826 bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() &&
4827 (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT());
4828 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
4829 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
4830 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00004831
Chris Lattner06a49542007-10-15 20:14:52 +00004832 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004833 SDValue Chain;
4834 if (i32Stack) {
4835 MachineFunction &MF = DAG.getMachineFunction();
4836 MachineMemOperand *MMO =
4837 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
4838 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
4839 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
4840 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
4841 MVT::i32, MMO);
4842 } else
4843 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
4844 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00004845
4846 // Result is a load from the stack slot. If loading 4 bytes, make sure to
4847 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004848 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00004849 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00004850 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkelf6d45f22013-04-01 17:52:07 +00004851 MPI = MachinePointerInfo();
4852 }
4853
4854 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004855 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00004856}
4857
Hal Finkelf6d45f22013-04-01 17:52:07 +00004858SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004859 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004860 SDLoc dl(Op);
Dan Gohmand6819da2008-03-11 01:59:03 +00004861 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00004862 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004863 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00004864
Hal Finkelf6d45f22013-04-01 17:52:07 +00004865 assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) &&
4866 "UINT_TO_FP is supported only with FPCVT");
4867
4868 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00004869 // Otherwise, convert to double-precision and then round.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004870 unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4871 (Op.getOpcode() == ISD::UINT_TO_FP ?
4872 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
4873 (Op.getOpcode() == ISD::UINT_TO_FP ?
4874 PPCISD::FCFIDU : PPCISD::FCFID);
4875 MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4876 MVT::f32 : MVT::f64;
4877
Owen Anderson9f944592009-08-11 20:47:22 +00004878 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00004879 SDValue SINT = Op.getOperand(0);
4880 // When converting to single-precision, we actually need to convert
4881 // to double-precision first and then round to single-precision.
4882 // To avoid double-rounding effects during that operation, we have
4883 // to prepare the input operand. Bits that might be truncated when
4884 // converting to double-precision are replaced by a bit that won't
4885 // be lost at this stage, but is below the single-precision rounding
4886 // position.
4887 //
4888 // However, if -enable-unsafe-fp-math is in effect, accept double
4889 // rounding to avoid the extra overhead.
4890 if (Op.getValueType() == MVT::f32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00004891 !PPCSubTarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00004892 !DAG.getTarget().Options.UnsafeFPMath) {
4893
4894 // Twiddle input to make sure the low 11 bits are zero. (If this
4895 // is the case, we are guaranteed the value will fit into the 53 bit
4896 // mantissa of an IEEE double-precision value without rounding.)
4897 // If any of those low 11 bits were not zero originally, make sure
4898 // bit 12 (value 2048) is set instead, so that the final rounding
4899 // to single-precision gets the correct result.
4900 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4901 SINT, DAG.getConstant(2047, MVT::i64));
4902 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
4903 Round, DAG.getConstant(2047, MVT::i64));
4904 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
4905 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4906 Round, DAG.getConstant(-2048, MVT::i64));
4907
4908 // However, we cannot use that value unconditionally: if the magnitude
4909 // of the input value is small, the bit-twiddling we did above might
4910 // end up visibly changing the output. Fortunately, in that case, we
4911 // don't need to twiddle bits since the original input will convert
4912 // exactly to double-precision floating-point already. Therefore,
4913 // construct a conditional to use the original value if the top 11
4914 // bits are all sign-bit copies, and use the rounded value computed
4915 // above otherwise.
4916 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
4917 SINT, DAG.getConstant(53, MVT::i32));
4918 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
4919 Cond, DAG.getConstant(1, MVT::i64));
4920 Cond = DAG.getSetCC(dl, MVT::i32,
4921 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
4922
4923 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
4924 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00004925
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00004926 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkelf6d45f22013-04-01 17:52:07 +00004927 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
4928
4929 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00004930 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00004931 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00004932 return FP;
4933 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004934
Owen Anderson9f944592009-08-11 20:47:22 +00004935 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00004936 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00004937 // Since we only generate this in 64-bit mode, we can take advantage of
4938 // 64-bit registers. In particular, sign extend the input value into the
4939 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
4940 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00004941 MachineFunction &MF = DAG.getMachineFunction();
4942 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004943 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004944
Hal Finkelbeb296b2013-03-31 10:12:51 +00004945 SDValue Ld;
Hal Finkelf6d45f22013-04-01 17:52:07 +00004946 if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) {
Hal Finkelbeb296b2013-03-31 10:12:51 +00004947 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
4948 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004949
Hal Finkelbeb296b2013-03-31 10:12:51 +00004950 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
4951 MachinePointerInfo::getFixedStack(FrameIdx),
4952 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00004953
Hal Finkelbeb296b2013-03-31 10:12:51 +00004954 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
4955 "Expected an i32 store");
4956 MachineMemOperand *MMO =
4957 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
4958 MachineMemOperand::MOLoad, 4, 4);
4959 SDValue Ops[] = { Store, FIdx };
Hal Finkelf6d45f22013-04-01 17:52:07 +00004960 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
4961 PPCISD::LFIWZX : PPCISD::LFIWAX,
4962 dl, DAG.getVTList(MVT::f64, MVT::Other),
4963 Ops, 2, MVT::i32, MMO);
Hal Finkelbeb296b2013-03-31 10:12:51 +00004964 } else {
Hal Finkelf6d45f22013-04-01 17:52:07 +00004965 assert(PPCSubTarget.isPPC64() &&
4966 "i32->FP without LFIWAX supported only on PPC64");
4967
Hal Finkelbeb296b2013-03-31 10:12:51 +00004968 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
4969 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
4970
4971 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
4972 Op.getOperand(0));
4973
4974 // STD the extended value into the stack slot.
4975 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
4976 MachinePointerInfo::getFixedStack(FrameIdx),
4977 false, false, 0);
4978
4979 // Load the value as a double.
4980 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
4981 MachinePointerInfo::getFixedStack(FrameIdx),
4982 false, false, false, 0);
4983 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004984
Chris Lattner4211ca92006-04-14 06:01:58 +00004985 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004986 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
4987 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00004988 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00004989 return FP;
4990}
4991
Dan Gohman21cea8a2010-04-17 15:26:15 +00004992SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
4993 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004994 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00004995 /*
4996 The rounding mode is in bits 30:31 of FPSR, and has the following
4997 settings:
4998 00 Round to nearest
4999 01 Round to 0
5000 10 Round to +inf
5001 11 Round to -inf
5002
5003 FLT_ROUNDS, on the other hand, expects the following:
5004 -1 Undefined
5005 0 Round to 0
5006 1 Round to nearest
5007 2 Round to +inf
5008 3 Round to -inf
5009
5010 To perform the conversion, we do:
5011 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
5012 */
5013
5014 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005015 EVT VT = Op.getValueType();
5016 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005017 SDValue MFFSreg, InFlag;
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005018
5019 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005020 EVT NodeTys[] = {
5021 MVT::f64, // return register
5022 MVT::Glue // unused in this context
5023 };
Dale Johannesen021052a2009-02-04 20:06:27 +00005024 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005025
5026 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00005027 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005028 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005029 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00005030 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005031
5032 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005033 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005034 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00005035 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005036 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005037
5038 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005039 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00005040 DAG.getNode(ISD::AND, dl, MVT::i32,
5041 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005042 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00005043 DAG.getNode(ISD::SRL, dl, MVT::i32,
5044 DAG.getNode(ISD::AND, dl, MVT::i32,
5045 DAG.getNode(ISD::XOR, dl, MVT::i32,
5046 CWD, DAG.getConstant(3, MVT::i32)),
5047 DAG.getConstant(3, MVT::i32)),
5048 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005049
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005050 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00005051 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005052
Duncan Sands13237ac2008-06-06 12:08:01 +00005053 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00005054 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005055}
5056
Dan Gohman21cea8a2010-04-17 15:26:15 +00005057SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005058 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005059 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005060 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00005061 assert(Op.getNumOperands() == 3 &&
5062 VT == Op.getOperand(1).getValueType() &&
5063 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005064
Chris Lattner601b8652006-09-20 03:47:40 +00005065 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005066 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005067 SDValue Lo = Op.getOperand(0);
5068 SDValue Hi = Op.getOperand(1);
5069 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005070 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005071
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005072 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005073 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005074 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5075 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5076 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5077 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005078 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005079 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5080 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5081 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005082 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005083 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005084}
5085
Dan Gohman21cea8a2010-04-17 15:26:15 +00005086SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005087 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005088 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00005089 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005090 assert(Op.getNumOperands() == 3 &&
5091 VT == Op.getOperand(1).getValueType() &&
5092 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005093
Dan Gohman8d2ead22008-03-07 20:36:53 +00005094 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005095 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005096 SDValue Lo = Op.getOperand(0);
5097 SDValue Hi = Op.getOperand(1);
5098 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005099 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005100
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005101 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005102 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005103 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5104 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5105 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5106 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005107 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005108 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5109 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5110 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005111 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005112 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005113}
5114
Dan Gohman21cea8a2010-04-17 15:26:15 +00005115SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005116 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005117 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005118 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005119 assert(Op.getNumOperands() == 3 &&
5120 VT == Op.getOperand(1).getValueType() &&
5121 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005122
Dan Gohman8d2ead22008-03-07 20:36:53 +00005123 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005124 SDValue Lo = Op.getOperand(0);
5125 SDValue Hi = Op.getOperand(1);
5126 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005127 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005128
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005129 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005130 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005131 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5132 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5133 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5134 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005135 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005136 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5137 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5138 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00005139 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005140 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005141 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005142}
5143
5144//===----------------------------------------------------------------------===//
5145// Vector related lowering.
5146//
5147
Chris Lattner2a099c02006-04-17 06:00:21 +00005148/// BuildSplatI - Build a canonical splati of Val with an element size of
5149/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005150static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005151 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00005152 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005153
Owen Anderson53aa7a92009-08-10 22:56:29 +00005154 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00005155 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00005156 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005157
Owen Anderson9f944592009-08-11 20:47:22 +00005158 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005159
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005160 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5161 if (Val == -1)
5162 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005163
Owen Anderson53aa7a92009-08-10 22:56:29 +00005164 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005165
Chris Lattner2a099c02006-04-17 06:00:21 +00005166 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00005167 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005168 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00005169 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga49de9d2009-02-25 22:49:59 +00005170 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
5171 &Ops[0], Ops.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00005172 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005173}
5174
Hal Finkelcf2e9082013-05-24 23:00:14 +00005175/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5176/// specified intrinsic ID.
5177static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005178 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00005179 EVT DestVT = MVT::Other) {
5180 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5181 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5182 DAG.getConstant(IID, MVT::i32), Op);
5183}
5184
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005185/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00005186/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005187static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005188 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005189 EVT DestVT = MVT::Other) {
5190 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005191 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005192 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005193}
5194
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005195/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5196/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005197static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005198 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005199 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00005200 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005201 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005202 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005203}
5204
5205
Chris Lattner264c9082006-04-17 17:55:10 +00005206/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5207/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005208static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005209 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00005210 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00005211 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5212 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005213
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005214 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00005215 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005216 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00005217 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005218 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00005219}
5220
Chris Lattner19e90552006-04-14 05:19:18 +00005221// If this is a case we can't handle, return null and let the default
5222// expansion code take care of it. If we CAN select this case, and if it
5223// selects to a single instruction, return Op. Otherwise, if we can codegen
5224// this case more efficiently than a constant pool load, lower it to the
5225// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005226SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5227 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005228 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005229 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5230 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00005231
Bob Wilson85cefe82009-03-02 23:24:16 +00005232 // Check if this is a splat of a constant value.
5233 APInt APSplatBits, APSplatUndef;
5234 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005235 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00005236 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00005237 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00005238 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00005239
Bob Wilson530e0382009-03-03 19:26:27 +00005240 unsigned SplatBits = APSplatBits.getZExtValue();
5241 unsigned SplatUndef = APSplatUndef.getZExtValue();
5242 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005243
Bob Wilson530e0382009-03-03 19:26:27 +00005244 // First, handle single instruction cases.
5245
5246 // All zeros?
5247 if (SplatBits == 0) {
5248 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00005249 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5250 SDValue Z = DAG.getConstant(0, MVT::i32);
5251 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00005252 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00005253 }
Bob Wilson530e0382009-03-03 19:26:27 +00005254 return Op;
5255 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00005256
Bob Wilson530e0382009-03-03 19:26:27 +00005257 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5258 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5259 (32-SplatBitSize));
5260 if (SextVal >= -16 && SextVal <= 15)
5261 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005262
5263
Bob Wilson530e0382009-03-03 19:26:27 +00005264 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005265
Bob Wilson530e0382009-03-03 19:26:27 +00005266 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00005267 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5268 // If this value is in the range [17,31] and is odd, use:
5269 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5270 // If this value is in the range [-31,-17] and is odd, use:
5271 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5272 // Note the last two are three-instruction sequences.
5273 if (SextVal >= -32 && SextVal <= 31) {
5274 // To avoid having these optimizations undone by constant folding,
5275 // we convert to a pseudo that will be expanded later into one of
5276 // the above forms.
5277 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt51e79512013-02-20 15:50:31 +00005278 EVT VT = Op.getValueType();
5279 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5280 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5281 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilson530e0382009-03-03 19:26:27 +00005282 }
5283
5284 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5285 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5286 // for fneg/fabs.
5287 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5288 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00005289 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005290
5291 // Make the VSLW intrinsic, computing 0x8000_0000.
5292 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5293 OnesV, DAG, dl);
5294
5295 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00005296 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00005297 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005298 }
5299
5300 // Check to see if this is a wide variety of vsplti*, binop self cases.
5301 static const signed char SplatCsts[] = {
5302 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5303 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5304 };
5305
5306 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5307 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5308 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5309 int i = SplatCsts[idx];
5310
5311 // Figure out what shift amount will be used by altivec if shifted by i in
5312 // this splat size.
5313 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5314
5315 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00005316 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005317 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005318 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5319 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5320 Intrinsic::ppc_altivec_vslw
5321 };
5322 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005323 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005324 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005325
Bob Wilson530e0382009-03-03 19:26:27 +00005326 // vsplti + srl self.
5327 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005328 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005329 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5330 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5331 Intrinsic::ppc_altivec_vsrw
5332 };
5333 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005334 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005335 }
5336
Bob Wilson530e0382009-03-03 19:26:27 +00005337 // vsplti + sra self.
5338 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005339 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005340 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5341 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5342 Intrinsic::ppc_altivec_vsraw
5343 };
5344 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005345 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005346 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005347
Bob Wilson530e0382009-03-03 19:26:27 +00005348 // vsplti + rol self.
5349 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5350 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005351 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005352 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5353 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5354 Intrinsic::ppc_altivec_vrlw
5355 };
5356 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005357 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005358 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005359
Bob Wilson530e0382009-03-03 19:26:27 +00005360 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00005361 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005362 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005363 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00005364 }
Bob Wilson530e0382009-03-03 19:26:27 +00005365 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00005366 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005367 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005368 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00005369 }
Bob Wilson530e0382009-03-03 19:26:27 +00005370 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00005371 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005372 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005373 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5374 }
5375 }
5376
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005377 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00005378}
5379
Chris Lattner071ad012006-04-17 05:28:54 +00005380/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5381/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005382static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005383 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005384 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00005385 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00005386 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00005387 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005388
Chris Lattner071ad012006-04-17 05:28:54 +00005389 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00005390 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 +00005391 OP_VMRGHW,
5392 OP_VMRGLW,
5393 OP_VSPLTISW0,
5394 OP_VSPLTISW1,
5395 OP_VSPLTISW2,
5396 OP_VSPLTISW3,
5397 OP_VSLDOI4,
5398 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00005399 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00005400 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00005401
Chris Lattner071ad012006-04-17 05:28:54 +00005402 if (OpNum == OP_COPY) {
5403 if (LHSID == (1*9+2)*9+3) return LHS;
5404 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5405 return RHS;
5406 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005407
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005408 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005409 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5410 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005411
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005412 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00005413 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005414 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00005415 case OP_VMRGHW:
5416 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5417 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5418 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5419 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5420 break;
5421 case OP_VMRGLW:
5422 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5423 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5424 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5425 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5426 break;
5427 case OP_VSPLTISW0:
5428 for (unsigned i = 0; i != 16; ++i)
5429 ShufIdxs[i] = (i&3)+0;
5430 break;
5431 case OP_VSPLTISW1:
5432 for (unsigned i = 0; i != 16; ++i)
5433 ShufIdxs[i] = (i&3)+4;
5434 break;
5435 case OP_VSPLTISW2:
5436 for (unsigned i = 0; i != 16; ++i)
5437 ShufIdxs[i] = (i&3)+8;
5438 break;
5439 case OP_VSPLTISW3:
5440 for (unsigned i = 0; i != 16; ++i)
5441 ShufIdxs[i] = (i&3)+12;
5442 break;
5443 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005444 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005445 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005446 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005447 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005448 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005449 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00005450 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00005451 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5452 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005453 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00005454 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00005455}
5456
Chris Lattner19e90552006-04-14 05:19:18 +00005457/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5458/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5459/// return the code it can be lowered into. Worst case, it can always be
5460/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005461SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005462 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005463 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005464 SDValue V1 = Op.getOperand(0);
5465 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005466 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005467 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005468
Chris Lattner19e90552006-04-14 05:19:18 +00005469 // Cases that are handled by instructions that take permute immediates
5470 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5471 // selected by the instruction selector.
5472 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005473 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5474 PPC::isSplatShuffleMask(SVOp, 2) ||
5475 PPC::isSplatShuffleMask(SVOp, 4) ||
5476 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5477 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5478 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5479 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5480 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5481 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5482 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5483 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5484 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattner19e90552006-04-14 05:19:18 +00005485 return Op;
5486 }
5487 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005488
Chris Lattner19e90552006-04-14 05:19:18 +00005489 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5490 // and produce a fixed permutation. If any of these match, do not lower to
5491 // VPERM.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005492 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5493 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5494 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5495 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5496 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5497 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5498 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5499 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5500 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattner19e90552006-04-14 05:19:18 +00005501 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005502
Chris Lattner071ad012006-04-17 05:28:54 +00005503 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5504 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005505 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00005506
Chris Lattner071ad012006-04-17 05:28:54 +00005507 unsigned PFIndexes[4];
5508 bool isFourElementShuffle = true;
5509 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5510 unsigned EltNo = 8; // Start out undef.
5511 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005512 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00005513 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005514
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005515 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00005516 if ((ByteSource & 3) != j) {
5517 isFourElementShuffle = false;
5518 break;
5519 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005520
Chris Lattner071ad012006-04-17 05:28:54 +00005521 if (EltNo == 8) {
5522 EltNo = ByteSource/4;
5523 } else if (EltNo != ByteSource/4) {
5524 isFourElementShuffle = false;
5525 break;
5526 }
5527 }
5528 PFIndexes[i] = EltNo;
5529 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005530
5531 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00005532 // perfect shuffle vector to determine if it is cost effective to do this as
5533 // discrete instructions, or whether we should use a vperm.
5534 if (isFourElementShuffle) {
5535 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005536 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00005537 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005538
Chris Lattner071ad012006-04-17 05:28:54 +00005539 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5540 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005541
Chris Lattner071ad012006-04-17 05:28:54 +00005542 // Determining when to avoid vperm is tricky. Many things affect the cost
5543 // of vperm, particularly how many times the perm mask needs to be computed.
5544 // For example, if the perm mask can be hoisted out of a loop or is already
5545 // used (perhaps because there are multiple permutes with the same shuffle
5546 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5547 // the loop requires an extra register.
5548 //
5549 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00005550 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00005551 // available, if this block is within a loop, we should avoid using vperm
5552 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005553 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005554 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005555 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005556
Chris Lattner19e90552006-04-14 05:19:18 +00005557 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5558 // vector that will get spilled to the constant pool.
5559 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005560
Chris Lattner19e90552006-04-14 05:19:18 +00005561 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5562 // that it is in input element units, not in bytes. Convert now.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005563 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005564 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005565
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005566 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005567 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5568 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005569
Chris Lattner19e90552006-04-14 05:19:18 +00005570 for (unsigned j = 0; j != BytesPerElement; ++j)
5571 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson9f944592009-08-11 20:47:22 +00005572 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00005573 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005574
Owen Anderson9f944592009-08-11 20:47:22 +00005575 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga49de9d2009-02-25 22:49:59 +00005576 &ResultMask[0], ResultMask.size());
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005577 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00005578}
5579
Chris Lattner9754d142006-04-18 17:59:36 +00005580/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5581/// altivec comparison. If it is, return true and fill in Opc/isDot with
5582/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005583static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner9754d142006-04-18 17:59:36 +00005584 bool &isDot) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005585 unsigned IntrinsicID =
5586 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00005587 CompareOpc = -1;
5588 isDot = false;
5589 switch (IntrinsicID) {
5590 default: return false;
5591 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00005592 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5593 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5594 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5595 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5596 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5597 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5598 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5599 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5600 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5601 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5602 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5603 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5604 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005605
Chris Lattner4211ca92006-04-14 06:01:58 +00005606 // Normal Comparisons.
5607 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5608 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5609 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5610 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5611 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5612 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5613 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5614 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5615 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5616 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5617 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5618 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5619 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5620 }
Chris Lattner9754d142006-04-18 17:59:36 +00005621 return true;
5622}
5623
5624/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5625/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005626SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005627 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00005628 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5629 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005630 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00005631 int CompareOpc;
5632 bool isDot;
5633 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005634 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005635
Chris Lattner9754d142006-04-18 17:59:36 +00005636 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00005637 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00005638 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00005639 Op.getOperand(1), Op.getOperand(2),
5640 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00005641 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00005642 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005643
Chris Lattner4211ca92006-04-14 06:01:58 +00005644 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005645 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005646 Op.getOperand(2), // LHS
5647 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00005648 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005649 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005650 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00005651 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005652
Chris Lattner4211ca92006-04-14 06:01:58 +00005653 // Now that we have the comparison, emit a copy from the CR to a GPR.
5654 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00005655 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00005656 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00005657 CompNode.getValue(1));
5658
Chris Lattner4211ca92006-04-14 06:01:58 +00005659 // Unpack the result based on how the target uses it.
5660 unsigned BitNo; // Bit # of CR6.
5661 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00005662 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00005663 default: // Can't happen, don't crash on invalid number though.
5664 case 0: // Return the value of the EQ bit of CR6.
5665 BitNo = 0; InvertBit = false;
5666 break;
5667 case 1: // Return the inverted value of the EQ bit of CR6.
5668 BitNo = 0; InvertBit = true;
5669 break;
5670 case 2: // Return the value of the LT bit of CR6.
5671 BitNo = 2; InvertBit = false;
5672 break;
5673 case 3: // Return the inverted value of the LT bit of CR6.
5674 BitNo = 2; InvertBit = true;
5675 break;
5676 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005677
Chris Lattner4211ca92006-04-14 06:01:58 +00005678 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00005679 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5680 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005681 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00005682 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5683 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00005684
Chris Lattner4211ca92006-04-14 06:01:58 +00005685 // If we are supposed to, toggle the bit.
5686 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00005687 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5688 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005689 return Flags;
5690}
5691
Scott Michelcf0da6c2009-02-17 22:15:04 +00005692SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005693 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005694 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00005695 // Create a stack slot that is 16-byte aligned.
5696 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00005697 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00005698 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005699 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005700
Chris Lattner4211ca92006-04-14 06:01:58 +00005701 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00005702 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00005703 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005704 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005705 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00005706 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005707 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005708}
5709
Dan Gohman21cea8a2010-04-17 15:26:15 +00005710SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005711 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005712 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005713 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005714
Owen Anderson9f944592009-08-11 20:47:22 +00005715 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
5716 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005717
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005718 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005719 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005720
Chris Lattner7e4398742006-04-18 03:43:48 +00005721 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00005722 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
5723 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
5724 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005725
Chris Lattner7e4398742006-04-18 03:43:48 +00005726 // Low parts multiplied together, generating 32-bit results (we ignore the
5727 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005728 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00005729 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005730
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005731 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00005732 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00005733 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005734 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005735 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005736 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
5737 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005738 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005739
Owen Anderson9f944592009-08-11 20:47:22 +00005740 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00005741
Chris Lattner96d50482006-04-18 04:28:57 +00005742 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005743 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005744 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005745 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005746
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005747 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005748 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00005749 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005750 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005751
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005752 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005753 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00005754 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005755 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005756
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005757 // Merge the results together.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005758 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005759 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005760 Ops[i*2 ] = 2*i+1;
5761 Ops[i*2+1] = 2*i+1+16;
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005762 }
Owen Anderson9f944592009-08-11 20:47:22 +00005763 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00005764 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005765 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00005766 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005767}
5768
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005769/// LowerOperation - Provide custom lowering hooks for some operations.
5770///
Dan Gohman21cea8a2010-04-17 15:26:15 +00005771SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005772 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005773 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005774 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00005775 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005776 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00005777 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00005778 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005779 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00005780 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
5781 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005782 case ISD::VASTART:
Dan Gohman31ae5862010-04-17 14:41:14 +00005783 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005784
5785 case ISD::VAARG:
Dan Gohman31ae5862010-04-17 14:41:14 +00005786 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00005787
Roman Divackyc3825df2013-07-25 21:36:47 +00005788 case ISD::VACOPY:
5789 return LowerVACOPY(Op, DAG, PPCSubTarget);
5790
Jim Laskeye4f4d042006-12-04 22:04:42 +00005791 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00005792 case ISD::DYNAMIC_STACKALLOC:
5793 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng51096af2008-04-19 01:30:48 +00005794
Hal Finkel756810f2013-03-21 21:37:52 +00005795 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
5796 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
5797
Chris Lattner4211ca92006-04-14 06:01:58 +00005798 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005799 case ISD::FP_TO_UINT:
5800 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005801 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00005802 case ISD::UINT_TO_FP:
5803 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00005804 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00005805
Chris Lattner4211ca92006-04-14 06:01:58 +00005806 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00005807 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
5808 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
5809 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00005810
Chris Lattner4211ca92006-04-14 06:01:58 +00005811 // Vector-related lowering.
5812 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5813 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5814 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5815 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005816 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005817
Hal Finkel25c19922013-05-15 21:37:41 +00005818 // For counter-based loop handling.
5819 case ISD::INTRINSIC_W_CHAIN: return SDValue();
5820
Chris Lattnerf6a81562007-12-08 06:59:59 +00005821 // Frame & Return address.
5822 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00005823 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00005824 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005825}
5826
Duncan Sands6ed40142008-12-01 11:39:25 +00005827void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
5828 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005829 SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00005830 const TargetMachine &TM = getTargetMachine();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005831 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00005832 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00005833 default:
Craig Toppere55c5562012-02-07 02:50:20 +00005834 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkel25c19922013-05-15 21:37:41 +00005835 case ISD::INTRINSIC_W_CHAIN: {
5836 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
5837 Intrinsic::ppc_is_decremented_ctr_nonzero)
5838 break;
5839
5840 assert(N->getValueType(0) == MVT::i1 &&
5841 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00005842 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00005843 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
5844 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
5845 N->getOperand(1));
5846
5847 Results.push_back(NewInt);
5848 Results.push_back(NewInt.getValue(1));
5849 break;
5850 }
Roman Divacky4394e682011-06-28 15:30:42 +00005851 case ISD::VAARG: {
5852 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
5853 || TM.getSubtarget<PPCSubtarget>().isPPC64())
5854 return;
5855
5856 EVT VT = N->getValueType(0);
5857
5858 if (VT == MVT::i64) {
5859 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
5860
5861 Results.push_back(NewNode);
5862 Results.push_back(NewNode.getValue(1));
5863 }
5864 return;
5865 }
Duncan Sands6ed40142008-12-01 11:39:25 +00005866 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00005867 assert(N->getValueType(0) == MVT::ppcf128);
5868 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005869 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005870 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00005871 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00005872 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005873 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00005874 DAG.getIntPtrConstant(1));
5875
Ulrich Weigand874fc622013-03-26 10:56:22 +00005876 // Add the two halves of the long double in round-to-zero mode.
5877 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00005878
5879 // We know the low half is about to be thrown away, so just use something
5880 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00005881 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00005882 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00005883 return;
Duncan Sands2a287912008-07-19 16:26:02 +00005884 }
Duncan Sands6ed40142008-12-01 11:39:25 +00005885 case ISD::FP_TO_SINT:
Bill Schmidt41221692013-07-09 18:50:20 +00005886 // LowerFP_TO_INT() can only handle f32 and f64.
5887 if (N->getOperand(0).getValueType() == MVT::ppcf128)
5888 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005889 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00005890 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00005891 }
5892}
5893
5894
Chris Lattner4211ca92006-04-14 06:01:58 +00005895//===----------------------------------------------------------------------===//
5896// Other Lowering Code
5897//===----------------------------------------------------------------------===//
5898
Chris Lattner9b577f12005-08-26 21:23:58 +00005899MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00005900PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman747e55b2009-02-07 16:15:20 +00005901 bool is64bit, unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005902 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesend4eb0522008-08-25 22:34:37 +00005903 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5904
5905 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5906 MachineFunction *F = BB->getParent();
5907 MachineFunction::iterator It = BB;
5908 ++It;
5909
5910 unsigned dest = MI->getOperand(0).getReg();
5911 unsigned ptrA = MI->getOperand(1).getReg();
5912 unsigned ptrB = MI->getOperand(2).getReg();
5913 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00005914 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00005915
5916 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5917 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5918 F->insert(It, loopMBB);
5919 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00005920 exitMBB->splice(exitMBB->begin(), BB,
5921 llvm::next(MachineBasicBlock::iterator(MI)),
5922 BB->end());
5923 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00005924
5925 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005926 unsigned TmpReg = (!BinOpcode) ? incr :
5927 RegInfo.createVirtualRegister(
Dale Johannesenbc698292008-09-02 20:30:23 +00005928 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5929 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00005930
5931 // thisMBB:
5932 // ...
5933 // fallthrough --> loopMBB
5934 BB->addSuccessor(loopMBB);
5935
5936 // loopMBB:
5937 // l[wd]arx dest, ptr
5938 // add r0, dest, incr
5939 // st[wd]cx. r0, ptr
5940 // bne- loopMBB
5941 // fallthrough --> exitMBB
5942 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00005943 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00005944 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005945 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00005946 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
5947 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesend4eb0522008-08-25 22:34:37 +00005948 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00005949 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00005950 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00005951 BB->addSuccessor(loopMBB);
5952 BB->addSuccessor(exitMBB);
5953
5954 // exitMBB:
5955 // ...
5956 BB = exitMBB;
5957 return BB;
5958}
5959
5960MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00005961PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00005962 MachineBasicBlock *BB,
5963 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00005964 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005965 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesena32affb2008-08-28 17:53:09 +00005966 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5967 // In 64 bit mode we have to use 64 bits for addresses, even though the
5968 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
5969 // registers without caring whether they're 32 or 64, but here we're
5970 // doing actual arithmetic on the addresses.
5971 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00005972 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00005973
5974 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5975 MachineFunction *F = BB->getParent();
5976 MachineFunction::iterator It = BB;
5977 ++It;
5978
5979 unsigned dest = MI->getOperand(0).getReg();
5980 unsigned ptrA = MI->getOperand(1).getReg();
5981 unsigned ptrB = MI->getOperand(2).getReg();
5982 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00005983 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00005984
5985 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5986 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5987 F->insert(It, loopMBB);
5988 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00005989 exitMBB->splice(exitMBB->begin(), BB,
5990 llvm::next(MachineBasicBlock::iterator(MI)),
5991 BB->end());
5992 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00005993
5994 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005995 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00005996 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5997 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00005998 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
5999 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6000 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6001 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
6002 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6003 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6004 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6005 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6006 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
6007 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006008 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006009 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006010 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006011
6012 // thisMBB:
6013 // ...
6014 // fallthrough --> loopMBB
6015 BB->addSuccessor(loopMBB);
6016
6017 // The 4-byte load must be aligned, while a char or short may be
6018 // anywhere in the word. Hence all this nasty bookkeeping code.
6019 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6020 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006021 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00006022 // rlwinm ptr, ptr1, 0, 0, 29
6023 // slw incr2, incr, shift
6024 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6025 // slw mask, mask2, shift
6026 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00006027 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006028 // add tmp, tmpDest, incr2
6029 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00006030 // and tmp3, tmp, mask
6031 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00006032 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00006033 // bne- loopMBB
6034 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006035 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006036 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00006037 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006038 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006039 .addReg(ptrA).addReg(ptrB);
6040 } else {
6041 Ptr1Reg = ptrB;
6042 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006043 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006044 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006045 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006046 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6047 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006048 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006049 .addReg(Ptr1Reg).addImm(0).addImm(61);
6050 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006051 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006052 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006053 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006054 .addReg(incr).addReg(ShiftReg);
6055 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006056 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00006057 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006058 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6059 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00006060 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006061 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006062 .addReg(Mask2Reg).addReg(ShiftReg);
6063
6064 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006065 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006066 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006067 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006068 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006069 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006070 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006071 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006072 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006073 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006074 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006075 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00006076 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006077 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006078 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006079 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006080 BB->addSuccessor(loopMBB);
6081 BB->addSuccessor(exitMBB);
6082
6083 // exitMBB:
6084 // ...
6085 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006086 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6087 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00006088 return BB;
6089}
6090
Hal Finkel756810f2013-03-21 21:37:52 +00006091llvm::MachineBasicBlock*
6092PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6093 MachineBasicBlock *MBB) const {
6094 DebugLoc DL = MI->getDebugLoc();
6095 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6096
6097 MachineFunction *MF = MBB->getParent();
6098 MachineRegisterInfo &MRI = MF->getRegInfo();
6099
6100 const BasicBlock *BB = MBB->getBasicBlock();
6101 MachineFunction::iterator I = MBB;
6102 ++I;
6103
6104 // Memory Reference
6105 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6106 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6107
6108 unsigned DstReg = MI->getOperand(0).getReg();
6109 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6110 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6111 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6112 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6113
6114 MVT PVT = getPointerTy();
6115 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6116 "Invalid Pointer Size!");
6117 // For v = setjmp(buf), we generate
6118 //
6119 // thisMBB:
6120 // SjLjSetup mainMBB
6121 // bl mainMBB
6122 // v_restore = 1
6123 // b sinkMBB
6124 //
6125 // mainMBB:
6126 // buf[LabelOffset] = LR
6127 // v_main = 0
6128 //
6129 // sinkMBB:
6130 // v = phi(main, restore)
6131 //
6132
6133 MachineBasicBlock *thisMBB = MBB;
6134 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6135 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6136 MF->insert(I, mainMBB);
6137 MF->insert(I, sinkMBB);
6138
6139 MachineInstrBuilder MIB;
6140
6141 // Transfer the remainder of BB and its successor edges to sinkMBB.
6142 sinkMBB->splice(sinkMBB->begin(), MBB,
6143 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
6144 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6145
6146 // Note that the structure of the jmp_buf used here is not compatible
6147 // with that used by libc, and is not designed to be. Specifically, it
6148 // stores only those 'reserved' registers that LLVM does not otherwise
6149 // understand how to spill. Also, by convention, by the time this
6150 // intrinsic is called, Clang has already stored the frame address in the
6151 // first slot of the buffer and stack address in the third. Following the
6152 // X86 target code, we'll store the jump address in the second slot. We also
6153 // need to save the TOC pointer (R2) to handle jumps between shared
6154 // libraries, and that will be stored in the fourth slot. The thread
6155 // identifier (R13) is not affected.
6156
6157 // thisMBB:
6158 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6159 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006160 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006161
6162 // Prepare IP either in reg.
6163 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6164 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6165 unsigned BufReg = MI->getOperand(1).getReg();
6166
6167 if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) {
6168 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6169 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006170 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006171 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006172 MIB.setMemRefs(MMOBegin, MMOEnd);
6173 }
6174
Hal Finkelf05d6c72013-07-17 23:50:51 +00006175 // Naked functions never have a base pointer, and so we use r1. For all
6176 // other functions, this decision must be delayed until during PEI.
6177 unsigned BaseReg;
6178 if (MF->getFunction()->getAttributes().hasAttribute(
6179 AttributeSet::FunctionIndex, Attribute::Naked))
6180 BaseReg = PPCSubTarget.isPPC64() ? PPC::X1 : PPC::R1;
6181 else
6182 BaseReg = PPCSubTarget.isPPC64() ? PPC::BP8 : PPC::BP;
6183
6184 MIB = BuildMI(*thisMBB, MI, DL,
6185 TII->get(PPCSubTarget.isPPC64() ? PPC::STD : PPC::STW))
6186 .addReg(BaseReg)
6187 .addImm(BPOffset)
6188 .addReg(BufReg);
6189 MIB.setMemRefs(MMOBegin, MMOEnd);
6190
Hal Finkel756810f2013-03-21 21:37:52 +00006191 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00006192 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling5e7656b2013-06-07 07:55:53 +00006193 const PPCRegisterInfo *TRI =
6194 static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo());
6195 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00006196
6197 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6198
6199 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6200 .addMBB(mainMBB);
6201 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6202
6203 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6204 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6205
6206 // mainMBB:
6207 // mainDstReg = 0
6208 MIB = BuildMI(mainMBB, DL,
6209 TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
6210
6211 // Store IP
6212 if (PPCSubTarget.isPPC64()) {
6213 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6214 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006215 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006216 .addReg(BufReg);
6217 } else {
6218 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6219 .addReg(LabelReg)
6220 .addImm(LabelOffset)
6221 .addReg(BufReg);
6222 }
6223
6224 MIB.setMemRefs(MMOBegin, MMOEnd);
6225
6226 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6227 mainMBB->addSuccessor(sinkMBB);
6228
6229 // sinkMBB:
6230 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6231 TII->get(PPC::PHI), DstReg)
6232 .addReg(mainDstReg).addMBB(mainMBB)
6233 .addReg(restoreDstReg).addMBB(thisMBB);
6234
6235 MI->eraseFromParent();
6236 return sinkMBB;
6237}
6238
6239MachineBasicBlock *
6240PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6241 MachineBasicBlock *MBB) const {
6242 DebugLoc DL = MI->getDebugLoc();
6243 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6244
6245 MachineFunction *MF = MBB->getParent();
6246 MachineRegisterInfo &MRI = MF->getRegInfo();
6247
6248 // Memory Reference
6249 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6250 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6251
6252 MVT PVT = getPointerTy();
6253 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6254 "Invalid Pointer Size!");
6255
6256 const TargetRegisterClass *RC =
6257 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6258 unsigned Tmp = MRI.createVirtualRegister(RC);
6259 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6260 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6261 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006262 unsigned BP = (PVT == MVT::i64) ? PPC::X30 : PPC::R30;
Hal Finkel756810f2013-03-21 21:37:52 +00006263
6264 MachineInstrBuilder MIB;
6265
6266 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6267 const int64_t SPOffset = 2 * PVT.getStoreSize();
6268 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006269 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006270
6271 unsigned BufReg = MI->getOperand(0).getReg();
6272
6273 // Reload FP (the jumped-to function may not have had a
6274 // frame pointer, and if so, then its r31 will be restored
6275 // as necessary).
6276 if (PVT == MVT::i64) {
6277 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6278 .addImm(0)
6279 .addReg(BufReg);
6280 } else {
6281 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6282 .addImm(0)
6283 .addReg(BufReg);
6284 }
6285 MIB.setMemRefs(MMOBegin, MMOEnd);
6286
6287 // Reload IP
6288 if (PVT == MVT::i64) {
6289 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006290 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006291 .addReg(BufReg);
6292 } else {
6293 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6294 .addImm(LabelOffset)
6295 .addReg(BufReg);
6296 }
6297 MIB.setMemRefs(MMOBegin, MMOEnd);
6298
6299 // Reload SP
6300 if (PVT == MVT::i64) {
6301 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006302 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006303 .addReg(BufReg);
6304 } else {
6305 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6306 .addImm(SPOffset)
6307 .addReg(BufReg);
6308 }
6309 MIB.setMemRefs(MMOBegin, MMOEnd);
6310
Hal Finkelf05d6c72013-07-17 23:50:51 +00006311 // Reload BP
6312 if (PVT == MVT::i64) {
6313 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
6314 .addImm(BPOffset)
6315 .addReg(BufReg);
6316 } else {
6317 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
6318 .addImm(BPOffset)
6319 .addReg(BufReg);
6320 }
6321 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00006322
6323 // Reload TOC
6324 if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) {
6325 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006326 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006327 .addReg(BufReg);
6328
6329 MIB.setMemRefs(MMOBegin, MMOEnd);
6330 }
6331
6332 // Jump
6333 BuildMI(*MBB, MI, DL,
6334 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6335 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6336
6337 MI->eraseFromParent();
6338 return MBB;
6339}
6340
Dale Johannesena32affb2008-08-28 17:53:09 +00006341MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00006342PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00006343 MachineBasicBlock *BB) const {
Hal Finkel756810f2013-03-21 21:37:52 +00006344 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6345 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6346 return emitEHSjLjSetJmp(MI, BB);
6347 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6348 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6349 return emitEHSjLjLongJmp(MI, BB);
6350 }
6351
Evan Cheng20350c42006-11-27 23:37:22 +00006352 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00006353
6354 // To "insert" these instructions we actually have to insert their
6355 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00006356 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00006357 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00006358 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00006359
Dan Gohman3b460302008-07-07 23:14:23 +00006360 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00006361
Hal Finkel460e94d2012-06-22 23:10:08 +00006362 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6363 MI->getOpcode() == PPC::SELECT_CC_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00006364 SmallVector<MachineOperand, 2> Cond;
6365 Cond.push_back(MI->getOperand(4));
6366 Cond.push_back(MI->getOperand(1));
6367
Hal Finkel460e94d2012-06-22 23:10:08 +00006368 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00006369 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6370 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
6371 Cond, MI->getOperand(2).getReg(),
6372 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00006373 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6374 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6375 MI->getOpcode() == PPC::SELECT_CC_F4 ||
6376 MI->getOpcode() == PPC::SELECT_CC_F8 ||
6377 MI->getOpcode() == PPC::SELECT_CC_VRRC) {
6378
Evan Cheng32e376f2008-07-12 02:23:19 +00006379
6380 // The incoming instruction knows the destination vreg to set, the
6381 // condition code register to branch on, the true/false values to
6382 // select between, and a branch opcode to use.
6383
6384 // thisMBB:
6385 // ...
6386 // TrueVal = ...
6387 // cmpTY ccX, r1, r2
6388 // bCC copy1MBB
6389 // fallthrough --> copy0MBB
6390 MachineBasicBlock *thisMBB = BB;
6391 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6392 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
6393 unsigned SelectPred = MI->getOperand(4).getImm();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006394 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006395 F->insert(It, copy0MBB);
6396 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006397
6398 // Transfer the remainder of BB and its successor edges to sinkMBB.
6399 sinkMBB->splice(sinkMBB->begin(), BB,
6400 llvm::next(MachineBasicBlock::iterator(MI)),
6401 BB->end());
6402 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6403
Evan Cheng32e376f2008-07-12 02:23:19 +00006404 // Next, add the true and fallthrough blocks as its successors.
6405 BB->addSuccessor(copy0MBB);
6406 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006407
Dan Gohman34396292010-07-06 20:24:04 +00006408 BuildMI(BB, dl, TII->get(PPC::BCC))
6409 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6410
Evan Cheng32e376f2008-07-12 02:23:19 +00006411 // copy0MBB:
6412 // %FalseValue = ...
6413 // # fallthrough to sinkMBB
6414 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006415
Evan Cheng32e376f2008-07-12 02:23:19 +00006416 // Update machine-CFG edges
6417 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006418
Evan Cheng32e376f2008-07-12 02:23:19 +00006419 // sinkMBB:
6420 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6421 // ...
6422 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00006423 BuildMI(*BB, BB->begin(), dl,
6424 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00006425 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
6426 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6427 }
Dale Johannesena32affb2008-08-28 17:53:09 +00006428 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
6429 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
6430 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
6431 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006432 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
6433 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
6434 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
6435 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006436
6437 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
6438 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
6439 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
6440 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006441 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
6442 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
6443 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
6444 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006445
6446 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
6447 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
6448 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
6449 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006450 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
6451 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
6452 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
6453 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006454
6455 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
6456 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
6457 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
6458 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006459 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
6460 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
6461 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
6462 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006463
6464 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006465 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006466 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006467 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006468 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006469 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006470 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006471 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006472
6473 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6474 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6475 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6476 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006477 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6478 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6479 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6480 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006481
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006482 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6483 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6484 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6485 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6486 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6487 BB = EmitAtomicBinary(MI, BB, false, 0);
6488 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6489 BB = EmitAtomicBinary(MI, BB, true, 0);
6490
Evan Cheng32e376f2008-07-12 02:23:19 +00006491 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6492 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6493 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6494
6495 unsigned dest = MI->getOperand(0).getReg();
6496 unsigned ptrA = MI->getOperand(1).getReg();
6497 unsigned ptrB = MI->getOperand(2).getReg();
6498 unsigned oldval = MI->getOperand(3).getReg();
6499 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006500 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006501
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006502 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6503 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6504 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006505 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006506 F->insert(It, loop1MBB);
6507 F->insert(It, loop2MBB);
6508 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006509 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006510 exitMBB->splice(exitMBB->begin(), BB,
6511 llvm::next(MachineBasicBlock::iterator(MI)),
6512 BB->end());
6513 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006514
6515 // thisMBB:
6516 // ...
6517 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006518 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006519
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006520 // loop1MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006521 // l[wd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006522 // cmp[wd] dest, oldval
6523 // bne- midMBB
6524 // loop2MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006525 // st[wd]cx. newval, ptr
6526 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006527 // b exitBB
6528 // midMBB:
6529 // st[wd]cx. dest, ptr
6530 // exitBB:
6531 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006532 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00006533 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006534 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00006535 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006536 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006537 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6538 BB->addSuccessor(loop2MBB);
6539 BB->addSuccessor(midMBB);
6540
6541 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006542 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng32e376f2008-07-12 02:23:19 +00006543 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006544 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006545 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006546 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006547 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006548 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006549
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006550 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006551 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006552 .addReg(dest).addReg(ptrA).addReg(ptrB);
6553 BB->addSuccessor(exitMBB);
6554
Evan Cheng32e376f2008-07-12 02:23:19 +00006555 // exitMBB:
6556 // ...
6557 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00006558 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6559 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6560 // We must use 64-bit registers for addresses when targeting 64-bit,
6561 // since we're actually doing arithmetic on them. Other registers
6562 // can be 32-bit.
6563 bool is64bit = PPCSubTarget.isPPC64();
6564 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6565
6566 unsigned dest = MI->getOperand(0).getReg();
6567 unsigned ptrA = MI->getOperand(1).getReg();
6568 unsigned ptrB = MI->getOperand(2).getReg();
6569 unsigned oldval = MI->getOperand(3).getReg();
6570 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006571 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00006572
6573 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6574 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6575 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6576 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6577 F->insert(It, loop1MBB);
6578 F->insert(It, loop2MBB);
6579 F->insert(It, midMBB);
6580 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006581 exitMBB->splice(exitMBB->begin(), BB,
6582 llvm::next(MachineBasicBlock::iterator(MI)),
6583 BB->end());
6584 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006585
6586 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006587 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006588 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6589 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00006590 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6591 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6592 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6593 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6594 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6595 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6596 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6597 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6598 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6599 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6600 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6601 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6602 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6603 unsigned Ptr1Reg;
6604 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00006605 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00006606 // thisMBB:
6607 // ...
6608 // fallthrough --> loopMBB
6609 BB->addSuccessor(loop1MBB);
6610
6611 // The 4-byte load must be aligned, while a char or short may be
6612 // anywhere in the word. Hence all this nasty bookkeeping code.
6613 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6614 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006615 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00006616 // rlwinm ptr, ptr1, 0, 0, 29
6617 // slw newval2, newval, shift
6618 // slw oldval2, oldval,shift
6619 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6620 // slw mask, mask2, shift
6621 // and newval3, newval2, mask
6622 // and oldval3, oldval2, mask
6623 // loop1MBB:
6624 // lwarx tmpDest, ptr
6625 // and tmp, tmpDest, mask
6626 // cmpw tmp, oldval3
6627 // bne- midMBB
6628 // loop2MBB:
6629 // andc tmp2, tmpDest, mask
6630 // or tmp4, tmp2, newval3
6631 // stwcx. tmp4, ptr
6632 // bne- loop1MBB
6633 // b exitBB
6634 // midMBB:
6635 // stwcx. tmpDest, ptr
6636 // exitBB:
6637 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006638 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00006639 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006640 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006641 .addReg(ptrA).addReg(ptrB);
6642 } else {
6643 Ptr1Reg = ptrB;
6644 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006645 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006646 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006647 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006648 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6649 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006650 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006651 .addReg(Ptr1Reg).addImm(0).addImm(61);
6652 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006653 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006654 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006655 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006656 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006657 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006658 .addReg(oldval).addReg(ShiftReg);
6659 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006660 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00006661 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006662 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6663 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6664 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00006665 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006666 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006667 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006668 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006669 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006670 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006671 .addReg(OldVal2Reg).addReg(MaskReg);
6672
6673 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006674 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006675 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006676 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6677 .addReg(TmpDestReg).addReg(MaskReg);
6678 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00006679 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006680 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006681 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6682 BB->addSuccessor(loop2MBB);
6683 BB->addSuccessor(midMBB);
6684
6685 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006686 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6687 .addReg(TmpDestReg).addReg(MaskReg);
6688 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6689 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6690 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006691 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006692 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006693 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006694 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006695 BB->addSuccessor(loop1MBB);
6696 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006697
Dale Johannesen340d2642008-08-30 00:08:53 +00006698 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006699 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006700 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00006701 BB->addSuccessor(exitMBB);
6702
6703 // exitMBB:
6704 // ...
6705 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006706 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
6707 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00006708 } else if (MI->getOpcode() == PPC::FADDrtz) {
6709 // This pseudo performs an FADD with rounding mode temporarily forced
6710 // to round-to-zero. We emit this via custom inserter since the FPSCR
6711 // is not modeled at the SelectionDAG level.
6712 unsigned Dest = MI->getOperand(0).getReg();
6713 unsigned Src1 = MI->getOperand(1).getReg();
6714 unsigned Src2 = MI->getOperand(2).getReg();
6715 DebugLoc dl = MI->getDebugLoc();
6716
6717 MachineRegisterInfo &RegInfo = F->getRegInfo();
6718 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
6719
6720 // Save FPSCR value.
6721 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
6722
6723 // Set rounding mode to round-to-zero.
6724 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
6725 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
6726
6727 // Perform addition.
6728 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
6729
6730 // Restore FPSCR value.
6731 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00006732 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006733 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00006734 }
Chris Lattner9b577f12005-08-26 21:23:58 +00006735
Dan Gohman34396292010-07-06 20:24:04 +00006736 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00006737 return BB;
6738}
6739
Chris Lattner4211ca92006-04-14 06:01:58 +00006740//===----------------------------------------------------------------------===//
6741// Target Optimization Hooks
6742//===----------------------------------------------------------------------===//
6743
Hal Finkelb0c810f2013-04-03 17:44:56 +00006744SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
6745 DAGCombinerInfo &DCI) const {
Hal Finkel2e103312013-04-03 04:01:11 +00006746 if (DCI.isAfterLegalizeVectorOps())
6747 return SDValue();
6748
Hal Finkelb0c810f2013-04-03 17:44:56 +00006749 EVT VT = Op.getValueType();
6750
6751 if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) ||
6752 (VT == MVT::f64 && PPCSubTarget.hasFRE()) ||
6753 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel2e103312013-04-03 04:01:11 +00006754
6755 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6756 // For the reciprocal, we need to find the zero of the function:
6757 // F(X) = A X - 1 [which has a zero at X = 1/A]
6758 // =>
6759 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
6760 // does not require additional intermediate precision]
6761
6762 // Convergence is quadratic, so we essentially double the number of digits
6763 // correct after every iteration. The minimum architected relative
6764 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6765 // 23 digits and double has 52 digits.
6766 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00006767 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00006768 ++Iterations;
6769
6770 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006771 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006772
6773 SDValue FPOne =
Hal Finkelb0c810f2013-04-03 17:44:56 +00006774 DAG.getConstantFP(1.0, VT.getScalarType());
6775 if (VT.isVector()) {
6776 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00006777 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00006778 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel2e103312013-04-03 04:01:11 +00006779 FPOne, FPOne, FPOne, FPOne);
6780 }
6781
Hal Finkelb0c810f2013-04-03 17:44:56 +00006782 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006783 DCI.AddToWorklist(Est.getNode());
6784
6785 // Newton iterations: Est = Est + Est (1 - Arg * Est)
6786 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00006787 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00006788 DCI.AddToWorklist(NewEst.getNode());
6789
Hal Finkelb0c810f2013-04-03 17:44:56 +00006790 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006791 DCI.AddToWorklist(NewEst.getNode());
6792
Hal Finkelb0c810f2013-04-03 17:44:56 +00006793 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006794 DCI.AddToWorklist(NewEst.getNode());
6795
Hal Finkelb0c810f2013-04-03 17:44:56 +00006796 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006797 DCI.AddToWorklist(Est.getNode());
6798 }
6799
6800 return Est;
6801 }
6802
6803 return SDValue();
6804}
6805
Hal Finkelb0c810f2013-04-03 17:44:56 +00006806SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel2e103312013-04-03 04:01:11 +00006807 DAGCombinerInfo &DCI) const {
6808 if (DCI.isAfterLegalizeVectorOps())
6809 return SDValue();
6810
Hal Finkelb0c810f2013-04-03 17:44:56 +00006811 EVT VT = Op.getValueType();
6812
6813 if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) ||
6814 (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) ||
6815 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel2e103312013-04-03 04:01:11 +00006816
6817 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6818 // For the reciprocal sqrt, we need to find the zero of the function:
6819 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
6820 // =>
6821 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
6822 // As a result, we precompute A/2 prior to the iteration loop.
6823
6824 // Convergence is quadratic, so we essentially double the number of digits
6825 // correct after every iteration. The minimum architected relative
6826 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6827 // 23 digits and double has 52 digits.
6828 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00006829 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00006830 ++Iterations;
6831
6832 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006833 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006834
Hal Finkelb0c810f2013-04-03 17:44:56 +00006835 SDValue FPThreeHalves =
6836 DAG.getConstantFP(1.5, VT.getScalarType());
6837 if (VT.isVector()) {
6838 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00006839 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00006840 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
6841 FPThreeHalves, FPThreeHalves,
6842 FPThreeHalves, FPThreeHalves);
Hal Finkel2e103312013-04-03 04:01:11 +00006843 }
6844
Hal Finkelb0c810f2013-04-03 17:44:56 +00006845 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006846 DCI.AddToWorklist(Est.getNode());
6847
6848 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
6849 // this entire sequence requires only one FP constant.
Hal Finkelb0c810f2013-04-03 17:44:56 +00006850 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006851 DCI.AddToWorklist(HalfArg.getNode());
6852
Hal Finkelb0c810f2013-04-03 17:44:56 +00006853 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006854 DCI.AddToWorklist(HalfArg.getNode());
6855
6856 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
6857 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00006858 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00006859 DCI.AddToWorklist(NewEst.getNode());
6860
Hal Finkelb0c810f2013-04-03 17:44:56 +00006861 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006862 DCI.AddToWorklist(NewEst.getNode());
6863
Hal Finkelb0c810f2013-04-03 17:44:56 +00006864 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006865 DCI.AddToWorklist(NewEst.getNode());
6866
Hal Finkelb0c810f2013-04-03 17:44:56 +00006867 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006868 DCI.AddToWorklist(Est.getNode());
6869 }
6870
6871 return Est;
6872 }
6873
6874 return SDValue();
6875}
6876
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00006877// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
6878// not enforce equality of the chain operands.
6879static bool isConsecutiveLS(LSBaseSDNode *LS, LSBaseSDNode *Base,
6880 unsigned Bytes, int Dist,
6881 SelectionDAG &DAG) {
6882 EVT VT = LS->getMemoryVT();
6883 if (VT.getSizeInBits() / 8 != Bytes)
6884 return false;
6885
6886 SDValue Loc = LS->getBasePtr();
6887 SDValue BaseLoc = Base->getBasePtr();
6888 if (Loc.getOpcode() == ISD::FrameIndex) {
6889 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6890 return false;
6891 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6892 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6893 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6894 int FS = MFI->getObjectSize(FI);
6895 int BFS = MFI->getObjectSize(BFI);
6896 if (FS != BFS || FS != (int)Bytes) return false;
6897 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6898 }
6899
6900 // Handle X+C
6901 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6902 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6903 return true;
6904
6905 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6906 const GlobalValue *GV1 = NULL;
6907 const GlobalValue *GV2 = NULL;
6908 int64_t Offset1 = 0;
6909 int64_t Offset2 = 0;
6910 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6911 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6912 if (isGA1 && isGA2 && GV1 == GV2)
6913 return Offset1 == (Offset2 + Dist*Bytes);
6914 return false;
6915}
6916
Hal Finkel7d8a6912013-05-26 18:08:30 +00006917// Return true is there is a nearyby consecutive load to the one provided
6918// (regardless of alignment). We search up and down the chain, looking though
6919// token factors and other loads (but nothing else). As a result, a true
6920// results indicates that it is safe to create a new consecutive load adjacent
6921// to the load provided.
6922static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
6923 SDValue Chain = LD->getChain();
6924 EVT VT = LD->getMemoryVT();
6925
6926 SmallSet<SDNode *, 16> LoadRoots;
6927 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
6928 SmallSet<SDNode *, 16> Visited;
6929
6930 // First, search up the chain, branching to follow all token-factor operands.
6931 // If we find a consecutive load, then we're done, otherwise, record all
6932 // nodes just above the top-level loads and token factors.
6933 while (!Queue.empty()) {
6934 SDNode *ChainNext = Queue.pop_back_val();
6935 if (!Visited.insert(ChainNext))
6936 continue;
6937
6938 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00006939 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00006940 return true;
6941
6942 if (!Visited.count(ChainLD->getChain().getNode()))
6943 Queue.push_back(ChainLD->getChain().getNode());
6944 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
6945 for (SDNode::op_iterator O = ChainNext->op_begin(),
6946 OE = ChainNext->op_end(); O != OE; ++O)
6947 if (!Visited.count(O->getNode()))
6948 Queue.push_back(O->getNode());
6949 } else
6950 LoadRoots.insert(ChainNext);
6951 }
6952
6953 // Second, search down the chain, starting from the top-level nodes recorded
6954 // in the first phase. These top-level nodes are the nodes just above all
6955 // loads and token factors. Starting with their uses, recursively look though
6956 // all loads (just the chain uses) and token factors to find a consecutive
6957 // load.
6958 Visited.clear();
6959 Queue.clear();
6960
6961 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
6962 IE = LoadRoots.end(); I != IE; ++I) {
6963 Queue.push_back(*I);
6964
6965 while (!Queue.empty()) {
6966 SDNode *LoadRoot = Queue.pop_back_val();
6967 if (!Visited.insert(LoadRoot))
6968 continue;
6969
6970 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00006971 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00006972 return true;
6973
6974 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
6975 UE = LoadRoot->use_end(); UI != UE; ++UI)
6976 if (((isa<LoadSDNode>(*UI) &&
6977 cast<LoadSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
6978 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
6979 Queue.push_back(*UI);
6980 }
6981 }
6982
6983 return false;
6984}
6985
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006986SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
6987 DAGCombinerInfo &DCI) const {
Dan Gohman57c732b2010-04-21 01:34:56 +00006988 const TargetMachine &TM = getTargetMachine();
Chris Lattnerf4184352006-03-01 04:57:39 +00006989 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006990 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00006991 switch (N->getOpcode()) {
6992 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00006993 case PPCISD::SHL:
6994 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00006995 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00006996 return N->getOperand(0);
6997 }
6998 break;
6999 case PPCISD::SRL:
7000 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007001 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007002 return N->getOperand(0);
7003 }
7004 break;
7005 case PPCISD::SRA:
7006 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007007 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007008 C->isAllOnesValue()) // -1 >>s V -> -1.
7009 return N->getOperand(0);
7010 }
7011 break;
Hal Finkel2e103312013-04-03 04:01:11 +00007012 case ISD::FDIV: {
7013 assert(TM.Options.UnsafeFPMath &&
7014 "Reciprocal estimates require UnsafeFPMath");
Scott Michelcf0da6c2009-02-17 22:15:04 +00007015
Hal Finkel2e103312013-04-03 04:01:11 +00007016 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007017 SDValue RV =
7018 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007019 if (RV.getNode() != 0) {
7020 DCI.AddToWorklist(RV.getNode());
7021 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7022 N->getOperand(0), RV);
7023 }
Hal Finkelf96c18e2013-04-04 22:44:12 +00007024 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
7025 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7026 SDValue RV =
7027 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7028 DCI);
7029 if (RV.getNode() != 0) {
7030 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007031 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007032 N->getValueType(0), RV);
7033 DCI.AddToWorklist(RV.getNode());
7034 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7035 N->getOperand(0), RV);
7036 }
7037 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
7038 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7039 SDValue RV =
7040 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7041 DCI);
7042 if (RV.getNode() != 0) {
7043 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007044 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007045 N->getValueType(0), RV,
7046 N->getOperand(1).getOperand(1));
7047 DCI.AddToWorklist(RV.getNode());
7048 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7049 N->getOperand(0), RV);
7050 }
Hal Finkel2e103312013-04-03 04:01:11 +00007051 }
7052
Hal Finkelb0c810f2013-04-03 17:44:56 +00007053 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007054 if (RV.getNode() != 0) {
7055 DCI.AddToWorklist(RV.getNode());
7056 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7057 N->getOperand(0), RV);
7058 }
7059
7060 }
7061 break;
7062 case ISD::FSQRT: {
7063 assert(TM.Options.UnsafeFPMath &&
7064 "Reciprocal estimates require UnsafeFPMath");
7065
7066 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
7067 // reciprocal sqrt.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007068 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007069 if (RV.getNode() != 0) {
7070 DCI.AddToWorklist(RV.getNode());
Hal Finkelb0c810f2013-04-03 17:44:56 +00007071 RV = DAGCombineFastRecip(RV, DCI);
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007072 if (RV.getNode() != 0) {
7073 // Unfortunately, RV is now NaN if the input was exactly 0. Select out
7074 // this case and force the answer to 0.
7075
7076 EVT VT = RV.getValueType();
7077
7078 SDValue Zero = DAG.getConstantFP(0.0, VT.getScalarType());
7079 if (VT.isVector()) {
7080 assert(VT.getVectorNumElements() == 4 && "Unknown vector type");
7081 Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Zero, Zero, Zero, Zero);
7082 }
7083
7084 SDValue ZeroCmp =
7085 DAG.getSetCC(dl, getSetCCResultType(*DAG.getContext(), VT),
7086 N->getOperand(0), Zero, ISD::SETEQ);
7087 DCI.AddToWorklist(ZeroCmp.getNode());
7088 DCI.AddToWorklist(RV.getNode());
7089
7090 RV = DAG.getNode(VT.isVector() ? ISD::VSELECT : ISD::SELECT, dl, VT,
7091 ZeroCmp, Zero, RV);
Hal Finkel2e103312013-04-03 04:01:11 +00007092 return RV;
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007093 }
Hal Finkel2e103312013-04-03 04:01:11 +00007094 }
7095
7096 }
7097 break;
Chris Lattnerf4184352006-03-01 04:57:39 +00007098 case ISD::SINT_TO_FP:
Chris Lattnera35f3062006-06-16 17:34:12 +00007099 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007100 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
7101 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
7102 // We allow the src/dst to be either f32/f64, but the intermediate
7103 // type must be i64.
Owen Anderson9f944592009-08-11 20:47:22 +00007104 if (N->getOperand(0).getValueType() == MVT::i64 &&
7105 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007106 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007107 if (Val.getValueType() == MVT::f32) {
7108 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007109 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007110 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007111
Owen Anderson9f944592009-08-11 20:47:22 +00007112 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007113 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007114 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007115 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007116 if (N->getValueType(0) == MVT::f32) {
7117 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner72733e52008-01-17 07:00:52 +00007118 DAG.getIntPtrConstant(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007119 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007120 }
7121 return Val;
Owen Anderson9f944592009-08-11 20:47:22 +00007122 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007123 // If the intermediate type is i32, we can avoid the load/store here
7124 // too.
Chris Lattnerf4184352006-03-01 04:57:39 +00007125 }
Chris Lattnerf4184352006-03-01 04:57:39 +00007126 }
7127 }
7128 break;
Chris Lattner27f53452006-03-01 05:50:56 +00007129 case ISD::STORE:
7130 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
7131 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnerf5b46f72008-01-18 16:54:56 +00007132 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00007133 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00007134 N->getOperand(1).getValueType() == MVT::i32 &&
7135 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007136 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007137 if (Val.getValueType() == MVT::f32) {
7138 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007139 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007140 }
Owen Anderson9f944592009-08-11 20:47:22 +00007141 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007142 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007143
Hal Finkel60c75102013-04-01 15:37:53 +00007144 SDValue Ops[] = {
7145 N->getOperand(0), Val, N->getOperand(2),
7146 DAG.getValueType(N->getOperand(1).getValueType())
7147 };
7148
7149 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
7150 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
7151 cast<StoreSDNode>(N)->getMemoryVT(),
7152 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00007153 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007154 return Val;
7155 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007156
Chris Lattnera7976d32006-07-10 20:56:58 +00007157 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00007158 if (cast<StoreSDNode>(N)->isUnindexed() &&
7159 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00007160 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00007161 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00007162 N->getOperand(1).getValueType() == MVT::i16 ||
7163 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00007164 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007165 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007166 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00007167 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00007168 if (BSwapOp.getValueType() == MVT::i16)
7169 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00007170
Dan Gohman48b185d2009-09-25 20:36:54 +00007171 SDValue Ops[] = {
7172 N->getOperand(0), BSwapOp, N->getOperand(2),
7173 DAG.getValueType(N->getOperand(1).getValueType())
7174 };
7175 return
7176 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
7177 Ops, array_lengthof(Ops),
7178 cast<StoreSDNode>(N)->getMemoryVT(),
7179 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00007180 }
7181 break;
Hal Finkelcf2e9082013-05-24 23:00:14 +00007182 case ISD::LOAD: {
7183 LoadSDNode *LD = cast<LoadSDNode>(N);
7184 EVT VT = LD->getValueType(0);
7185 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
7186 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
7187 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
7188 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
Hal Finkel40c34782013-09-15 22:09:58 +00007189 (VT == MVT::v16i8 || VT == MVT::v8i16 ||
7190 VT == MVT::v4i32 || VT == MVT::v4f32) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +00007191 LD->getAlignment() < ABIAlignment) {
7192 // This is a type-legal unaligned Altivec load.
7193 SDValue Chain = LD->getChain();
7194 SDValue Ptr = LD->getBasePtr();
7195
7196 // This implements the loading of unaligned vectors as described in
7197 // the venerable Apple Velocity Engine overview. Specifically:
7198 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
7199 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
7200 //
7201 // The general idea is to expand a sequence of one or more unaligned
7202 // loads into a alignment-based permutation-control instruction (lvsl),
7203 // a series of regular vector loads (which always truncate their
7204 // input address to an aligned address), and a series of permutations.
7205 // The results of these permutations are the requested loaded values.
7206 // The trick is that the last "extra" load is not taken from the address
7207 // you might suspect (sizeof(vector) bytes after the last requested
7208 // load), but rather sizeof(vector) - 1 bytes after the last
7209 // requested vector. The point of this is to avoid a page fault if the
7210 // base address happend to be aligned. This works because if the base
7211 // address is aligned, then adding less than a full vector length will
7212 // cause the last vector in the sequence to be (re)loaded. Otherwise,
7213 // the next vector will be fetched as you might suspect was necessary.
7214
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007215 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00007216 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007217 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
7218 // optimization later.
Hal Finkelcf2e9082013-05-24 23:00:14 +00007219 SDValue PermCntl = BuildIntrinsicOp(Intrinsic::ppc_altivec_lvsl, Ptr,
7220 DAG, dl, MVT::v16i8);
7221
7222 // Refine the alignment of the original load (a "new" load created here
7223 // which was identical to the first except for the alignment would be
7224 // merged with the existing node regardless).
7225 MachineFunction &MF = DAG.getMachineFunction();
7226 MachineMemOperand *MMO =
7227 MF.getMachineMemOperand(LD->getPointerInfo(),
7228 LD->getMemOperand()->getFlags(),
7229 LD->getMemoryVT().getStoreSize(),
7230 ABIAlignment);
7231 LD->refineAlignment(MMO);
7232 SDValue BaseLoad = SDValue(LD, 0);
7233
7234 // Note that the value of IncOffset (which is provided to the next
7235 // load's pointer info offset value, and thus used to calculate the
7236 // alignment), and the value of IncValue (which is actually used to
7237 // increment the pointer value) are different! This is because we
7238 // require the next load to appear to be aligned, even though it
7239 // is actually offset from the base pointer by a lesser amount.
7240 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +00007241 int IncValue = IncOffset;
7242
7243 // Walk (both up and down) the chain looking for another load at the real
7244 // (aligned) offset (the alignment of the other load does not matter in
7245 // this case). If found, then do not use the offset reduction trick, as
7246 // that will prevent the loads from being later combined (as they would
7247 // otherwise be duplicates).
7248 if (!findConsecutiveLoad(LD, DAG))
7249 --IncValue;
7250
Hal Finkelcf2e9082013-05-24 23:00:14 +00007251 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
7252 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
7253
Hal Finkelcf2e9082013-05-24 23:00:14 +00007254 SDValue ExtraLoad =
7255 DAG.getLoad(VT, dl, Chain, Ptr,
7256 LD->getPointerInfo().getWithOffset(IncOffset),
7257 LD->isVolatile(), LD->isNonTemporal(),
7258 LD->isInvariant(), ABIAlignment);
7259
7260 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7261 BaseLoad.getValue(1), ExtraLoad.getValue(1));
7262
7263 if (BaseLoad.getValueType() != MVT::v4i32)
7264 BaseLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, BaseLoad);
7265
7266 if (ExtraLoad.getValueType() != MVT::v4i32)
7267 ExtraLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ExtraLoad);
7268
7269 SDValue Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
7270 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
7271
7272 if (VT != MVT::v4i32)
7273 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
7274
7275 // Now we need to be really careful about how we update the users of the
7276 // original load. We cannot just call DCI.CombineTo (or
7277 // DAG.ReplaceAllUsesWith for that matter), because the load still has
7278 // uses created here (the permutation for example) that need to stay.
7279 SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
7280 while (UI != UE) {
7281 SDUse &Use = UI.getUse();
7282 SDNode *User = *UI;
7283 // Note: BaseLoad is checked here because it might not be N, but a
7284 // bitcast of N.
7285 if (User == Perm.getNode() || User == BaseLoad.getNode() ||
7286 User == TF.getNode() || Use.getResNo() > 1) {
7287 ++UI;
7288 continue;
7289 }
7290
7291 SDValue To = Use.getResNo() ? TF : Perm;
7292 ++UI;
7293
7294 SmallVector<SDValue, 8> Ops;
7295 for (SDNode::op_iterator O = User->op_begin(),
7296 OE = User->op_end(); O != OE; ++O) {
7297 if (*O == Use)
7298 Ops.push_back(To);
7299 else
7300 Ops.push_back(*O);
7301 }
7302
7303 DAG.UpdateNodeOperands(User, Ops.data(), Ops.size());
7304 }
7305
7306 return SDValue(N, 0);
7307 }
7308 }
7309 break;
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007310 case ISD::INTRINSIC_WO_CHAIN:
7311 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() ==
7312 Intrinsic::ppc_altivec_lvsl &&
7313 N->getOperand(1)->getOpcode() == ISD::ADD) {
7314 SDValue Add = N->getOperand(1);
7315
7316 if (DAG.MaskedValueIsZero(Add->getOperand(1),
7317 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
7318 Add.getValueType().getScalarType().getSizeInBits()))) {
7319 SDNode *BasePtr = Add->getOperand(0).getNode();
7320 for (SDNode::use_iterator UI = BasePtr->use_begin(),
7321 UE = BasePtr->use_end(); UI != UE; ++UI) {
7322 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
7323 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
7324 Intrinsic::ppc_altivec_lvsl) {
7325 // We've found another LVSL, and this address if an aligned
7326 // multiple of that one. The results will be the same, so use the
7327 // one we've just found instead.
7328
7329 return SDValue(*UI, 0);
7330 }
7331 }
7332 }
7333 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +00007334
7335 break;
Chris Lattnera7976d32006-07-10 20:56:58 +00007336 case ISD::BSWAP:
7337 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +00007338 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +00007339 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007340 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
7341 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00007342 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007343 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007344 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00007345 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +00007346 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007347 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +00007348 LD->getChain(), // Chain
7349 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007350 DAG.getValueType(N->getValueType(0)) // VT
7351 };
Dan Gohman48b185d2009-09-25 20:36:54 +00007352 SDValue BSLoad =
7353 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +00007354 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
7355 MVT::i64 : MVT::i32, MVT::Other),
Hal Finkel93492fa2013-03-28 19:43:12 +00007356 Ops, 3, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00007357
Scott Michelcf0da6c2009-02-17 22:15:04 +00007358 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007359 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +00007360 if (N->getValueType(0) == MVT::i16)
7361 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007362
Chris Lattnera7976d32006-07-10 20:56:58 +00007363 // First, combine the bswap away. This makes the value produced by the
7364 // load dead.
7365 DCI.CombineTo(N, ResVal);
7366
7367 // Next, combine the load away, we give it a bogus result value but a real
7368 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +00007369 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00007370
Chris Lattnera7976d32006-07-10 20:56:58 +00007371 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007372 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +00007373 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007374
Chris Lattner27f53452006-03-01 05:50:56 +00007375 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +00007376 case PPCISD::VCMP: {
7377 // If a VCMPo node already exists with exactly the same operands as this
7378 // node, use its result instead of this node (VCMPo computes both a CR6 and
7379 // a normal output).
7380 //
7381 if (!N->getOperand(0).hasOneUse() &&
7382 !N->getOperand(1).hasOneUse() &&
7383 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00007384
Chris Lattnerd4058a52006-03-31 06:02:07 +00007385 // Scan all of the users of the LHS, looking for VCMPo's that match.
7386 SDNode *VCMPoNode = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007387
Gabor Greiff304a7a2008-08-28 21:40:38 +00007388 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +00007389 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
7390 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007391 if (UI->getOpcode() == PPCISD::VCMPo &&
7392 UI->getOperand(1) == N->getOperand(1) &&
7393 UI->getOperand(2) == N->getOperand(2) &&
7394 UI->getOperand(0) == N->getOperand(0)) {
7395 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +00007396 break;
7397 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007398
Chris Lattner518834c2006-04-18 18:28:22 +00007399 // If there is no VCMPo node, or if the flag value has a single use, don't
7400 // transform this.
7401 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
7402 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007403
7404 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +00007405 // chain, this transformation is more complex. Note that multiple things
7406 // could use the value result, which we should ignore.
7407 SDNode *FlagUser = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007408 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner518834c2006-04-18 18:28:22 +00007409 FlagUser == 0; ++UI) {
7410 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007411 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +00007412 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007413 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +00007414 FlagUser = User;
7415 break;
7416 }
7417 }
7418 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007419
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00007420 // If the user is a MFOCRF instruction, we know this is safe.
7421 // Otherwise we give up for right now.
7422 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007423 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +00007424 }
7425 break;
7426 }
Chris Lattner9754d142006-04-18 17:59:36 +00007427 case ISD::BR_CC: {
7428 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00007429 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +00007430 // lowering is done pre-legalize, because the legalizer lowers the predicate
7431 // compare down to code that is difficult to reassemble.
7432 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007433 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +00007434
7435 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
7436 // value. If so, pass-through the AND to get to the intrinsic.
7437 if (LHS.getOpcode() == ISD::AND &&
7438 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
7439 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
7440 Intrinsic::ppc_is_decremented_ctr_nonzero &&
7441 isa<ConstantSDNode>(LHS.getOperand(1)) &&
7442 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
7443 isZero())
7444 LHS = LHS.getOperand(0);
7445
7446 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
7447 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
7448 Intrinsic::ppc_is_decremented_ctr_nonzero &&
7449 isa<ConstantSDNode>(RHS)) {
7450 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
7451 "Counter decrement comparison is not EQ or NE");
7452
7453 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
7454 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
7455 (CC == ISD::SETNE && !Val);
7456
7457 // We now need to make the intrinsic dead (it cannot be instruction
7458 // selected).
7459 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
7460 assert(LHS.getNode()->hasOneUse() &&
7461 "Counter decrement has more than one use");
7462
7463 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
7464 N->getOperand(0), N->getOperand(4));
7465 }
7466
Chris Lattner9754d142006-04-18 17:59:36 +00007467 int CompareOpc;
7468 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007469
Chris Lattner9754d142006-04-18 17:59:36 +00007470 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
7471 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
7472 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
7473 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00007474
Chris Lattner9754d142006-04-18 17:59:36 +00007475 // If this is a comparison against something other than 0/1, then we know
7476 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +00007477 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00007478 if (Val != 0 && Val != 1) {
7479 if (CC == ISD::SETEQ) // Cond never true, remove branch.
7480 return N->getOperand(0);
7481 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +00007482 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +00007483 N->getOperand(0), N->getOperand(4));
7484 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007485
Chris Lattner9754d142006-04-18 17:59:36 +00007486 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007487
Chris Lattner9754d142006-04-18 17:59:36 +00007488 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007489 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007490 LHS.getOperand(2), // LHS of compare
7491 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +00007492 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007493 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00007494 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00007495 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007496
Chris Lattner9754d142006-04-18 17:59:36 +00007497 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007498 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +00007499 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +00007500 default: // Can't happen, don't crash on invalid number though.
7501 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007502 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +00007503 break;
7504 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007505 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +00007506 break;
7507 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007508 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +00007509 break;
7510 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007511 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +00007512 break;
7513 }
7514
Owen Anderson9f944592009-08-11 20:47:22 +00007515 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
7516 DAG.getConstant(CompOpc, MVT::i32),
7517 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +00007518 N->getOperand(4), CompNode.getValue(1));
7519 }
7520 break;
7521 }
Chris Lattnerf4184352006-03-01 04:57:39 +00007522 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007523
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007524 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +00007525}
7526
Chris Lattner4211ca92006-04-14 06:01:58 +00007527//===----------------------------------------------------------------------===//
7528// Inline Assembly Support
7529//===----------------------------------------------------------------------===//
7530
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007531void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Scott Michelcf0da6c2009-02-17 22:15:04 +00007532 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +00007533 APInt &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +00007534 const SelectionDAG &DAG,
Chris Lattnerc5287c02006-04-02 06:26:07 +00007535 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00007536 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +00007537 switch (Op.getOpcode()) {
7538 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +00007539 case PPCISD::LBRX: {
7540 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +00007541 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +00007542 KnownZero = 0xFFFF0000;
7543 break;
7544 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00007545 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00007546 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +00007547 default: break;
7548 case Intrinsic::ppc_altivec_vcmpbfp_p:
7549 case Intrinsic::ppc_altivec_vcmpeqfp_p:
7550 case Intrinsic::ppc_altivec_vcmpequb_p:
7551 case Intrinsic::ppc_altivec_vcmpequh_p:
7552 case Intrinsic::ppc_altivec_vcmpequw_p:
7553 case Intrinsic::ppc_altivec_vcmpgefp_p:
7554 case Intrinsic::ppc_altivec_vcmpgtfp_p:
7555 case Intrinsic::ppc_altivec_vcmpgtsb_p:
7556 case Intrinsic::ppc_altivec_vcmpgtsh_p:
7557 case Intrinsic::ppc_altivec_vcmpgtsw_p:
7558 case Intrinsic::ppc_altivec_vcmpgtub_p:
7559 case Intrinsic::ppc_altivec_vcmpgtuh_p:
7560 case Intrinsic::ppc_altivec_vcmpgtuw_p:
7561 KnownZero = ~1U; // All bits but the low one are known to be zero.
7562 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007563 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00007564 }
7565 }
7566}
7567
7568
Chris Lattnerd6855142007-03-25 02:14:49 +00007569/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +00007570/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007571PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00007572PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
7573 if (Constraint.size() == 1) {
7574 switch (Constraint[0]) {
7575 default: break;
7576 case 'b':
7577 case 'r':
7578 case 'f':
7579 case 'v':
7580 case 'y':
7581 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +00007582 case 'Z':
7583 // FIXME: While Z does indicate a memory constraint, it specifically
7584 // indicates an r+r address (used in conjunction with the 'y' modifier
7585 // in the replacement string). Currently, we're forcing the base
7586 // register to be r0 in the asm printer (which is interpreted as zero)
7587 // and forming the complete address in the second register. This is
7588 // suboptimal.
7589 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +00007590 }
7591 }
7592 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +00007593}
7594
John Thompsone8360b72010-10-29 17:29:13 +00007595/// Examine constraint type and operand type and determine a weight value.
7596/// This object must already have been set up with the operand type
7597/// and the current alternative constraint selected.
7598TargetLowering::ConstraintWeight
7599PPCTargetLowering::getSingleConstraintMatchWeight(
7600 AsmOperandInfo &info, const char *constraint) const {
7601 ConstraintWeight weight = CW_Invalid;
7602 Value *CallOperandVal = info.CallOperandVal;
7603 // If we don't have a value, we can't do a match,
7604 // but allow it at the lowest weight.
7605 if (CallOperandVal == NULL)
7606 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00007607 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00007608 // Look at the constraint type.
7609 switch (*constraint) {
7610 default:
7611 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7612 break;
7613 case 'b':
7614 if (type->isIntegerTy())
7615 weight = CW_Register;
7616 break;
7617 case 'f':
7618 if (type->isFloatTy())
7619 weight = CW_Register;
7620 break;
7621 case 'd':
7622 if (type->isDoubleTy())
7623 weight = CW_Register;
7624 break;
7625 case 'v':
7626 if (type->isVectorTy())
7627 weight = CW_Register;
7628 break;
7629 case 'y':
7630 weight = CW_Register;
7631 break;
Hal Finkel4f24c622012-11-05 18:18:42 +00007632 case 'Z':
7633 weight = CW_Memory;
7634 break;
John Thompsone8360b72010-10-29 17:29:13 +00007635 }
7636 return weight;
7637}
7638
Scott Michelcf0da6c2009-02-17 22:15:04 +00007639std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner584a11a2006-11-02 01:44:04 +00007640PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00007641 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +00007642 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +00007643 // GCC RS6000 Constraint Letters
7644 switch (Constraint[0]) {
7645 case 'b': // R1-R31
Hal Finkel638a9fa2013-03-19 18:51:05 +00007646 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
7647 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
7648 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00007649 case 'r': // R0-R31
Owen Anderson9f944592009-08-11 20:47:22 +00007650 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +00007651 return std::make_pair(0U, &PPC::G8RCRegClass);
7652 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00007653 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00007654 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +00007655 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00007656 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +00007657 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00007658 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007659 case 'v':
Craig Topperabadc662012-04-20 06:31:50 +00007660 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00007661 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +00007662 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00007663 }
7664 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007665
Hal Finkelb176acb2013-08-03 12:25:10 +00007666 std::pair<unsigned, const TargetRegisterClass*> R =
7667 TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7668
7669 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
7670 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
7671 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
7672 // register.
7673 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
7674 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
7675 if (R.first && VT == MVT::i64 && PPCSubTarget.isPPC64() &&
7676 PPC::GPRCRegClass.contains(R.first)) {
7677 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
7678 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +00007679 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +00007680 &PPC::G8RCRegClass);
7681 }
7682
7683 return R;
Chris Lattner01513612006-01-31 19:20:21 +00007684}
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007685
Chris Lattner584a11a2006-11-02 01:44:04 +00007686
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007687/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +00007688/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +00007689void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00007690 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007691 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00007692 SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007693 SDValue Result(0,0);
Eric Christopher0713a9d2011-06-08 23:55:35 +00007694
Eric Christopherde9399b2011-06-02 23:16:42 +00007695 // Only support length 1 constraints.
7696 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +00007697
Eric Christopherde9399b2011-06-02 23:16:42 +00007698 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007699 switch (Letter) {
7700 default: break;
7701 case 'I':
7702 case 'J':
7703 case 'K':
7704 case 'L':
7705 case 'M':
7706 case 'N':
7707 case 'O':
7708 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +00007709 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007710 if (!CST) return; // Must be an immediate to match.
Dan Gohmaneffb8942008-09-12 16:56:44 +00007711 unsigned Value = CST->getZExtValue();
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007712 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007713 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007714 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007715 if ((short)Value == (int)Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007716 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007717 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007718 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
7719 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007720 if ((short)Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007721 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007722 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007723 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007724 if ((Value >> 16) == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007725 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007726 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007727 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007728 if (Value > 31)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007729 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007730 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007731 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007732 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007733 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007734 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007735 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007736 if (Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007737 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007738 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007739 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007740 if ((short)-Value == (int)-Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007741 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007742 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007743 }
7744 break;
7745 }
7746 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007747
Gabor Greiff304a7a2008-08-28 21:40:38 +00007748 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007749 Ops.push_back(Result);
7750 return;
7751 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007752
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007753 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +00007754 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007755}
Evan Cheng2dd2c652006-03-13 23:20:37 +00007756
Chris Lattner1eb94d92007-03-30 23:15:24 +00007757// isLegalAddressingMode - Return true if the addressing mode represented
7758// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007759bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00007760 Type *Ty) const {
Chris Lattner1eb94d92007-03-30 23:15:24 +00007761 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelcf0da6c2009-02-17 22:15:04 +00007762
Chris Lattner1eb94d92007-03-30 23:15:24 +00007763 // PPC allows a sign-extended 16-bit immediate field.
7764 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
7765 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007766
Chris Lattner1eb94d92007-03-30 23:15:24 +00007767 // No global is ever allowed as a base.
7768 if (AM.BaseGV)
7769 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007770
7771 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +00007772 switch (AM.Scale) {
7773 case 0: // "r+i" or just "i", depending on HasBaseReg.
7774 break;
7775 case 1:
7776 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
7777 return false;
7778 // Otherwise we have r+r or r+i.
7779 break;
7780 case 2:
7781 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
7782 return false;
7783 // Allow 2*r as r+r.
7784 break;
Chris Lattner19ccd622007-04-09 22:10:05 +00007785 default:
7786 // No other scales are supported.
7787 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +00007788 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007789
Chris Lattner1eb94d92007-03-30 23:15:24 +00007790 return true;
7791}
7792
Dan Gohman21cea8a2010-04-17 15:26:15 +00007793SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
7794 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00007795 MachineFunction &MF = DAG.getMachineFunction();
7796 MachineFrameInfo *MFI = MF.getFrameInfo();
7797 MFI->setReturnAddressIsTaken(true);
7798
Bill Wendlingdf7dd282014-01-05 01:47:20 +00007799 if (!isa<ConstantSDNode>(Op.getOperand(0))) {
7800 DAG.getContext()->emitError("argument to '__builtin_return_address' must "
7801 "be a constant integer");
7802 return SDValue();
7803 }
7804
Andrew Trickef9de2a2013-05-25 02:42:55 +00007805 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007806 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +00007807
Dale Johannesen81bfca72010-05-03 22:59:34 +00007808 // Make sure the function does not optimize away the store of the RA to
7809 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +00007810 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +00007811 FuncInfo->setLRStoreRequired();
7812 bool isPPC64 = PPCSubTarget.isPPC64();
7813 bool isDarwinABI = PPCSubTarget.isDarwinABI();
7814
7815 if (Depth > 0) {
7816 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7817 SDValue Offset =
Wesley Peck527da1b2010-11-23 03:31:01 +00007818
Anton Korobeynikov2f931282011-01-10 12:39:04 +00007819 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen81bfca72010-05-03 22:59:34 +00007820 isPPC64? MVT::i64 : MVT::i32);
7821 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
7822 DAG.getNode(ISD::ADD, dl, getPointerTy(),
7823 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00007824 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007825 }
Chris Lattnerf6a81562007-12-08 06:59:59 +00007826
Chris Lattnerf6a81562007-12-08 06:59:59 +00007827 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007828 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007829 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00007830 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +00007831}
7832
Dan Gohman21cea8a2010-04-17 15:26:15 +00007833SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
7834 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007835 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007836 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00007837
Owen Anderson53aa7a92009-08-10 22:56:29 +00007838 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00007839 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007840
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00007841 MachineFunction &MF = DAG.getMachineFunction();
7842 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +00007843 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +00007844
7845 // Naked functions never have a frame pointer, and so we use r1. For all
7846 // other functions, this decision must be delayed until during PEI.
7847 unsigned FrameReg;
7848 if (MF.getFunction()->getAttributes().hasAttribute(
7849 AttributeSet::FunctionIndex, Attribute::Naked))
7850 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
7851 else
7852 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
7853
Dale Johannesen81bfca72010-05-03 22:59:34 +00007854 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
7855 PtrVT);
7856 while (Depth--)
7857 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00007858 FrameAddr, MachinePointerInfo(), false, false,
7859 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007860 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00007861}
Dan Gohmanc14e5222008-10-21 03:41:46 +00007862
7863bool
7864PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7865 // The PowerPC target isn't yet aware of offsets.
7866 return false;
7867}
Tilmann Schellerb93960d2009-07-03 06:45:56 +00007868
Evan Chengd9929f02010-04-01 20:10:42 +00007869/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +00007870/// and store operations as a result of memset, memcpy, and memmove
7871/// lowering. If DstAlign is zero that means it's safe to destination
7872/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
7873/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +00007874/// probably because the source does not need to be loaded. If 'IsMemset' is
7875/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
7876/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
7877/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +00007878/// It returns EVT::Other if the type should be determined using generic
7879/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +00007880EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
7881 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00007882 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00007883 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +00007884 MachineFunction &MF) const {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00007885 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +00007886 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00007887 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00007888 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00007889 }
7890}
Hal Finkel88ed4e32012-04-01 19:23:08 +00007891
Hal Finkel8d7fbc92013-03-15 15:27:13 +00007892bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
7893 bool *Fast) const {
7894 if (DisablePPCUnaligned)
7895 return false;
7896
7897 // PowerPC supports unaligned memory access for simple non-vector types.
7898 // Although accessing unaligned addresses is not as efficient as accessing
7899 // aligned addresses, it is generally more efficient than manual expansion,
7900 // and generally only traps for software emulation when crossing page
7901 // boundaries.
7902
7903 if (!VT.isSimple())
7904 return false;
7905
7906 if (VT.getSimpleVT().isVector())
7907 return false;
7908
7909 if (VT == MVT::ppcf128)
7910 return false;
7911
7912 if (Fast)
7913 *Fast = true;
7914
7915 return true;
7916}
7917
Stephen Lin73de7bf2013-07-09 18:16:56 +00007918bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
7919 VT = VT.getScalarType();
7920
Hal Finkel0a479ae2012-06-22 00:49:52 +00007921 if (!VT.isSimple())
7922 return false;
7923
7924 switch (VT.getSimpleVT().SimpleTy) {
7925 case MVT::f32:
7926 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +00007927 return true;
7928 default:
7929 break;
7930 }
7931
7932 return false;
7933}
7934
Hal Finkel88ed4e32012-04-01 19:23:08 +00007935Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel21442b22013-09-11 23:05:25 +00007936 if (DisableILPPref || PPCSubTarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +00007937 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +00007938
Hal Finkel4e9f1a82012-06-10 19:32:29 +00007939 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +00007940}
7941
Bill Schmidt0cf702f2013-07-30 00:50:39 +00007942// Create a fast isel object.
7943FastISel *
7944PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
7945 const TargetLibraryInfo *LibInfo) const {
7946 return PPC::createFastISel(FuncInfo, LibInfo);
7947}