blob: 1759c0415e5b1223a511efd829870e4fce20657b [file] [log] [blame]
Nate Begeman0b71e002005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattnerf22556d2005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf22556d2005-08-16 17:14:42 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattnerf22556d2005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6f3b9542005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Jim Laskey48850c12006-11-16 22:43:37 +000016#include "PPCMachineFunctionInfo.h"
Bill Wendlingdd3fe942010-03-12 02:00:43 +000017#include "PPCPerfectShuffle.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000018#include "PPCTargetMachine.h"
Bill Schmidt22d40dc2013-05-13 19:34:37 +000019#include "PPCTargetObjectFile.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000020#include "llvm/ADT/STLExtras.h"
Chris Lattner4f2e4e02007-03-06 00:59:59 +000021#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner9b577f12005-08-26 21:23:58 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000026#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000027#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/Constants.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/Intrinsics.h"
Chris Lattnerce645542006-11-10 02:08:47 +000033#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
Craig Topperb25fda92012-03-17 18:46:09 +000035#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000036#include "llvm/Support/raw_ostream.h"
Craig Topperb25fda92012-03-17 18:46:09 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000038using namespace llvm;
39
Hal Finkel595817e2012-06-04 02:21:00 +000040static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
41cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000042
Hal Finkel4e9f1a82012-06-10 19:32:29 +000043static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
44cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
45
Hal Finkel8d7fbc92013-03-15 15:27:13 +000046static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
47cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
48
Chris Lattner5e693ed2009-07-28 03:13:23 +000049static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) {
50 if (TM.getSubtargetImpl()->isDarwin())
Bill Wendlingbbcaa402010-03-15 21:09:38 +000051 return new TargetLoweringObjectFileMachO();
Bill Wendlingdd3fe942010-03-12 02:00:43 +000052
Bill Schmidt22d40dc2013-05-13 19:34:37 +000053 if (TM.getSubtargetImpl()->isSVR4ABI())
54 return new PPC64LinuxTargetObjectFile();
55
Bruno Cardoso Lopes62e6a8b2009-08-13 23:30:21 +000056 return new TargetLoweringObjectFileELF();
Chris Lattner5e693ed2009-07-28 03:13:23 +000057}
58
Chris Lattner584a11a2006-11-02 01:44:04 +000059PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Chris Lattner5e693ed2009-07-28 03:13:23 +000060 : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) {
Evan Cheng39e90022012-07-02 22:39:56 +000061 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
Scott Michelcf0da6c2009-02-17 22:15:04 +000062
Nate Begeman4dd38312005-10-21 00:02:42 +000063 setPow2DivIsCheap();
Dale Johannesenc31eb202008-07-31 18:13:12 +000064
Chris Lattnera028e7a2005-09-27 22:18:25 +000065 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000066 setUseUnderscoreSetJmp(true);
67 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000068
Chris Lattnerd10babf2010-10-10 18:34:00 +000069 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
70 // arguments are at least 4/8 bytes aligned.
Evan Cheng39e90022012-07-02 22:39:56 +000071 bool isPPC64 = Subtarget->isPPC64();
72 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000073
Chris Lattnerf22556d2005-08-16 17:14:42 +000074 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000075 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
76 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
77 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +000078
Evan Cheng5d9fd972006-10-04 00:56:09 +000079 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson9f944592009-08-11 20:47:22 +000080 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
81 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sands95d46ef2008-01-23 20:39:46 +000082
Owen Anderson9f944592009-08-11 20:47:22 +000083 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000084
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000085 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000086 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
87 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
88 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
89 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
90 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
91 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
92 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
93 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
94 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
95 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +000096
Dale Johannesen666323e2007-10-10 01:01:31 +000097 // This is used in the ppcf128->int sequence. Note it has different semantics
98 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +000099 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000100
Roman Divacky1faf5b02012-08-16 18:19:29 +0000101 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000102 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
103 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
104 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
105 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
106 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000107 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000108
Chris Lattnerf22556d2005-08-16 17:14:42 +0000109 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000110 setOperationAction(ISD::SREM, MVT::i32, Expand);
111 setOperationAction(ISD::UREM, MVT::i32, Expand);
112 setOperationAction(ISD::SREM, MVT::i64, Expand);
113 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000114
115 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000116 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
117 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
118 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
119 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
120 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
121 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
122 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
123 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000124
Dan Gohman482732a2007-10-11 23:21:31 +0000125 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000126 setOperationAction(ISD::FSIN , MVT::f64, Expand);
127 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000128 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000129 setOperationAction(ISD::FREM , MVT::f64, Expand);
130 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000131 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000132 setOperationAction(ISD::FSIN , MVT::f32, Expand);
133 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000134 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000135 setOperationAction(ISD::FREM , MVT::f32, Expand);
136 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000137 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000138
Owen Anderson9f944592009-08-11 20:47:22 +0000139 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000140
Chris Lattnerf22556d2005-08-16 17:14:42 +0000141 // If we're enabling GP optimizations, use hardware square root
Hal Finkel2e103312013-04-03 04:01:11 +0000142 if (!Subtarget->hasFSQRT() &&
143 !(TM.Options.UnsafeFPMath &&
144 Subtarget->hasFRSQRTE() && Subtarget->hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000145 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000146
147 if (!Subtarget->hasFSQRT() &&
148 !(TM.Options.UnsafeFPMath &&
149 Subtarget->hasFRSQRTES() && Subtarget->hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000150 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000151
Owen Anderson9f944592009-08-11 20:47:22 +0000152 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
153 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000154
Hal Finkelc20a08d2013-03-29 08:57:48 +0000155 if (Subtarget->hasFPRND()) {
156 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
157 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
158 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
159
160 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
161 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
162 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
163
164 // frin does not implement "ties to even." Thus, this is safe only in
165 // fast-math mode.
166 if (TM.Options.UnsafeFPMath) {
167 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
168 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
Hal Finkelf8ac57e2013-03-29 19:41:55 +0000169
170 // These need to set FE_INEXACT, and use a custom inserter.
171 setOperationAction(ISD::FRINT, MVT::f64, Legal);
172 setOperationAction(ISD::FRINT, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000173 }
174 }
175
Nate Begeman2fba8a32006-01-14 03:14:10 +0000176 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000177 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000178 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000179 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
180 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000181 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000182 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000183 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
184 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000185
Hal Finkela4d07482013-03-28 13:29:47 +0000186 if (Subtarget->hasPOPCNTD()) {
Hal Finkel290376d2013-04-01 15:58:15 +0000187 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000188 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
189 } else {
190 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
191 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
192 }
193
Nate Begeman1b8121b2006-01-11 21:21:00 +0000194 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000195 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
196 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000197
Chris Lattnerf22556d2005-08-16 17:14:42 +0000198 // PowerPC does not have Select
Owen Anderson9f944592009-08-11 20:47:22 +0000199 setOperationAction(ISD::SELECT, MVT::i32, Expand);
200 setOperationAction(ISD::SELECT, MVT::i64, Expand);
201 setOperationAction(ISD::SELECT, MVT::f32, Expand);
202 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000203
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000204 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000205 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
206 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000207
Nate Begeman7e7f4392006-02-01 07:19:44 +0000208 // PowerPC wants to optimize integer setcc a bit
Owen Anderson9f944592009-08-11 20:47:22 +0000209 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000210
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000211 // PowerPC does not have BRCOND which requires SetCC
Owen Anderson9f944592009-08-11 20:47:22 +0000212 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000213
Owen Anderson9f944592009-08-11 20:47:22 +0000214 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000215
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000216 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000217 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000218
Jim Laskey6267b2c2005-08-17 00:40:22 +0000219 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000220 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
221 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000222
Wesley Peck527da1b2010-11-23 03:31:01 +0000223 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
224 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
225 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
226 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattnerc46fc242005-12-23 05:13:35 +0000227
Chris Lattner84b49d52006-04-28 21:56:10 +0000228 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000229 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000230
Hal Finkel1996f3d2013-03-27 19:10:42 +0000231 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000232 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
233 // support continuation, user-level threading, and etc.. As a result, no
234 // other SjLj exception interfaces are implemented and please don't build
235 // your own exception handling based on them.
236 // LLVM/Clang supports zero-cost DWARF exception handling.
237 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
238 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000239
240 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000241 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000242 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
243 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000244 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000245 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
246 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
247 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
248 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000249 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000250 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
251 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000252
Nate Begemanf69d13b2008-08-11 17:36:31 +0000253 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000254 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000255
256 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000257 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
258 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000259
Nate Begemane74795c2006-01-25 18:21:52 +0000260 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000261 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000262
Evan Cheng39e90022012-07-02 22:39:56 +0000263 if (Subtarget->isSVR4ABI()) {
264 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000265 // VAARG always uses double-word chunks, so promote anything smaller.
266 setOperationAction(ISD::VAARG, MVT::i1, Promote);
267 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
268 setOperationAction(ISD::VAARG, MVT::i8, Promote);
269 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
270 setOperationAction(ISD::VAARG, MVT::i16, Promote);
271 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
272 setOperationAction(ISD::VAARG, MVT::i32, Promote);
273 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
274 setOperationAction(ISD::VAARG, MVT::Other, Expand);
275 } else {
276 // VAARG is custom lowered with the 32-bit SVR4 ABI.
277 setOperationAction(ISD::VAARG, MVT::Other, Custom);
278 setOperationAction(ISD::VAARG, MVT::i64, Custom);
279 }
Roman Divacky4394e682011-06-28 15:30:42 +0000280 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000281 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000282
Chris Lattner5bd514d2006-01-15 09:02:48 +0000283 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000284 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
285 setOperationAction(ISD::VAEND , MVT::Other, Expand);
286 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
287 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
288 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
289 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000290
Chris Lattner6961fc72006-03-26 10:06:40 +0000291 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000292 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000293
Hal Finkel25c19922013-05-15 21:37:41 +0000294 // To handle counter-based loop conditions.
295 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
296
Dale Johannesen160be0f2008-11-07 22:54:33 +0000297 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000298 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
299 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
300 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
301 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
302 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
303 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
304 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
305 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
306 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
307 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
308 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
309 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000310
Evan Cheng39e90022012-07-02 22:39:56 +0000311 if (Subtarget->has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000312 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000313 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
314 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
315 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
316 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000317 // This is just the low 32 bits of a (signed) fp->i64 conversion.
318 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000320
Hal Finkelf6d45f22013-04-01 17:52:07 +0000321 if (PPCSubTarget.hasLFIWAX() || Subtarget->isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000322 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000323 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000324 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000325 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000326 }
327
Hal Finkelf6d45f22013-04-01 17:52:07 +0000328 // With the instructions enabled under FPCVT, we can do everything.
329 if (PPCSubTarget.hasFPCVT()) {
330 if (Subtarget->has64BitSupport()) {
331 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
332 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
333 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
334 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
335 }
336
337 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
338 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
339 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
340 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
341 }
342
Evan Cheng39e90022012-07-02 22:39:56 +0000343 if (Subtarget->use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000344 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000345 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000346 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000347 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000348 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000349 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
350 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
351 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000352 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000353 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000354 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
355 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
356 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000357 }
Evan Cheng19264272006-03-01 01:11:20 +0000358
Evan Cheng39e90022012-07-02 22:39:56 +0000359 if (Subtarget->hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000360 // First set operation action for all vector types to expand. Then we
361 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson9f944592009-08-11 20:47:22 +0000362 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
363 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
364 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands13237ac2008-06-06 12:08:01 +0000365
Chris Lattner06a21ba2006-04-16 01:37:57 +0000366 // add/sub are legal for all supported vector VT's.
Duncan Sands13237ac2008-06-06 12:08:01 +0000367 setOperationAction(ISD::ADD , VT, Legal);
368 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000369
Chris Lattner95c7adc2006-04-04 17:25:31 +0000370 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000371 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000372 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000373
374 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000375 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000376 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000377 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000378 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000379 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000380 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000381 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000382 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000383 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000384 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000385 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000386 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000387
Chris Lattner06a21ba2006-04-16 01:37:57 +0000388 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000389 setOperationAction(ISD::MUL , VT, Expand);
390 setOperationAction(ISD::SDIV, VT, Expand);
391 setOperationAction(ISD::SREM, VT, Expand);
392 setOperationAction(ISD::UDIV, VT, Expand);
393 setOperationAction(ISD::UREM, VT, Expand);
394 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000395 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000396 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000397 setOperationAction(ISD::FSQRT, VT, Expand);
398 setOperationAction(ISD::FLOG, VT, Expand);
399 setOperationAction(ISD::FLOG10, VT, Expand);
400 setOperationAction(ISD::FLOG2, VT, Expand);
401 setOperationAction(ISD::FEXP, VT, Expand);
402 setOperationAction(ISD::FEXP2, VT, Expand);
403 setOperationAction(ISD::FSIN, VT, Expand);
404 setOperationAction(ISD::FCOS, VT, Expand);
405 setOperationAction(ISD::FABS, VT, Expand);
406 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000407 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000408 setOperationAction(ISD::FCEIL, VT, Expand);
409 setOperationAction(ISD::FTRUNC, VT, Expand);
410 setOperationAction(ISD::FRINT, VT, Expand);
411 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000412 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
413 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
414 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
415 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
416 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
417 setOperationAction(ISD::UDIVREM, VT, Expand);
418 setOperationAction(ISD::SDIVREM, VT, Expand);
419 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
420 setOperationAction(ISD::FPOW, VT, Expand);
421 setOperationAction(ISD::CTPOP, VT, Expand);
422 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000423 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000424 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000425 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000426 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000427 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
428
429 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
430 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
431 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
432 setTruncStoreAction(VT, InnerVT, Expand);
433 }
434 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
435 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
436 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000437 }
438
Chris Lattner95c7adc2006-04-04 17:25:31 +0000439 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
440 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000441 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000442
Owen Anderson9f944592009-08-11 20:47:22 +0000443 setOperationAction(ISD::AND , MVT::v4i32, Legal);
444 setOperationAction(ISD::OR , MVT::v4i32, Legal);
445 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
446 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
447 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
448 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000449 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
450 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
451 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
452 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000453 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
454 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
455 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
456 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000457
Craig Topperabadc662012-04-20 06:31:50 +0000458 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
459 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
460 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
461 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000462
Owen Anderson9f944592009-08-11 20:47:22 +0000463 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000464 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000465
466 if (TM.Options.UnsafeFPMath) {
467 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
468 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
469 }
470
Owen Anderson9f944592009-08-11 20:47:22 +0000471 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
472 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
473 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000474
Owen Anderson9f944592009-08-11 20:47:22 +0000475 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
476 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000477
Owen Anderson9f944592009-08-11 20:47:22 +0000478 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
479 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
480 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
481 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000482
483 // Altivec does not contain unordered floating-point compare instructions
484 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
485 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
486 setCondCodeAction(ISD::SETUGT, MVT::v4f32, Expand);
487 setCondCodeAction(ISD::SETUGE, MVT::v4f32, Expand);
488 setCondCodeAction(ISD::SETULT, MVT::v4f32, Expand);
489 setCondCodeAction(ISD::SETULE, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000490
491 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
492 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000493 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000494
Hal Finkel70381a72012-08-04 14:10:46 +0000495 if (Subtarget->has64BitSupport()) {
Hal Finkel322e41a2012-04-01 20:08:17 +0000496 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel70381a72012-08-04 14:10:46 +0000497 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
498 }
Hal Finkel322e41a2012-04-01 20:08:17 +0000499
Eli Friedman7dfa7912011-08-29 18:23:02 +0000500 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
501 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkel1b5ff082012-12-25 17:22:53 +0000502 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
503 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000504
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000505 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidta76bf5a2013-04-23 18:49:44 +0000506 // Altivec instructions set fields to all zeros or all ones.
507 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000508
Evan Cheng39e90022012-07-02 22:39:56 +0000509 if (isPPC64) {
Chris Lattner454436d2006-10-18 01:20:43 +0000510 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000511 setExceptionPointerRegister(PPC::X3);
512 setExceptionSelectorRegister(PPC::X4);
513 } else {
Chris Lattner454436d2006-10-18 01:20:43 +0000514 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000515 setExceptionPointerRegister(PPC::R3);
516 setExceptionSelectorRegister(PPC::R4);
517 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000518
Chris Lattnerf4184352006-03-01 04:57:39 +0000519 // We have target-specific dag combine patterns for the following nodes:
520 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000521 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000522 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000523 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnera7976d32006-07-10 20:56:58 +0000524 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000525 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000526
Hal Finkel2e103312013-04-03 04:01:11 +0000527 // Use reciprocal estimates.
528 if (TM.Options.UnsafeFPMath) {
529 setTargetDAGCombine(ISD::FDIV);
530 setTargetDAGCombine(ISD::FSQRT);
531 }
532
Dale Johannesen10432e52007-10-19 00:59:18 +0000533 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng39e90022012-07-02 22:39:56 +0000534 if (Subtarget->isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000535 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000536 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
537 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000538 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
539 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000540 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
541 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
542 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
543 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
544 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000545 }
546
Hal Finkel65298572011-10-17 18:53:03 +0000547 setMinFunctionAlignment(2);
548 if (PPCSubTarget.isDarwin())
549 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000550
Evan Cheng39e90022012-07-02 22:39:56 +0000551 if (isPPC64 && Subtarget->isJITCodeModel())
552 // Temporary workaround for the inability of PPC64 JIT to handle jump
553 // tables.
554 setSupportJumpTables(false);
555
Eli Friedman30a49e92011-08-03 21:06:02 +0000556 setInsertFencesForAtomic(true);
557
Hal Finkel6f0ae782011-11-22 16:21:04 +0000558 setSchedulingPreference(Sched::Hybrid);
559
Chris Lattnerf22556d2005-08-16 17:14:42 +0000560 computeRegisterProperties();
Hal Finkel742b5352012-08-28 16:12:39 +0000561
562 // The Freescale cores does better with aggressive inlining of memcpy and
563 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
564 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
565 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000566 MaxStoresPerMemset = 32;
567 MaxStoresPerMemsetOptSize = 16;
568 MaxStoresPerMemcpy = 32;
569 MaxStoresPerMemcpyOptSize = 8;
570 MaxStoresPerMemmove = 32;
571 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel742b5352012-08-28 16:12:39 +0000572
573 setPrefFunctionAlignment(4);
Hal Finkel742b5352012-08-28 16:12:39 +0000574 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000575}
576
Dale Johannesencbde4c22008-02-28 22:31:51 +0000577/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
578/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000579unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dan Gohman57c732b2010-04-21 01:34:56 +0000580 const TargetMachine &TM = getTargetMachine();
Dale Johannesencbde4c22008-02-28 22:31:51 +0000581 // Darwin passes everything on 4 byte boundary.
582 if (TM.getSubtarget<PPCSubtarget>().isDarwin())
583 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000584
585 // 16byte and wider vectors are passed on 16byte boundary.
586 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
587 if (VTy->getBitWidth() >= 128)
588 return 16;
589
590 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
591 if (PPCSubTarget.isPPC64())
592 return 8;
593
Dale Johannesencbde4c22008-02-28 22:31:51 +0000594 return 4;
595}
596
Chris Lattner347ed8a2006-01-09 23:52:17 +0000597const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
598 switch (Opcode) {
599 default: return 0;
Evan Cheng32e376f2008-07-12 02:23:19 +0000600 case PPCISD::FSEL: return "PPCISD::FSEL";
601 case PPCISD::FCFID: return "PPCISD::FCFID";
602 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
603 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000604 case PPCISD::FRE: return "PPCISD::FRE";
605 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000606 case PPCISD::STFIWX: return "PPCISD::STFIWX";
607 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
608 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
609 case PPCISD::VPERM: return "PPCISD::VPERM";
610 case PPCISD::Hi: return "PPCISD::Hi";
611 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000612 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller79fef932009-12-18 13:00:15 +0000613 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
614 case PPCISD::LOAD: return "PPCISD::LOAD";
615 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000616 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
617 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
618 case PPCISD::SRL: return "PPCISD::SRL";
619 case PPCISD::SRA: return "PPCISD::SRA";
620 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000621 case PPCISD::CALL: return "PPCISD::CALL";
622 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000623 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000624 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng32e376f2008-07-12 02:23:19 +0000625 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel756810f2013-03-21 21:37:52 +0000626 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
627 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000628 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng32e376f2008-07-12 02:23:19 +0000629 case PPCISD::VCMP: return "PPCISD::VCMP";
630 case PPCISD::VCMPo: return "PPCISD::VCMPo";
631 case PPCISD::LBRX: return "PPCISD::LBRX";
632 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng32e376f2008-07-12 02:23:19 +0000633 case PPCISD::LARX: return "PPCISD::LARX";
634 case PPCISD::STCX: return "PPCISD::STCX";
635 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +0000636 case PPCISD::BDNZ: return "PPCISD::BDNZ";
637 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000638 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000639 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000640 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +0000641 case PPCISD::CR6SET: return "PPCISD::CR6SET";
642 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34627e32012-11-27 17:35:46 +0000643 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
644 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
645 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000646 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
647 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000648 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000649 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
650 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
651 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000652 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
653 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
654 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
655 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
656 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +0000657 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +0000658 case PPCISD::SC: return "PPCISD::SC";
Chris Lattner347ed8a2006-01-09 23:52:17 +0000659 }
660}
661
Matt Arsenault758659232013-05-18 00:21:46 +0000662EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000663 if (!VT.isVector())
664 return MVT::i32;
665 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000666}
667
Chris Lattner4211ca92006-04-14 06:01:58 +0000668//===----------------------------------------------------------------------===//
669// Node matching predicates, for use by the tblgen matching code.
670//===----------------------------------------------------------------------===//
671
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000672/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000673static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000674 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000675 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000676 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000677 // Maybe this has already been legalized into the constant pool?
678 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000679 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000680 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000681 }
682 return false;
683}
684
Chris Lattnere8b83b42006-04-06 17:23:16 +0000685/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
686/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000687static bool isConstantOrUndef(int Op, int Val) {
688 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000689}
690
691/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
692/// VPKUHUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000693bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000694 if (!isUnary) {
695 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000696 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000697 return false;
698 } else {
699 for (unsigned i = 0; i != 8; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000700 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
701 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000702 return false;
703 }
Chris Lattner1d338192006-04-06 18:26:28 +0000704 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000705}
706
707/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
708/// VPKUWUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000709bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000710 if (!isUnary) {
711 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000712 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
713 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000714 return false;
715 } else {
716 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000717 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
718 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
719 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
720 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000721 return false;
722 }
Chris Lattner1d338192006-04-06 18:26:28 +0000723 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000724}
725
Chris Lattnerf38e0332006-04-06 22:02:42 +0000726/// isVMerge - Common function, used to match vmrg* shuffles.
727///
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000728static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +0000729 unsigned LHSStart, unsigned RHSStart) {
Owen Anderson9f944592009-08-11 20:47:22 +0000730 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000731 "PPC only supports shuffles by bytes!");
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000732 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
733 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000734
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000735 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
736 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000737 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000738 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000739 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000740 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000741 return false;
742 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000743 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +0000744}
745
746/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
747/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000748bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000749 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000750 if (!isUnary)
751 return isVMerge(N, UnitSize, 8, 24);
752 return isVMerge(N, UnitSize, 8, 8);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000753}
754
755/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
756/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000757bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000758 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000759 if (!isUnary)
760 return isVMerge(N, UnitSize, 0, 16);
761 return isVMerge(N, UnitSize, 0, 0);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000762}
763
764
Chris Lattner1d338192006-04-06 18:26:28 +0000765/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
766/// amount, otherwise return -1.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000767int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Owen Anderson9f944592009-08-11 20:47:22 +0000768 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000769 "PPC only supports shuffles by bytes!");
770
771 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +0000772
Chris Lattner1d338192006-04-06 18:26:28 +0000773 // Find the first non-undef value in the shuffle mask.
774 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000775 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +0000776 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000777
Chris Lattner1d338192006-04-06 18:26:28 +0000778 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000779
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000780 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +0000781 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000782 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +0000783 if (ShiftAmt < i) return -1;
784 ShiftAmt -= i;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000785
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000786 if (!isUnary) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000787 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000788 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000789 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000790 return -1;
791 } else {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000792 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000793 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000794 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000795 return -1;
796 }
Chris Lattner1d338192006-04-06 18:26:28 +0000797 return ShiftAmt;
798}
Chris Lattnerffc47562006-03-20 06:33:01 +0000799
800/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
801/// specifies a splat of a single element that is suitable for input to
802/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000803bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +0000804 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +0000805 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000806
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000807 // This is a splat operation if each element of the permute is the same, and
808 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000809 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +0000810
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000811 // FIXME: Handle UNDEF elements too!
812 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +0000813 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000814
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000815 // Check that the indices are consecutive, in the case of a multi-byte element
816 // splatted with a v16i8 mask.
817 for (unsigned i = 1; i != EltSize; ++i)
818 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000819 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000820
Chris Lattner95c7adc2006-04-04 17:25:31 +0000821 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000822 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +0000823 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000824 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000825 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000826 }
Chris Lattner95c7adc2006-04-04 17:25:31 +0000827 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +0000828}
829
Evan Cheng581d2792007-07-30 07:51:22 +0000830/// isAllNegativeZeroVector - Returns true if all elements of build_vector
831/// are -0.0.
832bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000833 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
834
835 APInt APVal, APUndef;
836 unsigned BitSize;
837 bool HasAnyUndefs;
Wesley Peck527da1b2010-11-23 03:31:01 +0000838
Dale Johannesen5f4eecf2009-11-13 01:45:18 +0000839 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000840 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000841 return CFP->getValueAPF().isNegZero();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000842
Evan Cheng581d2792007-07-30 07:51:22 +0000843 return false;
844}
845
Chris Lattnerffc47562006-03-20 06:33:01 +0000846/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
847/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner95c7adc2006-04-04 17:25:31 +0000848unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000849 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
850 assert(isSplatShuffleMask(SVOp, EltSize));
851 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +0000852}
853
Chris Lattner74cf9ff2006-04-12 17:37:20 +0000854/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +0000855/// by using a vspltis[bhw] instruction of the specified element size, return
856/// the constant being splatted. The ByteSize field indicates the number of
857/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000858SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
859 SDValue OpVal(0, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000860
861 // If ByteSize of the splat is bigger than the element size of the
862 // build_vector, then we have a case where we are checking for a splat where
863 // multiple elements of the buildvector are folded together into a single
864 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
865 unsigned EltSize = 16/N->getNumOperands();
866 if (EltSize < ByteSize) {
867 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000868 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000869 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000870
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000871 // See if all of the elements in the buildvector agree across.
872 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
873 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
874 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000875 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000876
Scott Michelcf0da6c2009-02-17 22:15:04 +0000877
Gabor Greiff304a7a2008-08-28 21:40:38 +0000878 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000879 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
880 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000881 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000882 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000883
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000884 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
885 // either constant or undef values that are identical for each chunk. See
886 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000887
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000888 // Check to see if all of the leading entries are either 0 or -1. If
889 // neither, then this won't fit into the immediate field.
890 bool LeadingZero = true;
891 bool LeadingOnes = true;
892 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000893 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000894
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000895 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
896 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
897 }
898 // Finally, check the least significant entry.
899 if (LeadingZero) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000900 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +0000901 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +0000902 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000903 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +0000904 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000905 }
906 if (LeadingOnes) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000907 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson9f944592009-08-11 20:47:22 +0000908 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +0000909 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000910 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +0000911 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000912 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000913
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000914 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +0000915 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000916
Chris Lattner2771e2c2006-03-25 06:12:06 +0000917 // Check to see if this buildvec has a single non-undef value in its elements.
918 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
919 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greiff304a7a2008-08-28 21:40:38 +0000920 if (OpVal.getNode() == 0)
Chris Lattner2771e2c2006-03-25 06:12:06 +0000921 OpVal = N->getOperand(i);
922 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000923 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000924 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000925
Gabor Greiff304a7a2008-08-28 21:40:38 +0000926 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000927
Eli Friedman9c6ab1a2009-05-24 02:03:36 +0000928 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +0000929 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +0000930 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000931 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000932 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +0000933 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000934 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +0000935 }
936
937 // If the splat value is larger than the element value, then we can never do
938 // this splat. The only case that we could fit the replicated bits into our
939 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000940 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000941
Chris Lattner2771e2c2006-03-25 06:12:06 +0000942 // If the element value is larger than the splat value, cut it in half and
943 // check to see if the two halves are equal. Continue doing this until we
944 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
945 while (ValSizeInBytes > ByteSize) {
946 ValSizeInBytes >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000947
Chris Lattner2771e2c2006-03-25 06:12:06 +0000948 // If the top half equals the bottom half, we're still ok.
Chris Lattner39cc7172006-04-05 17:39:25 +0000949 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
950 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000951 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000952 }
953
954 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +0000955 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000956
Evan Chengb1ddc982006-03-26 09:52:32 +0000957 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000958 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000959
Chris Lattnerd71a1f92006-04-08 06:46:53 +0000960 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +0000961 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +0000962 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000963 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +0000964}
965
Chris Lattner4211ca92006-04-14 06:01:58 +0000966//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +0000967// Addressing Mode Selection
968//===----------------------------------------------------------------------===//
969
970/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
971/// or 64-bit immediate, and if the value can be accurately represented as a
972/// sign extension from a 16-bit value. If so, this returns true and the
973/// immediate.
974static bool isIntS16Immediate(SDNode *N, short &Imm) {
975 if (N->getOpcode() != ISD::Constant)
976 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000977
Dan Gohmaneffb8942008-09-12 16:56:44 +0000978 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000979 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000980 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +0000981 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000982 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +0000983}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000984static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000985 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +0000986}
987
988
989/// SelectAddressRegReg - Given the specified addressed, check to see if it
990/// can be represented as an indexed [r+r] operation. Returns false if it
991/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000992bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
993 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +0000994 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +0000995 short imm = 0;
996 if (N.getOpcode() == ISD::ADD) {
997 if (isIntS16Immediate(N.getOperand(1), imm))
998 return false; // r+i
999 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1000 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001001
Chris Lattnera801fced2006-11-08 02:15:41 +00001002 Base = N.getOperand(0);
1003 Index = N.getOperand(1);
1004 return true;
1005 } else if (N.getOpcode() == ISD::OR) {
1006 if (isIntS16Immediate(N.getOperand(1), imm))
1007 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001008
Chris Lattnera801fced2006-11-08 02:15:41 +00001009 // If this is an or of disjoint bitfields, we can codegen this as an add
1010 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1011 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001012 APInt LHSKnownZero, LHSKnownOne;
1013 APInt RHSKnownZero, RHSKnownOne;
1014 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001015 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001016
Dan Gohmanf19609a2008-02-27 01:23:58 +00001017 if (LHSKnownZero.getBoolValue()) {
1018 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001019 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001020 // If all of the bits are known zero on the LHS or RHS, the add won't
1021 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001022 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001023 Base = N.getOperand(0);
1024 Index = N.getOperand(1);
1025 return true;
1026 }
1027 }
1028 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001029
Chris Lattnera801fced2006-11-08 02:15:41 +00001030 return false;
1031}
1032
1033/// Returns true if the address N can be represented by a base register plus
1034/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001035/// represented as reg+reg. If Aligned is true, only accept displacements
1036/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001037bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001038 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001039 SelectionDAG &DAG,
1040 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001041 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001042 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001043 // If this can be more profitably realized as r+r, fail.
1044 if (SelectAddressRegReg(N, Disp, Base, DAG))
1045 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001046
Chris Lattnera801fced2006-11-08 02:15:41 +00001047 if (N.getOpcode() == ISD::ADD) {
1048 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001049 if (isIntS16Immediate(N.getOperand(1), imm) &&
1050 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001051 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001052 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1053 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1054 } else {
1055 Base = N.getOperand(0);
1056 }
1057 return true; // [r+i]
1058 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1059 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001060 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001061 && "Cannot handle constant offsets yet!");
1062 Disp = N.getOperand(1).getOperand(0); // The global address.
1063 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001064 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001065 Disp.getOpcode() == ISD::TargetConstantPool ||
1066 Disp.getOpcode() == ISD::TargetJumpTable);
1067 Base = N.getOperand(0);
1068 return true; // [&g+r]
1069 }
1070 } else if (N.getOpcode() == ISD::OR) {
1071 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001072 if (isIntS16Immediate(N.getOperand(1), imm) &&
1073 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001074 // If this is an or of disjoint bitfields, we can codegen this as an add
1075 // (for better address arithmetic) if the LHS and RHS of the OR are
1076 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001077 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001078 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001079
Dan Gohmanf19609a2008-02-27 01:23:58 +00001080 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001081 // If all of the bits are known zero on the LHS or RHS, the add won't
1082 // carry.
1083 Base = N.getOperand(0);
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001084 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001085 return true;
1086 }
1087 }
1088 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1089 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001090
Chris Lattnera801fced2006-11-08 02:15:41 +00001091 // If this address fits entirely in a 16-bit sext immediate field, codegen
1092 // this as "d, 0"
1093 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001094 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001095 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkelf70c41e2013-03-21 23:45:03 +00001096 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1097 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001098 return true;
1099 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001100
1101 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001102 if ((CN->getValueType(0) == MVT::i32 ||
1103 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1104 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001105 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001106
Chris Lattnera801fced2006-11-08 02:15:41 +00001107 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001108 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001109
Owen Anderson9f944592009-08-11 20:47:22 +00001110 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1111 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001112 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001113 return true;
1114 }
1115 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001116
Chris Lattnera801fced2006-11-08 02:15:41 +00001117 Disp = DAG.getTargetConstant(0, getPointerTy());
1118 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
1119 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1120 else
1121 Base = N;
1122 return true; // [r+0]
1123}
1124
1125/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1126/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001127bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1128 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001129 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001130 // Check to see if we can easily represent this as an [r+r] address. This
1131 // will fail if it thinks that the address is more profitably represented as
1132 // reg+imm, e.g. where imm = 0.
1133 if (SelectAddressRegReg(N, Base, Index, DAG))
1134 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001135
Chris Lattnera801fced2006-11-08 02:15:41 +00001136 // If the operand is an addition, always emit this as [r+r], since this is
1137 // better (for code size, and execution, as the memop does the add for free)
1138 // than emitting an explicit add.
1139 if (N.getOpcode() == ISD::ADD) {
1140 Base = N.getOperand(0);
1141 Index = N.getOperand(1);
1142 return true;
1143 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001144
Chris Lattnera801fced2006-11-08 02:15:41 +00001145 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkelf70c41e2013-03-21 23:45:03 +00001146 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1147 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001148 Index = N;
1149 return true;
1150}
1151
Chris Lattnera801fced2006-11-08 02:15:41 +00001152/// getPreIndexedAddressParts - returns true by value, base pointer and
1153/// offset pointer and addressing mode by reference if the node's address
1154/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001155bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1156 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001157 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001158 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001159 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001160
Ulrich Weigande90b0222013-03-22 14:58:48 +00001161 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001162 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001163 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001164 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001165 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1166 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001167 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001168 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001169 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001170 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001171 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001172 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001173 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001174 } else
1175 return false;
1176
Chris Lattner68371252006-11-14 01:38:31 +00001177 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands13237ac2008-06-06 12:08:01 +00001178 if (VT.isVector())
Chris Lattner68371252006-11-14 01:38:31 +00001179 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001180
Ulrich Weigande90b0222013-03-22 14:58:48 +00001181 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1182
1183 // Common code will reject creating a pre-inc form if the base pointer
1184 // is a frame index, or if N is a store and the base pointer is either
1185 // the same as or a predecessor of the value being stored. Check for
1186 // those situations here, and try with swapped Base/Offset instead.
1187 bool Swap = false;
1188
1189 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1190 Swap = true;
1191 else if (!isLoad) {
1192 SDValue Val = cast<StoreSDNode>(N)->getValue();
1193 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1194 Swap = true;
1195 }
1196
1197 if (Swap)
1198 std::swap(Base, Offset);
1199
Hal Finkelca542be2012-06-20 15:43:03 +00001200 AM = ISD::PRE_INC;
1201 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001202 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001203
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001204 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001205 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001206 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001207 return false;
1208 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001209 // LDU/STU need an address with at least 4-byte alignment.
1210 if (Alignment < 4)
1211 return false;
1212
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001213 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001214 return false;
1215 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001216
Chris Lattnerb314b152006-11-11 00:08:42 +00001217 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001218 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1219 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001220 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001221 LD->getExtensionType() == ISD::SEXTLOAD &&
1222 isa<ConstantSDNode>(Offset))
1223 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001224 }
1225
Chris Lattnerce645542006-11-10 02:08:47 +00001226 AM = ISD::PRE_INC;
1227 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001228}
1229
1230//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001231// LowerOperation implementation
1232//===----------------------------------------------------------------------===//
1233
Chris Lattneredb9d842010-11-15 02:46:57 +00001234/// GetLabelAccessInfo - Return true if we should reference labels using a
1235/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1236static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Chris Lattnerdd6df842010-11-15 03:13:19 +00001237 unsigned &LoOpFlags, const GlobalValue *GV = 0) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001238 HiOpFlags = PPCII::MO_HA;
1239 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001240
Chris Lattneredb9d842010-11-15 02:46:57 +00001241 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1242 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peck527da1b2010-11-23 03:31:01 +00001243 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattneredb9d842010-11-15 02:46:57 +00001244 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattnerdd6df842010-11-15 03:13:19 +00001245 if (isPIC) {
1246 HiOpFlags |= PPCII::MO_PIC_FLAG;
1247 LoOpFlags |= PPCII::MO_PIC_FLAG;
1248 }
1249
1250 // If this is a reference to a global value that requires a non-lazy-ptr, make
1251 // sure that instruction lowering adds it.
1252 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1253 HiOpFlags |= PPCII::MO_NLP_FLAG;
1254 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001255
Chris Lattnerdd6df842010-11-15 03:13:19 +00001256 if (GV->hasHiddenVisibility()) {
1257 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1258 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1259 }
1260 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001261
Chris Lattneredb9d842010-11-15 02:46:57 +00001262 return isPIC;
1263}
1264
1265static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1266 SelectionDAG &DAG) {
1267 EVT PtrVT = HiPart.getValueType();
1268 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001269 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001270
1271 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1272 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001273
Chris Lattneredb9d842010-11-15 02:46:57 +00001274 // With PIC, the first instruction is actually "GR+hi(&G)".
1275 if (isPIC)
1276 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1277 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001278
Chris Lattneredb9d842010-11-15 02:46:57 +00001279 // Generate non-pic code that has direct accesses to the constant pool.
1280 // The address of the global is just (hi(&g)+lo(&g)).
1281 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1282}
1283
Scott Michelcf0da6c2009-02-17 22:15:04 +00001284SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001285 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001286 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001287 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001288 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001289
Roman Divackyace47072012-08-24 16:26:02 +00001290 // 64-bit SVR4 ABI code is always position-independent.
1291 // The actual address of the GlobalValue is stored in the TOC.
1292 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1293 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001294 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001295 DAG.getRegister(PPC::X2, MVT::i64));
1296 }
1297
Chris Lattneredb9d842010-11-15 02:46:57 +00001298 unsigned MOHiFlag, MOLoFlag;
1299 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1300 SDValue CPIHi =
1301 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1302 SDValue CPILo =
1303 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1304 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001305}
1306
Dan Gohman21cea8a2010-04-17 15:26:15 +00001307SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001308 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001309 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001310
Roman Divackyace47072012-08-24 16:26:02 +00001311 // 64-bit SVR4 ABI code is always position-independent.
1312 // The actual address of the GlobalValue is stored in the TOC.
1313 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1314 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001315 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001316 DAG.getRegister(PPC::X2, MVT::i64));
1317 }
1318
Chris Lattneredb9d842010-11-15 02:46:57 +00001319 unsigned MOHiFlag, MOLoFlag;
1320 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1321 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1322 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1323 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001324}
1325
Dan Gohman21cea8a2010-04-17 15:26:15 +00001326SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1327 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001328 EVT PtrVT = Op.getValueType();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001329
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001330 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peck527da1b2010-11-23 03:31:01 +00001331
Chris Lattneredb9d842010-11-15 02:46:57 +00001332 unsigned MOHiFlag, MOLoFlag;
1333 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001334 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1335 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001336 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1337}
1338
Roman Divackye3f15c982012-06-04 17:36:38 +00001339SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1340 SelectionDAG &DAG) const {
1341
1342 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001343 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001344 const GlobalValue *GV = GA->getGlobal();
1345 EVT PtrVT = getPointerTy();
1346 bool is64bit = PPCSubTarget.isPPC64();
1347
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001348 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001349
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001350 if (Model == TLSModel::LocalExec) {
1351 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001352 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001353 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001354 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001355 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1356 is64bit ? MVT::i64 : MVT::i32);
1357 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1358 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1359 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001360
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001361 if (!is64bit)
1362 llvm_unreachable("only local-exec is currently supported for ppc32");
1363
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001364 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001365 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001366 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1367 PPCII::MO_TLS);
Bill Schmidt732eb912012-12-13 18:45:54 +00001368 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001369 SDValue TPOffsetHi = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1370 PtrVT, GOTReg, TGA);
1371 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
1372 PtrVT, TGA, TPOffsetHi);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001373 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001374 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001375
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001376 if (Model == TLSModel::GeneralDynamic) {
1377 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1378 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1379 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1380 GOTReg, TGA);
1381 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1382 GOTEntryHi, TGA);
1383
1384 // We need a chain node, and don't have one handy. The underlying
1385 // call has no side effects, so using the function entry node
1386 // suffices.
1387 SDValue Chain = DAG.getEntryNode();
1388 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1389 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1390 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1391 PtrVT, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001392 // The return value from GET_TLS_ADDR really is in X3 already, but
1393 // some hacks are needed here to tie everything together. The extra
1394 // copies dissolve during subsequent transforms.
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001395 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1396 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1397 }
1398
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001399 if (Model == TLSModel::LocalDynamic) {
1400 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1401 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1402 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1403 GOTReg, TGA);
1404 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1405 GOTEntryHi, TGA);
1406
1407 // We need a chain node, and don't have one handy. The underlying
1408 // call has no side effects, so using the function entry node
1409 // suffices.
1410 SDValue Chain = DAG.getEntryNode();
1411 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1412 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1413 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1414 PtrVT, ParmReg, TGA);
1415 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1416 // some hacks are needed here to tie everything together. The extra
1417 // copies dissolve during subsequent transforms.
1418 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1419 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt9ed4dbc2012-12-13 20:57:10 +00001420 Chain, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001421 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1422 }
1423
1424 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00001425}
1426
Chris Lattneredb9d842010-11-15 02:46:57 +00001427SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1428 SelectionDAG &DAG) const {
1429 EVT PtrVT = Op.getValueType();
1430 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001431 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00001432 const GlobalValue *GV = GSDN->getGlobal();
1433
Chris Lattneredb9d842010-11-15 02:46:57 +00001434 // 64-bit SVR4 ABI code is always position-independent.
1435 // The actual address of the GlobalValue is stored in the TOC.
1436 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1437 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1438 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1439 DAG.getRegister(PPC::X2, MVT::i64));
1440 }
1441
Chris Lattnerdd6df842010-11-15 03:13:19 +00001442 unsigned MOHiFlag, MOLoFlag;
1443 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00001444
Chris Lattnerdd6df842010-11-15 03:13:19 +00001445 SDValue GAHi =
1446 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1447 SDValue GALo =
1448 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00001449
Chris Lattnerdd6df842010-11-15 03:13:19 +00001450 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00001451
Chris Lattnerdd6df842010-11-15 03:13:19 +00001452 // If the global reference is actually to a non-lazy-pointer, we have to do an
1453 // extra load to get the address of the global.
1454 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1455 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001456 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00001457 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00001458}
1459
Dan Gohman21cea8a2010-04-17 15:26:15 +00001460SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00001461 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001462 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001463
Chris Lattner4211ca92006-04-14 06:01:58 +00001464 // If we're comparing for equality to zero, expose the fact that this is
1465 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1466 // fold the new nodes.
1467 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1468 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001469 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001470 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001471 if (VT.bitsLT(MVT::i32)) {
1472 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001473 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001474 }
Duncan Sands13237ac2008-06-06 12:08:01 +00001475 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001476 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1477 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00001478 DAG.getConstant(Log2b, MVT::i32));
1479 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00001480 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001481 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00001482 // optimized. FIXME: revisit this when we can custom lower all setcc
1483 // optimizations.
1484 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001485 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001486 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001487
Chris Lattner4211ca92006-04-14 06:01:58 +00001488 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00001489 // by xor'ing the rhs with the lhs, which is faster than setting a
1490 // condition register, reading it back out, and masking the correct bit. The
1491 // normal approach here uses sub to do this instead of xor. Using xor exposes
1492 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001493 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00001494 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001495 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001496 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00001497 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001498 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00001499 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001500 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001501}
1502
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001503SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001504 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00001505 SDNode *Node = Op.getNode();
1506 EVT VT = Node->getValueType(0);
1507 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1508 SDValue InChain = Node->getOperand(0);
1509 SDValue VAListPtr = Node->getOperand(1);
1510 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001511 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001512
Roman Divacky4394e682011-06-28 15:30:42 +00001513 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1514
1515 // gpr_index
1516 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1517 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1518 false, false, 0);
1519 InChain = GprIndex.getValue(1);
1520
1521 if (VT == MVT::i64) {
1522 // Check if GprIndex is even
1523 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1524 DAG.getConstant(1, MVT::i32));
1525 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1526 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1527 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1528 DAG.getConstant(1, MVT::i32));
1529 // Align GprIndex to be even if it isn't
1530 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1531 GprIndex);
1532 }
1533
1534 // fpr index is 1 byte after gpr
1535 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1536 DAG.getConstant(1, MVT::i32));
1537
1538 // fpr
1539 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1540 FprPtr, MachinePointerInfo(SV), MVT::i8,
1541 false, false, 0);
1542 InChain = FprIndex.getValue(1);
1543
1544 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1545 DAG.getConstant(8, MVT::i32));
1546
1547 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1548 DAG.getConstant(4, MVT::i32));
1549
1550 // areas
1551 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001552 MachinePointerInfo(), false, false,
1553 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001554 InChain = OverflowArea.getValue(1);
1555
1556 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001557 MachinePointerInfo(), false, false,
1558 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001559 InChain = RegSaveArea.getValue(1);
1560
1561 // select overflow_area if index > 8
1562 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1563 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1564
Roman Divacky4394e682011-06-28 15:30:42 +00001565 // adjustment constant gpr_index * 4/8
1566 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1567 VT.isInteger() ? GprIndex : FprIndex,
1568 DAG.getConstant(VT.isInteger() ? 4 : 8,
1569 MVT::i32));
1570
1571 // OurReg = RegSaveArea + RegConstant
1572 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1573 RegConstant);
1574
1575 // Floating types are 32 bytes into RegSaveArea
1576 if (VT.isFloatingPoint())
1577 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1578 DAG.getConstant(32, MVT::i32));
1579
1580 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1581 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1582 VT.isInteger() ? GprIndex : FprIndex,
1583 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1584 MVT::i32));
1585
1586 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1587 VT.isInteger() ? VAListPtr : FprPtr,
1588 MachinePointerInfo(SV),
1589 MVT::i8, false, false, 0);
1590
1591 // determine if we should load from reg_save_area or overflow_area
1592 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1593
1594 // increase overflow_area by 4/8 if gpr/fpr > 8
1595 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1596 DAG.getConstant(VT.isInteger() ? 4 : 8,
1597 MVT::i32));
1598
1599 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1600 OverflowAreaPlusN);
1601
1602 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1603 OverflowAreaPtr,
1604 MachinePointerInfo(),
1605 MVT::i32, false, false, 0);
1606
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00001607 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001608 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001609}
1610
Duncan Sandsa0984362011-09-06 13:37:06 +00001611SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1612 SelectionDAG &DAG) const {
1613 return Op.getOperand(0);
1614}
1615
1616SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1617 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00001618 SDValue Chain = Op.getOperand(0);
1619 SDValue Trmp = Op.getOperand(1); // trampoline
1620 SDValue FPtr = Op.getOperand(2); // nested function
1621 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00001622 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00001623
Owen Anderson53aa7a92009-08-10 22:56:29 +00001624 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00001625 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00001626 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001627 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00001628 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00001629
Scott Michelcf0da6c2009-02-17 22:15:04 +00001630 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00001631 TargetLowering::ArgListEntry Entry;
1632
1633 Entry.Ty = IntPtrTy;
1634 Entry.Node = Trmp; Args.push_back(Entry);
1635
1636 // TrampSize == (isPPC64 ? 48 : 40);
1637 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00001638 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00001639 Args.push_back(Entry);
1640
1641 Entry.Node = FPtr; Args.push_back(Entry);
1642 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001643
Bill Wendling95e1af22008-09-17 00:30:57 +00001644 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Justin Holewinskiaa583972012-05-25 16:35:28 +00001645 TargetLowering::CallLoweringInfo CLI(Chain,
1646 Type::getVoidTy(*DAG.getContext()),
1647 false, false, false, false, 0,
1648 CallingConv::C,
Evan Cheng65f9d192012-02-28 18:51:51 +00001649 /*isTailCall=*/false,
Justin Holewinskiaa583972012-05-25 16:35:28 +00001650 /*doesNotRet=*/false,
1651 /*isReturnValueUsed=*/true,
Bill Wendling95e1af22008-09-17 00:30:57 +00001652 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00001653 Args, DAG, dl);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001654 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bill Wendling95e1af22008-09-17 00:30:57 +00001655
Duncan Sandsa0984362011-09-06 13:37:06 +00001656 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00001657}
1658
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001659SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001660 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001661 MachineFunction &MF = DAG.getMachineFunction();
1662 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1663
Andrew Trickef9de2a2013-05-25 02:42:55 +00001664 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001665
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001666 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001667 // vastart just stores the address of the VarArgsFrameIndex slot into the
1668 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001669 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00001670 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001671 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00001672 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1673 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00001674 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001675 }
1676
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001677 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001678 // We suppose the given va_list is already allocated.
1679 //
1680 // typedef struct {
1681 // char gpr; /* index into the array of 8 GPRs
1682 // * stored in the register save area
1683 // * gpr=0 corresponds to r3,
1684 // * gpr=1 to r4, etc.
1685 // */
1686 // char fpr; /* index into the array of 8 FPRs
1687 // * stored in the register save area
1688 // * fpr=0 corresponds to f1,
1689 // * fpr=1 to f2, etc.
1690 // */
1691 // char *overflow_arg_area;
1692 // /* location on stack that holds
1693 // * the next overflow argument
1694 // */
1695 // char *reg_save_area;
1696 // /* where r3:r10 and f1:f8 (if saved)
1697 // * are stored
1698 // */
1699 // } va_list[1];
1700
1701
Dan Gohman31ae5862010-04-17 14:41:14 +00001702 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1703 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001704
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001705
Owen Anderson53aa7a92009-08-10 22:56:29 +00001706 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001707
Dan Gohman31ae5862010-04-17 14:41:14 +00001708 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1709 PtrVT);
1710 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1711 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001712
Duncan Sands13237ac2008-06-06 12:08:01 +00001713 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001714 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001715
Duncan Sands13237ac2008-06-06 12:08:01 +00001716 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001717 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001718
1719 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001720 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001721
Dan Gohman2d489b52008-02-06 22:27:42 +00001722 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001723
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001724 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001725 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00001726 Op.getOperand(1),
1727 MachinePointerInfo(SV),
1728 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001729 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001730 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001731 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001732
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001733 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001734 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00001735 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1736 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00001737 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001738 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001739 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001740
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001741 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001742 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00001743 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1744 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001745 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001746 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001747 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001748
1749 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00001750 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1751 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001752 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001753
Chris Lattner4211ca92006-04-14 06:01:58 +00001754}
1755
Chris Lattner4f2e4e02007-03-06 00:59:59 +00001756#include "PPCGenCallingConv.inc"
1757
Bill Schmidt230b4512013-06-12 16:39:22 +00001758bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1759 CCValAssign::LocInfo &LocInfo,
1760 ISD::ArgFlagsTy &ArgFlags,
1761 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001762 return true;
1763}
1764
Bill Schmidt230b4512013-06-12 16:39:22 +00001765bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
1766 MVT &LocVT,
1767 CCValAssign::LocInfo &LocInfo,
1768 ISD::ArgFlagsTy &ArgFlags,
1769 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00001770 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001771 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1772 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1773 };
1774 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00001775
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001776 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1777
1778 // Skip one register if the first unallocated register has an even register
1779 // number and there are still argument registers available which have not been
1780 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1781 // need to skip a register if RegNum is odd.
1782 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1783 State.AllocateReg(ArgRegs[RegNum]);
1784 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001785
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001786 // Always return false here, as this function only makes sure that the first
1787 // unallocated register has an odd register number and does not actually
1788 // allocate a register for the current argument.
1789 return false;
1790}
1791
Bill Schmidt230b4512013-06-12 16:39:22 +00001792bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
1793 MVT &LocVT,
1794 CCValAssign::LocInfo &LocInfo,
1795 ISD::ArgFlagsTy &ArgFlags,
1796 CCState &State) {
Craig Topperbef78fc2012-03-11 07:57:25 +00001797 static const uint16_t ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001798 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1799 PPC::F8
1800 };
1801
1802 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00001803
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001804 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1805
1806 // If there is only one Floating-point register left we need to put both f64
1807 // values of a split ppc_fp128 value on the stack.
1808 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1809 State.AllocateReg(ArgRegs[RegNum]);
1810 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001811
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001812 // Always return false here, as this function only makes sure that the two f64
1813 // values a ppc_fp128 value is split into are both passed in registers or both
1814 // passed on the stack and does not actually allocate a register for the
1815 // current argument.
1816 return false;
1817}
1818
Chris Lattner43df5b32007-02-25 05:34:32 +00001819/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001820/// on Darwin.
Craig Topperca658c22012-03-11 07:16:55 +00001821static const uint16_t *GetFPR() {
1822 static const uint16_t FPR[] = {
Chris Lattner43df5b32007-02-25 05:34:32 +00001823 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001824 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner43df5b32007-02-25 05:34:32 +00001825 };
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001826
Chris Lattner43df5b32007-02-25 05:34:32 +00001827 return FPR;
1828}
1829
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00001830/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1831/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001832static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00001833 unsigned PtrByteSize) {
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00001834 unsigned ArgSize = ArgVT.getSizeInBits()/8;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00001835 if (Flags.isByVal())
1836 ArgSize = Flags.getByValSize();
1837 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1838
1839 return ArgSize;
1840}
1841
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001842SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001843PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001844 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001845 const SmallVectorImpl<ISD::InputArg>
1846 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001847 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001848 SmallVectorImpl<SDValue> &InVals)
1849 const {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00001850 if (PPCSubTarget.isSVR4ABI()) {
1851 if (PPCSubTarget.isPPC64())
1852 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
1853 dl, DAG, InVals);
1854 else
1855 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
1856 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00001857 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00001858 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
1859 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001860 }
1861}
1862
1863SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00001864PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001865 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001866 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001867 const SmallVectorImpl<ISD::InputArg>
1868 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001869 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001870 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001871
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001872 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001873 // +-----------------------------------+
1874 // +--> | Back chain |
1875 // | +-----------------------------------+
1876 // | | Floating-point register save area |
1877 // | +-----------------------------------+
1878 // | | General register save area |
1879 // | +-----------------------------------+
1880 // | | CR save word |
1881 // | +-----------------------------------+
1882 // | | VRSAVE save word |
1883 // | +-----------------------------------+
1884 // | | Alignment padding |
1885 // | +-----------------------------------+
1886 // | | Vector register save area |
1887 // | +-----------------------------------+
1888 // | | Local variable space |
1889 // | +-----------------------------------+
1890 // | | Parameter list area |
1891 // | +-----------------------------------+
1892 // | | LR save word |
1893 // | +-----------------------------------+
1894 // SP--> +--- | Back chain |
1895 // +-----------------------------------+
1896 //
1897 // Specifications:
1898 // System V Application Binary Interface PowerPC Processor Supplement
1899 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00001900
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001901 MachineFunction &MF = DAG.getMachineFunction();
1902 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00001903 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001904
Owen Anderson53aa7a92009-08-10 22:56:29 +00001905 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001906 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001907 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
1908 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001909 unsigned PtrByteSize = 4;
1910
1911 // Assign locations to all of the incoming arguments.
1912 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001913 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00001914 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001915
1916 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001917 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001918
Bill Schmidtef17c142013-02-06 17:33:58 +00001919 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00001920
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001921 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1922 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00001923
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001924 // Arguments stored in registers.
1925 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00001926 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001927 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00001928
Owen Anderson9f944592009-08-11 20:47:22 +00001929 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001930 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001931 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Owen Anderson9f944592009-08-11 20:47:22 +00001932 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00001933 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001934 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001935 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00001936 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001937 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001938 case MVT::f64:
Craig Topperabadc662012-04-20 06:31:50 +00001939 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001940 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001941 case MVT::v16i8:
1942 case MVT::v8i16:
1943 case MVT::v4i32:
1944 case MVT::v4f32:
Craig Topperabadc662012-04-20 06:31:50 +00001945 RC = &PPC::VRRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001946 break;
1947 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001948
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001949 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00001950 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001951 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, ValVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001952
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001953 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001954 } else {
1955 // Argument stored in memory.
1956 assert(VA.isMemLoc());
1957
1958 unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8;
1959 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00001960 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001961
1962 // Create load nodes to retrieve arguments from the stack.
1963 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00001964 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1965 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001966 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001967 }
1968 }
1969
1970 // Assign locations to all of the incoming aggregate by value arguments.
1971 // Aggregates passed by value are stored in the local variable space of the
1972 // caller's stack frame, right above the parameter list area.
1973 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001974 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00001975 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001976
1977 // Reserve stack space for the allocations in CCInfo.
1978 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
1979
Bill Schmidtef17c142013-02-06 17:33:58 +00001980 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001981
1982 // Area that is at least reserved in the caller of this function.
1983 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00001984
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001985 // Set the size that is at least reserved in caller of this function. Tail
1986 // call optimized function's reserved stack space needs to be aligned so that
1987 // taking the difference between two stack areas will result in an aligned
1988 // stack.
1989 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1990
1991 MinReservedArea =
1992 std::max(MinReservedArea,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001993 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peck527da1b2010-11-23 03:31:01 +00001994
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001995 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001996 getStackAlignment();
1997 unsigned AlignMask = TargetAlign-1;
1998 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peck527da1b2010-11-23 03:31:01 +00001999
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002000 FI->setMinReservedArea(MinReservedArea);
2001
2002 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002003
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002004 // If the function takes variable number of arguments, make a frame index for
2005 // the start of the first vararg value... for expansion of llvm.va_start.
2006 if (isVarArg) {
Craig Topperbef78fc2012-03-11 07:57:25 +00002007 static const uint16_t GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002008 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2009 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2010 };
2011 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2012
Craig Topperbef78fc2012-03-11 07:57:25 +00002013 static const uint16_t FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002014 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2015 PPC::F8
2016 };
2017 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2018
Dan Gohman31ae5862010-04-17 14:41:14 +00002019 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2020 NumGPArgRegs));
2021 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2022 NumFPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002023
2024 // Make room for NumGPArgRegs and NumFPArgRegs.
2025 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson9f944592009-08-11 20:47:22 +00002026 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002027
Dan Gohman31ae5862010-04-17 14:41:14 +00002028 FuncInfo->setVarArgsStackOffset(
2029 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002030 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002031
Dan Gohman31ae5862010-04-17 14:41:14 +00002032 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2033 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002034
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002035 // The fixed integer arguments of a variadic function are stored to the
2036 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2037 // the result of va_next.
2038 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2039 // Get an existing live-in vreg, or add a new one.
2040 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2041 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002042 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002043
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002044 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002045 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2046 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002047 MemOps.push_back(Store);
2048 // Increment the address by four for the next argument to store
2049 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2050 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2051 }
2052
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002053 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2054 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002055 // The double arguments are stored to the VarArgsFrameIndex
2056 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002057 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2058 // Get an existing live-in vreg, or add a new one.
2059 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2060 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002061 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002062
Owen Anderson9f944592009-08-11 20:47:22 +00002063 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002064 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2065 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002066 MemOps.push_back(Store);
2067 // Increment the address by eight for the next argument to store
Owen Anderson9f944592009-08-11 20:47:22 +00002068 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002069 PtrVT);
2070 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2071 }
2072 }
2073
2074 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002075 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002076 MVT::Other, &MemOps[0], MemOps.size());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002077
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002078 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002079}
2080
Bill Schmidt57d6de52012-10-23 15:51:16 +00002081// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2082// value to MVT::i64 and then truncate to the correct register size.
2083SDValue
2084PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2085 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002086 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002087 if (Flags.isSExt())
2088 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2089 DAG.getValueType(ObjectVT));
2090 else if (Flags.isZExt())
2091 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2092 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002093
Bill Schmidt57d6de52012-10-23 15:51:16 +00002094 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
2095}
2096
2097// Set the size that is at least reserved in caller of this function. Tail
2098// call optimized functions' reserved stack space needs to be aligned so that
2099// taking the difference between two stack areas will result in an aligned
2100// stack.
2101void
2102PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2103 unsigned nAltivecParamsAtEnd,
2104 unsigned MinReservedArea,
2105 bool isPPC64) const {
2106 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2107 // Add the Altivec parameters at the end, if needed.
2108 if (nAltivecParamsAtEnd) {
2109 MinReservedArea = ((MinReservedArea+15)/16)*16;
2110 MinReservedArea += 16*nAltivecParamsAtEnd;
2111 }
2112 MinReservedArea =
2113 std::max(MinReservedArea,
2114 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2115 unsigned TargetAlign
2116 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2117 getStackAlignment();
2118 unsigned AlignMask = TargetAlign-1;
2119 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2120 FI->setMinReservedArea(MinReservedArea);
2121}
2122
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002123SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002124PPCTargetLowering::LowerFormalArguments_64SVR4(
2125 SDValue Chain,
2126 CallingConv::ID CallConv, bool isVarArg,
2127 const SmallVectorImpl<ISD::InputArg>
2128 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002129 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002130 SmallVectorImpl<SDValue> &InVals) const {
2131 // TODO: add description of PPC stack frame format, or at least some docs.
2132 //
2133 MachineFunction &MF = DAG.getMachineFunction();
2134 MachineFrameInfo *MFI = MF.getFrameInfo();
2135 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2136
2137 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2138 // Potential tail calls could cause overwriting of argument stack slots.
2139 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2140 (CallConv == CallingConv::Fast));
2141 unsigned PtrByteSize = 8;
2142
2143 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2144 // Area that is at least reserved in caller of this function.
2145 unsigned MinReservedArea = ArgOffset;
2146
2147 static const uint16_t GPR[] = {
2148 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2149 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2150 };
2151
2152 static const uint16_t *FPR = GetFPR();
2153
2154 static const uint16_t VR[] = {
2155 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2156 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2157 };
2158
2159 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2160 const unsigned Num_FPR_Regs = 13;
2161 const unsigned Num_VR_Regs = array_lengthof(VR);
2162
2163 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2164
2165 // Add DAG nodes to load the arguments or copy them out of registers. On
2166 // entry to a function on PPC, the arguments start after the linkage area,
2167 // although the first ones are often in registers.
2168
2169 SmallVector<SDValue, 8> MemOps;
2170 unsigned nAltivecParamsAtEnd = 0;
2171 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002172 unsigned CurArgIdx = 0;
2173 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002174 SDValue ArgVal;
2175 bool needsLoad = false;
2176 EVT ObjectVT = Ins[ArgNo].VT;
2177 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
2178 unsigned ArgSize = ObjSize;
2179 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt6631e942013-02-20 17:31:41 +00002180 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2181 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002182
2183 unsigned CurArgOffset = ArgOffset;
2184
2185 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2186 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2187 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
2188 if (isVarArg) {
2189 MinReservedArea = ((MinReservedArea+15)/16)*16;
2190 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2191 Flags,
2192 PtrByteSize);
2193 } else
2194 nAltivecParamsAtEnd++;
2195 } else
2196 // Calculate min reserved area.
2197 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2198 Flags,
2199 PtrByteSize);
2200
2201 // FIXME the codegen can be much improved in some cases.
2202 // We do not have to keep everything in memory.
2203 if (Flags.isByVal()) {
2204 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2205 ObjSize = Flags.getByValSize();
2206 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002207 // Empty aggregate parameters do not take up registers. Examples:
2208 // struct { } a;
2209 // union { } b;
2210 // int c[0];
2211 // etc. However, we have to provide a place-holder in InVals, so
2212 // pretend we have an 8-byte item at the current address for that
2213 // purpose.
2214 if (!ObjSize) {
2215 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2216 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2217 InVals.push_back(FIN);
2218 continue;
2219 }
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002220 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt48081ca2012-10-16 13:30:53 +00002221 if (ObjSize < PtrByteSize)
2222 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002223 // The value of the object is its address.
2224 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2225 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2226 InVals.push_back(FIN);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002227
2228 if (ObjSize < 8) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002229 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002230 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002231 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002232 SDValue Store;
2233
2234 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2235 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2236 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2237 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
2238 MachinePointerInfo(FuncArg, CurArgOffset),
2239 ObjType, false, false, 0);
2240 } else {
2241 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2242 // store the whole register as-is to the parameter save area
2243 // slot. The address of the parameter was already calculated
2244 // above (InVals.push_back(FIN)) to be the right-justified
2245 // offset within the slot. For this store, we need a new
2246 // frame index that points at the beginning of the slot.
2247 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2248 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2249 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2250 MachinePointerInfo(FuncArg, ArgOffset),
2251 false, false, 0);
2252 }
2253
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002254 MemOps.push_back(Store);
2255 ++GPR_idx;
2256 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002257 // Whether we copied from a register or not, advance the offset
2258 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002259 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002260 continue;
2261 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002262
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002263 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2264 // Store whatever pieces of the object are in registers
2265 // to memory. ArgOffset will be the address of the beginning
2266 // of the object.
2267 if (GPR_idx != Num_GPR_Regs) {
2268 unsigned VReg;
2269 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2270 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2271 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2272 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002273 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002274 MachinePointerInfo(FuncArg, ArgOffset),
2275 false, false, 0);
2276 MemOps.push_back(Store);
2277 ++GPR_idx;
2278 ArgOffset += PtrByteSize;
2279 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00002280 ArgOffset += ArgSize - j;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002281 break;
2282 }
2283 }
2284 continue;
2285 }
2286
2287 switch (ObjectVT.getSimpleVT().SimpleTy) {
2288 default: llvm_unreachable("Unhandled argument type!");
2289 case MVT::i32:
2290 case MVT::i64:
2291 if (GPR_idx != Num_GPR_Regs) {
2292 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2293 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2294
Bill Schmidt57d6de52012-10-23 15:51:16 +00002295 if (ObjectVT == MVT::i32)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002296 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2297 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002298 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002299
2300 ++GPR_idx;
2301 } else {
2302 needsLoad = true;
2303 ArgSize = PtrByteSize;
2304 }
2305 ArgOffset += 8;
2306 break;
2307
2308 case MVT::f32:
2309 case MVT::f64:
2310 // Every 8 bytes of argument space consumes one of the GPRs available for
2311 // argument passing.
2312 if (GPR_idx != Num_GPR_Regs) {
2313 ++GPR_idx;
2314 }
2315 if (FPR_idx != Num_FPR_Regs) {
2316 unsigned VReg;
2317
2318 if (ObjectVT == MVT::f32)
2319 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2320 else
2321 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
2322
2323 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2324 ++FPR_idx;
2325 } else {
2326 needsLoad = true;
Bill Schmidt22162472012-10-11 15:38:20 +00002327 ArgSize = PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002328 }
2329
2330 ArgOffset += 8;
2331 break;
2332 case MVT::v4f32:
2333 case MVT::v4i32:
2334 case MVT::v8i16:
2335 case MVT::v16i8:
2336 // Note that vector arguments in registers don't reserve stack space,
2337 // except in varargs functions.
2338 if (VR_idx != Num_VR_Regs) {
2339 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
2340 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2341 if (isVarArg) {
2342 while ((ArgOffset % 16) != 0) {
2343 ArgOffset += PtrByteSize;
2344 if (GPR_idx != Num_GPR_Regs)
2345 GPR_idx++;
2346 }
2347 ArgOffset += 16;
2348 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2349 }
2350 ++VR_idx;
2351 } else {
2352 // Vectors are aligned.
2353 ArgOffset = ((ArgOffset+15)/16)*16;
2354 CurArgOffset = ArgOffset;
2355 ArgOffset += 16;
2356 needsLoad = true;
2357 }
2358 break;
2359 }
2360
2361 // We need to load the argument to a virtual register if we determined
2362 // above that we ran out of physical registers of the appropriate type.
2363 if (needsLoad) {
2364 int FI = MFI->CreateFixedObject(ObjSize,
2365 CurArgOffset + (ArgSize - ObjSize),
2366 isImmutable);
2367 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2368 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2369 false, false, false, 0);
2370 }
2371
2372 InVals.push_back(ArgVal);
2373 }
2374
2375 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002376 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002377 // taking the difference between two stack areas will result in an aligned
2378 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002379 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002380
2381 // If the function takes variable number of arguments, make a frame index for
2382 // the start of the first vararg value... for expansion of llvm.va_start.
2383 if (isVarArg) {
2384 int Depth = ArgOffset;
2385
2386 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00002387 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002388 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2389
2390 // If this function is vararg, store any remaining integer argument regs
2391 // to their spots on the stack so that they may be loaded by deferencing the
2392 // result of va_next.
2393 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2394 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2395 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2396 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2397 MachinePointerInfo(), false, false, 0);
2398 MemOps.push_back(Store);
2399 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00002400 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002401 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2402 }
2403 }
2404
2405 if (!MemOps.empty())
2406 Chain = DAG.getNode(ISD::TokenFactor, dl,
2407 MVT::Other, &MemOps[0], MemOps.size());
2408
2409 return Chain;
2410}
2411
2412SDValue
2413PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002414 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002415 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002416 const SmallVectorImpl<ISD::InputArg>
2417 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002418 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002419 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002420 // TODO: add description of PPC stack frame format, or at least some docs.
2421 //
2422 MachineFunction &MF = DAG.getMachineFunction();
2423 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002424 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002425
Owen Anderson53aa7a92009-08-10 22:56:29 +00002426 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002427 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002428 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002429 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2430 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00002431 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey48850c12006-11-16 22:43:37 +00002432
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002433 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002434 // Area that is at least reserved in caller of this function.
2435 unsigned MinReservedArea = ArgOffset;
2436
Craig Topperca658c22012-03-11 07:16:55 +00002437 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002438 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2439 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2440 };
Craig Topperca658c22012-03-11 07:16:55 +00002441 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00002442 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2443 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2444 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00002445
Craig Topperca658c22012-03-11 07:16:55 +00002446 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002447
Craig Topperca658c22012-03-11 07:16:55 +00002448 static const uint16_t VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002449 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2450 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2451 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00002452
Owen Andersone2f23a32007-09-07 04:06:50 +00002453 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002454 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00002455 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00002456
2457 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002458
Craig Topperca658c22012-03-11 07:16:55 +00002459 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002460
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002461 // In 32-bit non-varargs functions, the stack space for vectors is after the
2462 // stack space for non-vectors. We do not use this space unless we have
2463 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00002464 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002465 // that out...for the pathological case, compute VecArgOffset as the
2466 // start of the vector parameter area. Computing VecArgOffset is the
2467 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002468 unsigned VecArgOffset = ArgOffset;
2469 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002470 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002471 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002472 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002473 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002474
Duncan Sandsd97eea32008-03-21 09:14:45 +00002475 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002476 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00002477 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002478 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002479 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2480 VecArgOffset += ArgSize;
2481 continue;
2482 }
2483
Owen Anderson9f944592009-08-11 20:47:22 +00002484 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002485 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002486 case MVT::i32:
2487 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002488 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002489 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002490 case MVT::i64: // PPC64
2491 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002492 // FIXME: We are guaranteed to be !isPPC64 at this point.
2493 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002494 VecArgOffset += 8;
2495 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002496 case MVT::v4f32:
2497 case MVT::v4i32:
2498 case MVT::v8i16:
2499 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002500 // Nothing to do, we're only looking at Nonvector args here.
2501 break;
2502 }
2503 }
2504 }
2505 // We've found where the vector parameter area in memory is. Skip the
2506 // first 12 parameters; these don't use that memory.
2507 VecArgOffset = ((VecArgOffset+15)/16)*16;
2508 VecArgOffset += 12*16;
2509
Chris Lattner4302e8f2006-05-16 18:18:50 +00002510 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00002511 // entry to a function on PPC, the arguments start after the linkage area,
2512 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00002513
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002514 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002515 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00002516 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002517 unsigned CurArgIdx = 0;
2518 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002519 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002520 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002521 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00002522 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00002523 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002524 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002525 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2526 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002527
Chris Lattner318f0d22006-05-16 18:51:52 +00002528 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002529
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002530 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002531 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2532 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002533 if (isVarArg || isPPC64) {
2534 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002535 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002536 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002537 PtrByteSize);
2538 } else nAltivecParamsAtEnd++;
2539 } else
2540 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002541 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002542 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002543 PtrByteSize);
2544
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002545 // FIXME the codegen can be much improved in some cases.
2546 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002547 if (Flags.isByVal()) {
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002548 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002549 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002550 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002551 // Objects of size 1 and 2 are right justified, everything else is
2552 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00002553 if (ObjSize==1 || ObjSize==2) {
2554 CurArgOffset = CurArgOffset + (4 - ObjSize);
2555 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002556 // The value of the object is its address.
Evan Cheng0664a672010-07-03 00:40:23 +00002557 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002558 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002559 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002560 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00002561 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002562 unsigned VReg;
2563 if (isPPC64)
2564 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2565 else
2566 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002567 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002568 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002569 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Roman Divackyca103892012-09-24 20:47:19 +00002570 MachinePointerInfo(FuncArg,
2571 CurArgOffset),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002572 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00002573 MemOps.push_back(Store);
2574 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00002575 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002576
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002577 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00002578
Dale Johannesen21a8f142008-03-08 01:41:42 +00002579 continue;
2580 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002581 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2582 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002583 // to memory. ArgOffset will be the address of the beginning
2584 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002585 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002586 unsigned VReg;
2587 if (isPPC64)
2588 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2589 else
2590 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00002591 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002592 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002593 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002594 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Roman Divackyca103892012-09-24 20:47:19 +00002595 MachinePointerInfo(FuncArg, ArgOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002596 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002597 MemOps.push_back(Store);
2598 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002599 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002600 } else {
2601 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2602 break;
2603 }
2604 }
2605 continue;
2606 }
2607
Owen Anderson9f944592009-08-11 20:47:22 +00002608 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002609 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002610 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00002611 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00002612 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002613 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002614 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Bill Wendling968f32c2008-03-07 20:49:02 +00002615 ++GPR_idx;
2616 } else {
2617 needsLoad = true;
2618 ArgSize = PtrByteSize;
2619 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002620 // All int arguments reserve stack space in the Darwin ABI.
2621 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00002622 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002623 }
Bill Wendling968f32c2008-03-07 20:49:02 +00002624 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00002625 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00002626 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002627 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002628 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00002629
Bill Schmidt57d6de52012-10-23 15:51:16 +00002630 if (ObjectVT == MVT::i32)
Bill Wendling968f32c2008-03-07 20:49:02 +00002631 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00002632 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002633 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00002634
Chris Lattnerec78cad2006-06-26 22:48:35 +00002635 ++GPR_idx;
2636 } else {
2637 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00002638 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002639 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002640 // All int arguments reserve stack space in the Darwin ABI.
2641 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002642 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002643
Owen Anderson9f944592009-08-11 20:47:22 +00002644 case MVT::f32:
2645 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00002646 // Every 4 bytes of argument space consumes one of the GPRs available for
2647 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002648 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002649 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00002650 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002651 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00002652 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002653 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002654 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002655
Owen Anderson9f944592009-08-11 20:47:22 +00002656 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00002657 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002658 else
Devang Patelf3292b22011-02-21 23:21:26 +00002659 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002660
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002661 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002662 ++FPR_idx;
2663 } else {
2664 needsLoad = true;
2665 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002666
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002667 // All FP arguments reserve stack space in the Darwin ABI.
2668 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002669 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002670 case MVT::v4f32:
2671 case MVT::v4i32:
2672 case MVT::v8i16:
2673 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00002674 // Note that vector arguments in registers don't reserve stack space,
2675 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002676 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002677 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002678 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00002679 if (isVarArg) {
2680 while ((ArgOffset % 16) != 0) {
2681 ArgOffset += PtrByteSize;
2682 if (GPR_idx != Num_GPR_Regs)
2683 GPR_idx++;
2684 }
2685 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002686 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00002687 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002688 ++VR_idx;
2689 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002690 if (!isVarArg && !isPPC64) {
2691 // Vectors go after all the nonvectors.
2692 CurArgOffset = VecArgOffset;
2693 VecArgOffset += 16;
2694 } else {
2695 // Vectors are aligned.
2696 ArgOffset = ((ArgOffset+15)/16)*16;
2697 CurArgOffset = ArgOffset;
2698 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00002699 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002700 needsLoad = true;
2701 }
2702 break;
2703 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002704
Chris Lattner4302e8f2006-05-16 18:18:50 +00002705 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002706 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002707 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002708 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002709 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00002710 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002711 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002712 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002713 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002714 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002715
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002716 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002717 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002718
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002719 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002720 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002721 // taking the difference between two stack areas will result in an aligned
2722 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002723 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002724
Chris Lattner4302e8f2006-05-16 18:18:50 +00002725 // If the function takes variable number of arguments, make a frame index for
2726 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002727 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002728 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002729
Dan Gohman31ae5862010-04-17 14:41:14 +00002730 FuncInfo->setVarArgsFrameIndex(
2731 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002732 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00002733 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002734
Chris Lattner4302e8f2006-05-16 18:18:50 +00002735 // If this function is vararg, store any remaining integer argument regs
2736 // to their spots on the stack so that they may be loaded by deferencing the
2737 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002738 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00002739 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00002740
Chris Lattner2cca3852006-11-18 01:57:19 +00002741 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00002742 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00002743 else
Devang Patelf3292b22011-02-21 23:21:26 +00002744 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00002745
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002746 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002747 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2748 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002749 MemOps.push_back(Store);
2750 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002751 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00002752 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002753 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002754 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002755
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002756 if (!MemOps.empty())
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002757 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002758 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002759
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002760 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002761}
2762
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002763/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
2764/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002765static unsigned
2766CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2767 bool isPPC64,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002768 bool isVarArg,
2769 unsigned CC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002770 const SmallVectorImpl<ISD::OutputArg>
2771 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002772 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002773 unsigned &nAltivecParamsAtEnd) {
2774 // Count how many bytes are to be pushed on the stack, including the linkage
2775 // area, and parameter passing area. We start with 24/48 bytes, which is
2776 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002777 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002778 unsigned NumOps = Outs.size();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002779 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2780
2781 // Add up all the space actually used.
2782 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2783 // they all go in registers, but we must reserve stack space for them for
2784 // possible use by the caller. In varargs or 64-bit calls, parameters are
2785 // assigned stack space in order, with padding so Altivec parameters are
2786 // 16-byte aligned.
2787 nAltivecParamsAtEnd = 0;
2788 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002789 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002790 EVT ArgVT = Outs[i].VT;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002791 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002792 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2793 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002794 if (!isVarArg && !isPPC64) {
2795 // Non-varargs Altivec parameters go after all the non-Altivec
2796 // parameters; handle those later so we know how much padding we need.
2797 nAltivecParamsAtEnd++;
2798 continue;
2799 }
2800 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2801 NumBytes = ((NumBytes+15)/16)*16;
2802 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002803 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002804 }
2805
2806 // Allow for Altivec parameters at the end, if needed.
2807 if (nAltivecParamsAtEnd) {
2808 NumBytes = ((NumBytes+15)/16)*16;
2809 NumBytes += 16*nAltivecParamsAtEnd;
2810 }
2811
2812 // The prolog code of the callee may store up to 8 GPR argument registers to
2813 // the stack, allowing va_start to index over them in memory if its varargs.
2814 // Because we cannot tell if this is needed on the caller side, we have to
2815 // conservatively assume that it is needed. As such, make sure we have at
2816 // least enough stack space for the caller to store the 8 GPRs.
2817 NumBytes = std::max(NumBytes,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002818 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002819
2820 // Tail call needs the stack to be aligned.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002821 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
2822 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
2823 getFrameLowering()->getStackAlignment();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002824 unsigned AlignMask = TargetAlign-1;
2825 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2826 }
2827
2828 return NumBytes;
2829}
2830
2831/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00002832/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00002833static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002834 unsigned ParamSize) {
2835
Dale Johannesen86dcae12009-11-24 01:09:07 +00002836 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002837
2838 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
2839 unsigned CallerMinReservedArea = FI->getMinReservedArea();
2840 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
2841 // Remember only if the new adjustement is bigger.
2842 if (SPDiff < FI->getTailCallSPDelta())
2843 FI->setTailCallSPDelta(SPDiff);
2844
2845 return SPDiff;
2846}
2847
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002848/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2849/// for tail call optimization. Targets which want to do tail call
2850/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002851bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002852PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002853 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002854 bool isVarArg,
2855 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002856 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002857 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00002858 return false;
2859
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002860 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002861 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00002862 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002863
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002864 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00002865 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002866 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
2867 // Functions containing by val parameters are not supported.
2868 for (unsigned i = 0; i != Ins.size(); i++) {
2869 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2870 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002871 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002872
2873 // Non PIC/GOT tail calls are supported.
2874 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
2875 return true;
2876
2877 // At the moment we can only do local tail calls (in same module, hidden
2878 // or protected) if we are generating PIC.
2879 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2880 return G->getGlobal()->hasHiddenVisibility()
2881 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002882 }
2883
2884 return false;
2885}
2886
Chris Lattnereb755fc2006-05-17 19:00:46 +00002887/// isCallCompatibleAddress - Return the immediate to use if the specified
2888/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002889static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00002890 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2891 if (!C) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002892
Dan Gohmaneffb8942008-09-12 16:56:44 +00002893 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00002894 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00002895 SignExtend32<26>(Addr) != Addr)
Chris Lattnereb755fc2006-05-17 19:00:46 +00002896 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002897
Dan Gohmaneffb8942008-09-12 16:56:44 +00002898 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00002899 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00002900}
2901
Dan Gohmand78c4002008-05-13 00:00:25 +00002902namespace {
2903
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002904struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002905 SDValue Arg;
2906 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002907 int FrameIdx;
2908
2909 TailCallArgumentInfo() : FrameIdx(0) {}
2910};
2911
Dan Gohmand78c4002008-05-13 00:00:25 +00002912}
2913
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002914/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
2915static void
2916StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00002917 SDValue Chain,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002918 const SmallVector<TailCallArgumentInfo, 8> &TailCallArgs,
Dale Johannesen021052a2009-02-04 20:06:27 +00002919 SmallVector<SDValue, 8> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002920 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002921 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002922 SDValue Arg = TailCallArgs[i].Arg;
2923 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002924 int FI = TailCallArgs[i].FrameIdx;
2925 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00002926 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002927 MachinePointerInfo::getFixedStack(FI),
2928 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002929 }
2930}
2931
2932/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
2933/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002934static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002935 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002936 SDValue Chain,
2937 SDValue OldRetAddr,
2938 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002939 int SPDiff,
2940 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002941 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002942 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002943 if (SPDiff) {
2944 // Calculate the new stack slot for the return address.
2945 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002946 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002947 isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002948 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00002949 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00002950 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002951 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002952 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00002953 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00002954 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002955
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002956 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
2957 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002958 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002959 int NewFPLoc =
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002960 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene1fbe0542009-11-12 20:49:22 +00002961 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00002962 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002963 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
2964 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00002965 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00002966 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002967 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002968 }
2969 return Chain;
2970}
2971
2972/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
2973/// the position of the argument.
2974static void
2975CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002976 SDValue Arg, int SPDiff, unsigned ArgOffset,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002977 SmallVector<TailCallArgumentInfo, 8>& TailCallArguments) {
2978 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00002979 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00002980 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00002981 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002982 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002983 TailCallArgumentInfo Info;
2984 Info.Arg = Arg;
2985 Info.FrameIdxOp = FIN;
2986 Info.FrameIdx = FI;
2987 TailCallArguments.push_back(Info);
2988}
2989
2990/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
2991/// stack slot. Returns the chain as result and the loaded frame pointers in
2992/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002993SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00002994 int SPDiff,
2995 SDValue Chain,
2996 SDValue &LROpOut,
2997 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002998 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002999 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003000 if (SPDiff) {
3001 // Load the LR and FP stack slot for later adjusting.
Owen Anderson9f944592009-08-11 20:47:22 +00003002 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003003 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003004 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003005 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003006 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003007
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003008 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3009 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003010 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003011 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003012 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003013 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003014 Chain = SDValue(FPOpOut.getNode(), 1);
3015 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003016 }
3017 return Chain;
3018}
3019
Dale Johannesen85d41a12008-03-04 23:17:14 +00003020/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003021/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003022/// specified by the specific parameter attribute. The copy will be passed as
3023/// a byval function parameter.
3024/// Sometimes what we are copying is the end of a larger object, the part that
3025/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003026static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003027CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003028 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003029 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003030 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003031 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Chris Lattner2510de22010-09-21 05:40:29 +00003032 false, false, MachinePointerInfo(0),
3033 MachinePointerInfo(0));
Dale Johannesen85d41a12008-03-04 23:17:14 +00003034}
Chris Lattner43df5b32007-02-25 05:34:32 +00003035
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003036/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3037/// tail calls.
3038static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003039LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3040 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003041 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003042 bool isVector, SmallVector<SDValue, 8> &MemOpChains,
Chris Lattner676c61d2010-09-21 18:41:36 +00003043 SmallVector<TailCallArgumentInfo, 8> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003044 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003045 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003046 if (!isTailCall) {
3047 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003048 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003049 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003050 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003051 else
Owen Anderson9f944592009-08-11 20:47:22 +00003052 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003053 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003054 DAG.getConstant(ArgOffset, PtrVT));
3055 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003056 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3057 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003058 // Calculate and remember argument location.
3059 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3060 TailCallArguments);
3061}
3062
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003063static
3064void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003065 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003066 SDValue LROp, SDValue FPOp, bool isDarwinABI,
3067 SmallVector<TailCallArgumentInfo, 8> &TailCallArguments) {
3068 MachineFunction &MF = DAG.getMachineFunction();
3069
3070 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3071 // might overwrite each other in case of tail call optimization.
3072 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003073 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003074 InFlag = SDValue();
3075 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3076 MemOpChains2, dl);
3077 if (!MemOpChains2.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003078 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003079 &MemOpChains2[0], MemOpChains2.size());
3080
3081 // Store the return address to the appropriate stack slot.
3082 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3083 isPPC64, isDarwinABI, dl);
3084
3085 // Emit callseq_end just before tailcall node.
3086 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003087 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003088 InFlag = Chain.getValue(1);
3089}
3090
3091static
3092unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003093 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003094 SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass,
Owen Anderson53aa7a92009-08-10 22:56:29 +00003095 SmallVector<SDValue, 8> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003096 const PPCSubtarget &PPCSubTarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003097
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003098 bool isPPC64 = PPCSubTarget.isPPC64();
3099 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3100
Owen Anderson53aa7a92009-08-10 22:56:29 +00003101 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003102 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003103 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003104
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003105 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003106
Torok Edwin31e90d22010-08-04 20:47:44 +00003107 bool needIndirectCall = true;
3108 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003109 // If this is an absolute destination address, use the munged value.
3110 Callee = SDValue(Dest, 0);
Torok Edwin31e90d22010-08-04 20:47:44 +00003111 needIndirectCall = false;
3112 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003113
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003114 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3115 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3116 // Use indirect calls for ALL functions calls in JIT mode, since the
3117 // far-call stubs may be outside relocation limits for a BL instruction.
3118 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3119 unsigned OpFlags = 0;
3120 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003121 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003122 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003123 (G->getGlobal()->isDeclaration() ||
3124 G->getGlobal()->isWeakForLinker())) {
3125 // PC-relative references to external symbols should go through $stub,
3126 // unless we're building with the leopard linker or later, which
3127 // automatically synthesizes these stubs.
3128 OpFlags = PPCII::MO_DARWIN_STUB;
3129 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003130
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003131 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3132 // every direct call is) turn it into a TargetGlobalAddress /
3133 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin31e90d22010-08-04 20:47:44 +00003134 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003135 Callee.getValueType(),
3136 0, OpFlags);
Torok Edwin31e90d22010-08-04 20:47:44 +00003137 needIndirectCall = false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003138 }
Torok Edwin31e90d22010-08-04 20:47:44 +00003139 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003140
Torok Edwin31e90d22010-08-04 20:47:44 +00003141 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003142 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003143
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003144 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003145 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003146 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003147 // PC-relative references to external symbols should go through $stub,
3148 // unless we're building with the leopard linker or later, which
3149 // automatically synthesizes these stubs.
3150 OpFlags = PPCII::MO_DARWIN_STUB;
3151 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003152
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003153 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3154 OpFlags);
3155 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003156 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003157
Torok Edwin31e90d22010-08-04 20:47:44 +00003158 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003159 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3160 // to do the call, we can't use PPCISD::CALL.
3161 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003162
3163 if (isSVR4ABI && isPPC64) {
3164 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3165 // entry point, but to the function descriptor (the function entry point
3166 // address is part of the function descriptor though).
3167 // The function descriptor is a three doubleword structure with the
3168 // following fields: function entry point, TOC base address and
3169 // environment pointer.
3170 // Thus for a call through a function pointer, the following actions need
3171 // to be performed:
3172 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003173 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003174 // 2. Load the address of the function entry point from the function
3175 // descriptor.
3176 // 3. Load the TOC of the callee from the function descriptor into r2.
3177 // 4. Load the environment pointer from the function descriptor into
3178 // r11.
3179 // 5. Branch to the function entry point address.
3180 // 6. On return of the callee, the TOC of the caller needs to be
3181 // restored (this is done in FinishCall()).
3182 //
3183 // All those operations are flagged together to ensure that no other
3184 // operations can be scheduled in between. E.g. without flagging the
3185 // operations together, a TOC access in the caller could be scheduled
3186 // between the load of the callee TOC and the branch to the callee, which
3187 // results in the TOC access going through the TOC of the callee instead
3188 // of going through the TOC of the caller, which leads to incorrect code.
3189
3190 // Load the address of the function entry point from the function
3191 // descriptor.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003192 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003193 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, MTCTROps,
3194 InFlag.getNode() ? 3 : 2);
3195 Chain = LoadFuncPtr.getValue(1);
3196 InFlag = LoadFuncPtr.getValue(2);
3197
3198 // Load environment pointer into r11.
3199 // Offset of the environment pointer within the function descriptor.
3200 SDValue PtrOff = DAG.getIntPtrConstant(16);
3201
3202 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3203 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3204 InFlag);
3205 Chain = LoadEnvPtr.getValue(1);
3206 InFlag = LoadEnvPtr.getValue(2);
3207
3208 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3209 InFlag);
3210 Chain = EnvVal.getValue(0);
3211 InFlag = EnvVal.getValue(1);
3212
3213 // Load TOC of the callee into r2. We are using a target-specific load
3214 // with r2 hard coded, because the result of a target-independent load
3215 // would never go directly into r2, since r2 is a reserved register (which
3216 // prevents the register allocator from allocating it), resulting in an
3217 // additional register being allocated and an unnecessary move instruction
3218 // being generated.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003219 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003220 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3221 Callee, InFlag);
3222 Chain = LoadTOCPtr.getValue(0);
3223 InFlag = LoadTOCPtr.getValue(1);
3224
3225 MTCTROps[0] = Chain;
3226 MTCTROps[1] = LoadFuncPtr;
3227 MTCTROps[2] = InFlag;
3228 }
3229
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003230 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
3231 2 + (InFlag.getNode() != 0));
3232 InFlag = Chain.getValue(1);
3233
3234 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00003235 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003236 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003237 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003238 CallOpc = PPCISD::BCTRL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003239 Callee.setNode(0);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003240 // Add use of X11 (holding environment pointer)
3241 if (isSVR4ABI && isPPC64)
3242 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003243 // Add CTR register as callee so a bctr can be emitted later.
3244 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00003245 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003246 }
3247
3248 // If this is a direct call, pass the chain and the callee.
3249 if (Callee.getNode()) {
3250 Ops.push_back(Chain);
3251 Ops.push_back(Callee);
3252 }
3253 // If this is a tail call add stack pointer delta.
3254 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00003255 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003256
3257 // Add argument registers to the end of the list so that they are known live
3258 // into the call.
3259 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3260 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3261 RegsToPass[i].second.getValueType()));
3262
3263 return CallOpc;
3264}
3265
Roman Divacky76293062012-09-18 16:47:58 +00003266static
3267bool isLocalCall(const SDValue &Callee)
3268{
3269 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00003270 return !G->getGlobal()->isDeclaration() &&
3271 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00003272 return false;
3273}
3274
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003275SDValue
3276PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003277 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003278 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003279 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003280 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003281
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003282 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003283 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003284 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003285 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003286
3287 // Copy all of the result registers out of their specified physreg.
3288 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3289 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003290 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00003291
3292 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3293 VA.getLocReg(), VA.getLocVT(), InFlag);
3294 Chain = Val.getValue(1);
3295 InFlag = Val.getValue(2);
3296
3297 switch (VA.getLocInfo()) {
3298 default: llvm_unreachable("Unknown loc info!");
3299 case CCValAssign::Full: break;
3300 case CCValAssign::AExt:
3301 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3302 break;
3303 case CCValAssign::ZExt:
3304 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3305 DAG.getValueType(VA.getValVT()));
3306 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3307 break;
3308 case CCValAssign::SExt:
3309 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3310 DAG.getValueType(VA.getValVT()));
3311 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3312 break;
3313 }
3314
3315 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003316 }
3317
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003318 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003319}
3320
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003321SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00003322PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003323 bool isTailCall, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003324 SelectionDAG &DAG,
3325 SmallVector<std::pair<unsigned, SDValue>, 8>
3326 &RegsToPass,
3327 SDValue InFlag, SDValue Chain,
3328 SDValue &Callee,
3329 int SPDiff, unsigned NumBytes,
3330 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003331 SmallVectorImpl<SDValue> &InVals) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003332 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003333 SmallVector<SDValue, 8> Ops;
3334 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3335 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003336 PPCSubTarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003337
Hal Finkel5ab37802012-08-28 02:10:27 +00003338 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3339 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3340 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3341
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003342 // When performing tail call optimization the callee pops its arguments off
3343 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00003344 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003345 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003346 (CallConv == CallingConv::Fast &&
3347 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003348
Roman Divackyef21be22012-03-06 16:41:49 +00003349 // Add a register mask operand representing the call-preserved registers.
3350 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3351 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3352 assert(Mask && "Missing call preserved mask for calling convention");
3353 Ops.push_back(DAG.getRegisterMask(Mask));
3354
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003355 if (InFlag.getNode())
3356 Ops.push_back(InFlag);
3357
3358 // Emit tail call.
3359 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003360 assert(((Callee.getOpcode() == ISD::Register &&
3361 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3362 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3363 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3364 isa<ConstantSDNode>(Callee)) &&
3365 "Expecting an global address, external symbol, absolute value or register");
3366
Owen Anderson9f944592009-08-11 20:47:22 +00003367 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003368 }
3369
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003370 // Add a NOP immediately after the branch instruction when using the 64-bit
3371 // SVR4 ABI. At link time, if caller and callee are in a different module and
3372 // thus have a different TOC, the call will be replaced with a call to a stub
3373 // function which saves the current TOC, loads the TOC of the callee and
3374 // branches to the callee. The NOP will be replaced with a load instruction
3375 // which restores the TOC of the caller from the TOC save slot of the current
3376 // stack frame. If caller and callee belong to the same module (and have the
3377 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00003378
3379 bool needsTOCRestore = false;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003380 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003381 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003382 // This is a call through a function pointer.
3383 // Restore the caller TOC from the save area into R2.
3384 // See PrepareCall() for more information about calls through function
3385 // pointers in the 64-bit SVR4 ABI.
3386 // We are using a target-specific load with r2 hard coded, because the
3387 // result of a target-independent load would never go directly into r2,
3388 // since r2 is a reserved register (which prevents the register allocator
3389 // from allocating it), resulting in an additional register being
3390 // allocated and an unnecessary move instruction being generated.
Hal Finkel51861b42012-03-31 14:45:15 +00003391 needsTOCRestore = true;
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003392 } else if ((CallOpc == PPCISD::CALL) && !isLocalCall(Callee)) {
Roman Divacky76293062012-09-18 16:47:58 +00003393 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003394 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller79fef932009-12-18 13:00:15 +00003395 }
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003396 }
3397
Hal Finkel51861b42012-03-31 14:45:15 +00003398 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
3399 InFlag = Chain.getValue(1);
3400
3401 if (needsTOCRestore) {
3402 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3403 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3404 InFlag = Chain.getValue(1);
3405 }
3406
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003407 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3408 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003409 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003410 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003411 InFlag = Chain.getValue(1);
3412
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003413 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3414 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003415}
3416
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003417SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00003418PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003419 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00003420 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003421 SDLoc &dl = CLI.DL;
Justin Holewinskiaa583972012-05-25 16:35:28 +00003422 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
3423 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
3424 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
3425 SDValue Chain = CLI.Chain;
3426 SDValue Callee = CLI.Callee;
3427 bool &isTailCall = CLI.IsTailCall;
3428 CallingConv::ID CallConv = CLI.CallConv;
3429 bool isVarArg = CLI.IsVarArg;
3430
Evan Cheng67a69dd2010-01-27 00:07:07 +00003431 if (isTailCall)
3432 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3433 Ins, DAG);
3434
Bill Schmidt57d6de52012-10-23 15:51:16 +00003435 if (PPCSubTarget.isSVR4ABI()) {
3436 if (PPCSubTarget.isPPC64())
3437 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3438 isTailCall, Outs, OutVals, Ins,
3439 dl, DAG, InVals);
3440 else
3441 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3442 isTailCall, Outs, OutVals, Ins,
3443 dl, DAG, InVals);
3444 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003445
Bill Schmidt57d6de52012-10-23 15:51:16 +00003446 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3447 isTailCall, Outs, OutVals, Ins,
3448 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003449}
3450
3451SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003452PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3453 CallingConv::ID CallConv, bool isVarArg,
3454 bool isTailCall,
3455 const SmallVectorImpl<ISD::OutputArg> &Outs,
3456 const SmallVectorImpl<SDValue> &OutVals,
3457 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003458 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003459 SmallVectorImpl<SDValue> &InVals) const {
3460 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003461 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003462
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003463 assert((CallConv == CallingConv::C ||
3464 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003465
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003466 unsigned PtrByteSize = 4;
3467
3468 MachineFunction &MF = DAG.getMachineFunction();
3469
3470 // Mark this function as potentially containing a function that contains a
3471 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3472 // and restoring the callers stack pointer in this functions epilog. This is
3473 // done because by tail calling the called function might overwrite the value
3474 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003475 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3476 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003477 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00003478
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003479 // Count how many bytes are to be pushed on the stack, including the linkage
3480 // area, parameter list area and the part of the local variable space which
3481 // contains copies of aggregates which are passed by value.
3482
3483 // Assign locations to all of the outgoing arguments.
3484 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003485 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003486 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003487
3488 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003489 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003490
3491 if (isVarArg) {
3492 // Handle fixed and variable vector arguments differently.
3493 // Fixed vector arguments go into registers as long as registers are
3494 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003495 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00003496
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003497 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003498 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003499 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003500 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00003501
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003502 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00003503 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3504 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003505 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00003506 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3507 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003508 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003509
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003510 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003511#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00003512 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00003513 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003514#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00003515 llvm_unreachable(0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003516 }
3517 }
3518 } else {
3519 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00003520 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003521 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003522
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003523 // Assign locations to all of the outgoing aggregate by value arguments.
3524 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003525 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003526 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003527
3528 // Reserve stack space for the allocations in CCInfo.
3529 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3530
Bill Schmidtef17c142013-02-06 17:33:58 +00003531 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003532
3533 // Size of the linkage area, parameter list area and the part of the local
3534 // space variable where copies of aggregates which are passed by value are
3535 // stored.
3536 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003537
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003538 // Calculate by how many bytes the stack has to be adjusted in case of tail
3539 // call optimization.
3540 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3541
3542 // Adjust the stack pointer for the new arguments...
3543 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003544 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3545 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003546 SDValue CallSeqStart = Chain;
3547
3548 // Load the return address and frame pointer so it can be moved somewhere else
3549 // later.
3550 SDValue LROp, FPOp;
3551 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3552 dl);
3553
3554 // Set up a copy of the stack pointer for use loading and storing any
3555 // arguments that may not fit in the registers available for argument
3556 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00003557 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00003558
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003559 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3560 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3561 SmallVector<SDValue, 8> MemOpChains;
3562
Roman Divacky71038e72011-08-30 17:04:16 +00003563 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003564 // Walk the register/memloc assignments, inserting copies/loads.
3565 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3566 i != e;
3567 ++i) {
3568 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003569 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003570 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00003571
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003572 if (Flags.isByVal()) {
3573 // Argument is an aggregate which is passed by value, thus we need to
3574 // create a copy of it in the local variable space of the current stack
3575 // frame (which is the stack frame of the caller) and pass the address of
3576 // this copy to the callee.
3577 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3578 CCValAssign &ByValVA = ByValArgLocs[j++];
3579 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00003580
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003581 // Memory reserved in the local variable space of the callers stack frame.
3582 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003583
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003584 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3585 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00003586
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003587 // Create a copy of the argument in the local area of the current
3588 // stack frame.
3589 SDValue MemcpyCall =
3590 CreateCopyOfByValArgument(Arg, PtrOff,
3591 CallSeqStart.getNode()->getOperand(0),
3592 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00003593
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003594 // This must go outside the CALLSEQ_START..END.
3595 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003596 CallSeqStart.getNode()->getOperand(1),
3597 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003598 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3599 NewCallSeqStart.getNode());
3600 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00003601
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003602 // Pass the address of the aggregate copy on the stack either in a
3603 // physical register or in the parameter list area of the current stack
3604 // frame to the callee.
3605 Arg = PtrOff;
3606 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003607
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003608 if (VA.isRegLoc()) {
Roman Divacky71038e72011-08-30 17:04:16 +00003609 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003610 // Put argument in a physical register.
3611 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3612 } else {
3613 // Put argument in the parameter list area of the current stack frame.
3614 assert(VA.isMemLoc());
3615 unsigned LocMemOffset = VA.getLocMemOffset();
3616
3617 if (!isTailCall) {
3618 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3619 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3620
3621 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00003622 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00003623 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003624 } else {
3625 // Calculate and remember argument location.
3626 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3627 TailCallArguments);
3628 }
3629 }
3630 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003631
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003632 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00003633 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003634 &MemOpChains[0], MemOpChains.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00003635
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003636 // Build a sequence of copy-to-reg nodes chained together with token chain
3637 // and flag operands which copy the outgoing args into the appropriate regs.
3638 SDValue InFlag;
3639 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3640 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3641 RegsToPass[i].second, InFlag);
3642 InFlag = Chain.getValue(1);
3643 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003644
Hal Finkel5ab37802012-08-28 02:10:27 +00003645 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3646 // registers.
3647 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003648 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3649 SDValue Ops[] = { Chain, InFlag };
3650
Hal Finkel5ab37802012-08-28 02:10:27 +00003651 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003652 dl, VTs, Ops, InFlag.getNode() ? 2 : 1);
3653
Hal Finkel5ab37802012-08-28 02:10:27 +00003654 InFlag = Chain.getValue(1);
3655 }
3656
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003657 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003658 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3659 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003660
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003661 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3662 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3663 Ins, InVals);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003664}
3665
Bill Schmidt57d6de52012-10-23 15:51:16 +00003666// Copy an argument into memory, being careful to do this outside the
3667// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003668SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00003669PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3670 SDValue CallSeqStart,
3671 ISD::ArgFlagsTy Flags,
3672 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003673 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003674 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3675 CallSeqStart.getNode()->getOperand(0),
3676 Flags, DAG, dl);
3677 // The MEMCPY must go outside the CALLSEQ_START..END.
3678 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003679 CallSeqStart.getNode()->getOperand(1),
3680 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00003681 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3682 NewCallSeqStart.getNode());
3683 return NewCallSeqStart;
3684}
3685
3686SDValue
3687PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003688 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003689 bool isTailCall,
3690 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003691 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003692 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003693 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003694 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003695
Bill Schmidt57d6de52012-10-23 15:51:16 +00003696 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003697
Bill Schmidt57d6de52012-10-23 15:51:16 +00003698 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3699 unsigned PtrByteSize = 8;
3700
3701 MachineFunction &MF = DAG.getMachineFunction();
3702
3703 // Mark this function as potentially containing a function that contains a
3704 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3705 // and restoring the callers stack pointer in this functions epilog. This is
3706 // done because by tail calling the called function might overwrite the value
3707 // in this function's (MF) stack pointer stack slot 0(SP).
3708 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3709 CallConv == CallingConv::Fast)
3710 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3711
3712 unsigned nAltivecParamsAtEnd = 0;
3713
3714 // Count how many bytes are to be pushed on the stack, including the linkage
3715 // area, and parameter passing area. We start with at least 48 bytes, which
3716 // is reserved space for [SP][CR][LR][3 x unused].
3717 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
3718 // of this call.
3719 unsigned NumBytes =
3720 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
3721 Outs, OutVals, nAltivecParamsAtEnd);
3722
3723 // Calculate by how many bytes the stack has to be adjusted in case of tail
3724 // call optimization.
3725 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3726
3727 // To protect arguments on the stack from being clobbered in a tail call,
3728 // force all the loads to happen before doing any other lowering.
3729 if (isTailCall)
3730 Chain = DAG.getStackArgumentTokenFactor(Chain);
3731
3732 // Adjust the stack pointer for the new arguments...
3733 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003734 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3735 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003736 SDValue CallSeqStart = Chain;
3737
3738 // Load the return address and frame pointer so it can be move somewhere else
3739 // later.
3740 SDValue LROp, FPOp;
3741 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
3742 dl);
3743
3744 // Set up a copy of the stack pointer for use loading and storing any
3745 // arguments that may not fit in the registers available for argument
3746 // passing.
3747 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
3748
3749 // Figure out which arguments are going to go in registers, and which in
3750 // memory. Also, if this is a vararg function, floating point operations
3751 // must be stored to our stack, and loaded into integer regs as well, if
3752 // any integer regs are available for argument passing.
3753 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
3754 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
3755
3756 static const uint16_t GPR[] = {
3757 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3758 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3759 };
3760 static const uint16_t *FPR = GetFPR();
3761
3762 static const uint16_t VR[] = {
3763 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3764 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3765 };
3766 const unsigned NumGPRs = array_lengthof(GPR);
3767 const unsigned NumFPRs = 13;
3768 const unsigned NumVRs = array_lengthof(VR);
3769
3770 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3771 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3772
3773 SmallVector<SDValue, 8> MemOpChains;
3774 for (unsigned i = 0; i != NumOps; ++i) {
3775 SDValue Arg = OutVals[i];
3776 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3777
3778 // PtrOff will be used to store the current argument to the stack if a
3779 // register cannot be found for it.
3780 SDValue PtrOff;
3781
3782 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
3783
3784 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
3785
3786 // Promote integers to 64-bit values.
3787 if (Arg.getValueType() == MVT::i32) {
3788 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
3789 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3790 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
3791 }
3792
3793 // FIXME memcpy is used way more than necessary. Correctness first.
3794 // Note: "by value" is code for passing a structure by value, not
3795 // basic types.
3796 if (Flags.isByVal()) {
3797 // Note: Size includes alignment padding, so
3798 // struct x { short a; char b; }
3799 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
3800 // These are the proper values we need for right-justifying the
3801 // aggregate in a parameter register.
3802 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00003803
3804 // An empty aggregate parameter takes up no storage and no
3805 // registers.
3806 if (Size == 0)
3807 continue;
3808
Bill Schmidt57d6de52012-10-23 15:51:16 +00003809 // All aggregates smaller than 8 bytes must be passed right-justified.
3810 if (Size==1 || Size==2 || Size==4) {
3811 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
3812 if (GPR_idx != NumGPRs) {
3813 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
3814 MachinePointerInfo(), VT,
3815 false, false, 0);
3816 MemOpChains.push_back(Load.getValue(1));
3817 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3818
3819 ArgOffset += PtrByteSize;
3820 continue;
3821 }
3822 }
3823
3824 if (GPR_idx == NumGPRs && Size < 8) {
3825 SDValue Const = DAG.getConstant(PtrByteSize - Size,
3826 PtrOff.getValueType());
3827 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3828 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3829 CallSeqStart,
3830 Flags, DAG, dl);
3831 ArgOffset += PtrByteSize;
3832 continue;
3833 }
3834 // Copy entire object into memory. There are cases where gcc-generated
3835 // code assumes it is there, even if it could be put entirely into
3836 // registers. (This is not what the doc says.)
3837
3838 // FIXME: The above statement is likely due to a misunderstanding of the
3839 // documents. All arguments must be copied into the parameter area BY
3840 // THE CALLEE in the event that the callee takes the address of any
3841 // formal argument. That has not yet been implemented. However, it is
3842 // reasonable to use the stack area as a staging area for the register
3843 // load.
3844
3845 // Skip this for small aggregates, as we will use the same slot for a
3846 // right-justified copy, below.
3847 if (Size >= 8)
3848 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
3849 CallSeqStart,
3850 Flags, DAG, dl);
3851
3852 // When a register is available, pass a small aggregate right-justified.
3853 if (Size < 8 && GPR_idx != NumGPRs) {
3854 // The easiest way to get this right-justified in a register
3855 // is to copy the structure into the rightmost portion of a
3856 // local variable slot, then load the whole slot into the
3857 // register.
3858 // FIXME: The memcpy seems to produce pretty awful code for
3859 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00003860 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00003861 // parameter save area instead of a new local variable.
3862 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
3863 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3864 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3865 CallSeqStart,
3866 Flags, DAG, dl);
3867
3868 // Load the slot into the register.
3869 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
3870 MachinePointerInfo(),
3871 false, false, false, 0);
3872 MemOpChains.push_back(Load.getValue(1));
3873 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3874
3875 // Done with this argument.
3876 ArgOffset += PtrByteSize;
3877 continue;
3878 }
3879
3880 // For aggregates larger than PtrByteSize, copy the pieces of the
3881 // object that fit into registers from the parameter save area.
3882 for (unsigned j=0; j<Size; j+=PtrByteSize) {
3883 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
3884 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
3885 if (GPR_idx != NumGPRs) {
3886 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
3887 MachinePointerInfo(),
3888 false, false, false, 0);
3889 MemOpChains.push_back(Load.getValue(1));
3890 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3891 ArgOffset += PtrByteSize;
3892 } else {
3893 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
3894 break;
3895 }
3896 }
3897 continue;
3898 }
3899
3900 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
3901 default: llvm_unreachable("Unexpected ValueType for argument!");
3902 case MVT::i32:
3903 case MVT::i64:
3904 if (GPR_idx != NumGPRs) {
3905 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
3906 } else {
3907 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3908 true, isTailCall, false, MemOpChains,
3909 TailCallArguments, dl);
3910 }
3911 ArgOffset += PtrByteSize;
3912 break;
3913 case MVT::f32:
3914 case MVT::f64:
3915 if (FPR_idx != NumFPRs) {
3916 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
3917
3918 if (isVarArg) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00003919 // A single float or an aggregate containing only a single float
3920 // must be passed right-justified in the stack doubleword, and
3921 // in the GPR, if one is available.
3922 SDValue StoreOff;
3923 if (Arg.getValueType().getSimpleVT().SimpleTy == MVT::f32) {
3924 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
3925 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
3926 } else
3927 StoreOff = PtrOff;
3928
3929 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt57d6de52012-10-23 15:51:16 +00003930 MachinePointerInfo(), false, false, 0);
3931 MemOpChains.push_back(Store);
3932
3933 // Float varargs are always shadowed in available integer registers
3934 if (GPR_idx != NumGPRs) {
3935 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
3936 MachinePointerInfo(), false, false,
3937 false, 0);
3938 MemOpChains.push_back(Load.getValue(1));
3939 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3940 }
3941 } else if (GPR_idx != NumGPRs)
3942 // If we have any FPRs remaining, we may also have GPRs remaining.
3943 ++GPR_idx;
3944 } else {
3945 // Single-precision floating-point values are mapped to the
3946 // second (rightmost) word of the stack doubleword.
3947 if (Arg.getValueType() == MVT::f32) {
3948 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
3949 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
3950 }
3951
3952 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3953 true, isTailCall, false, MemOpChains,
3954 TailCallArguments, dl);
3955 }
3956 ArgOffset += 8;
3957 break;
3958 case MVT::v4f32:
3959 case MVT::v4i32:
3960 case MVT::v8i16:
3961 case MVT::v16i8:
3962 if (isVarArg) {
3963 // These go aligned on the stack, or in the corresponding R registers
3964 // when within range. The Darwin PPC ABI doc claims they also go in
3965 // V registers; in fact gcc does this only for arguments that are
3966 // prototyped, not for those that match the ... We do it for all
3967 // arguments, seems to work.
3968 while (ArgOffset % 16 !=0) {
3969 ArgOffset += PtrByteSize;
3970 if (GPR_idx != NumGPRs)
3971 GPR_idx++;
3972 }
3973 // We could elide this store in the case where the object fits
3974 // entirely in R registers. Maybe later.
3975 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
3976 DAG.getConstant(ArgOffset, PtrVT));
3977 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
3978 MachinePointerInfo(), false, false, 0);
3979 MemOpChains.push_back(Store);
3980 if (VR_idx != NumVRs) {
3981 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
3982 MachinePointerInfo(),
3983 false, false, false, 0);
3984 MemOpChains.push_back(Load.getValue(1));
3985 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
3986 }
3987 ArgOffset += 16;
3988 for (unsigned i=0; i<16; i+=PtrByteSize) {
3989 if (GPR_idx == NumGPRs)
3990 break;
3991 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
3992 DAG.getConstant(i, PtrVT));
3993 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
3994 false, false, false, 0);
3995 MemOpChains.push_back(Load.getValue(1));
3996 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3997 }
3998 break;
3999 }
4000
4001 // Non-varargs Altivec params generally go in registers, but have
4002 // stack space allocated at the end.
4003 if (VR_idx != NumVRs) {
4004 // Doesn't have GPR space allocated.
4005 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4006 } else {
4007 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4008 true, isTailCall, true, MemOpChains,
4009 TailCallArguments, dl);
4010 ArgOffset += 16;
4011 }
4012 break;
4013 }
4014 }
4015
4016 if (!MemOpChains.empty())
4017 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4018 &MemOpChains[0], MemOpChains.size());
4019
4020 // Check if this is an indirect call (MTCTR/BCTRL).
4021 // See PrepareCall() for more information about calls through function
4022 // pointers in the 64-bit SVR4 ABI.
4023 if (!isTailCall &&
4024 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4025 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4026 !isBLACompatibleAddress(Callee, DAG)) {
4027 // Load r2 into a virtual register and store it to the TOC save area.
4028 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4029 // TOC save area offset.
4030 SDValue PtrOff = DAG.getIntPtrConstant(40);
4031 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4032 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4033 false, false, 0);
4034 // R12 must contain the address of an indirect callee. This does not
4035 // mean the MTCTR instruction must use R12; it's easier to model this
4036 // as an extra parameter, so do that.
4037 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4038 }
4039
4040 // Build a sequence of copy-to-reg nodes chained together with token chain
4041 // and flag operands which copy the outgoing args into the appropriate regs.
4042 SDValue InFlag;
4043 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4044 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4045 RegsToPass[i].second, InFlag);
4046 InFlag = Chain.getValue(1);
4047 }
4048
4049 if (isTailCall)
4050 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4051 FPOp, true, TailCallArguments);
4052
4053 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4054 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4055 Ins, InVals);
4056}
4057
4058SDValue
4059PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4060 CallingConv::ID CallConv, bool isVarArg,
4061 bool isTailCall,
4062 const SmallVectorImpl<ISD::OutputArg> &Outs,
4063 const SmallVectorImpl<SDValue> &OutVals,
4064 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004065 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004066 SmallVectorImpl<SDValue> &InVals) const {
4067
4068 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004069
Owen Anderson53aa7a92009-08-10 22:56:29 +00004070 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00004071 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004072 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004073
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004074 MachineFunction &MF = DAG.getMachineFunction();
4075
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004076 // Mark this function as potentially containing a function that contains a
4077 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4078 // and restoring the callers stack pointer in this functions epilog. This is
4079 // done because by tail calling the called function might overwrite the value
4080 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004081 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4082 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004083 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4084
4085 unsigned nAltivecParamsAtEnd = 0;
4086
Chris Lattneraa40ec12006-05-16 22:56:08 +00004087 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00004088 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00004089 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004090 unsigned NumBytes =
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004091 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004092 Outs, OutVals,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004093 nAltivecParamsAtEnd);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004094
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004095 // Calculate by how many bytes the stack has to be adjusted in case of tail
4096 // call optimization.
4097 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004098
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004099 // To protect arguments on the stack from being clobbered in a tail call,
4100 // force all the loads to happen before doing any other lowering.
4101 if (isTailCall)
4102 Chain = DAG.getStackArgumentTokenFactor(Chain);
4103
Chris Lattnerb7552a82006-05-17 00:15:40 +00004104 // Adjust the stack pointer for the new arguments...
4105 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004106 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4107 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004108 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004109
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004110 // Load the return address and frame pointer so it can be move somewhere else
4111 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004112 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004113 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4114 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004115
Chris Lattnerb7552a82006-05-17 00:15:40 +00004116 // Set up a copy of the stack pointer for use loading and storing any
4117 // arguments that may not fit in the registers available for argument
4118 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004119 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004120 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004121 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004122 else
Owen Anderson9f944592009-08-11 20:47:22 +00004123 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004124
Chris Lattnerb7552a82006-05-17 00:15:40 +00004125 // Figure out which arguments are going to go in registers, and which in
4126 // memory. Also, if this is a vararg function, floating point operations
4127 // must be stored to our stack, and loaded into integer regs as well, if
4128 // any integer regs are available for argument passing.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004129 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004130 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004131
Craig Topperca658c22012-03-11 07:16:55 +00004132 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004133 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4134 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4135 };
Craig Topperca658c22012-03-11 07:16:55 +00004136 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00004137 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4138 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4139 };
Craig Topperca658c22012-03-11 07:16:55 +00004140 static const uint16_t *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004141
Craig Topperca658c22012-03-11 07:16:55 +00004142 static const uint16_t VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004143 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4144 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4145 };
Owen Andersone2f23a32007-09-07 04:06:50 +00004146 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004147 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00004148 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004149
Craig Topperca658c22012-03-11 07:16:55 +00004150 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004151
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004152 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004153 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4154
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004155 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00004156 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004157 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004158 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004159
Chris Lattnerb7552a82006-05-17 00:15:40 +00004160 // PtrOff will be used to store the current argument to the stack if a
4161 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004162 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004163
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004164 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004165
Dale Johannesen679073b2009-02-04 02:34:38 +00004166 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004167
4168 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00004169 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00004170 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4171 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00004172 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004173 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00004174
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004175 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004176 // Note: "by value" is code for passing a structure by value, not
4177 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00004178 if (Flags.isByVal()) {
4179 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004180 // Very small objects are passed right-justified. Everything else is
4181 // passed left-justified.
4182 if (Size==1 || Size==2) {
4183 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004184 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00004185 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00004186 MachinePointerInfo(), VT,
4187 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004188 MemOpChains.push_back(Load.getValue(1));
4189 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004190
4191 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004192 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00004193 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4194 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004195 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004196 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4197 CallSeqStart,
4198 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004199 ArgOffset += PtrByteSize;
4200 }
4201 continue;
4202 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004203 // Copy entire object into memory. There are cases where gcc-generated
4204 // code assumes it is there, even if it could be put entirely into
4205 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004206 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4207 CallSeqStart,
4208 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004209
4210 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4211 // copy the pieces of the object that fit into registers from the
4212 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00004213 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004214 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004215 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00004216 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004217 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4218 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004219 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00004220 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00004221 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004222 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004223 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004224 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004225 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004226 }
4227 }
4228 continue;
4229 }
4230
Owen Anderson9f944592009-08-11 20:47:22 +00004231 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004232 default: llvm_unreachable("Unexpected ValueType for argument!");
Owen Anderson9f944592009-08-11 20:47:22 +00004233 case MVT::i32:
4234 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004235 if (GPR_idx != NumGPRs) {
4236 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004237 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004238 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4239 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004240 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00004241 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004242 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004243 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004244 case MVT::f32:
4245 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004246 if (FPR_idx != NumFPRs) {
4247 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4248
Chris Lattnerb7552a82006-05-17 00:15:40 +00004249 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00004250 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4251 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004252 MemOpChains.push_back(Store);
4253
Chris Lattnerb7552a82006-05-17 00:15:40 +00004254 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004255 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004256 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004257 MachinePointerInfo(), false, false,
4258 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004259 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004260 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004261 }
Owen Anderson9f944592009-08-11 20:47:22 +00004262 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004263 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004264 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00004265 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4266 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004267 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004268 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004269 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00004270 }
4271 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00004272 // If we have any FPRs remaining, we may also have GPRs remaining.
4273 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4274 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004275 if (GPR_idx != NumGPRs)
4276 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00004277 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004278 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4279 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004280 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004281 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004282 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4283 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004284 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004285 if (isPPC64)
4286 ArgOffset += 8;
4287 else
Owen Anderson9f944592009-08-11 20:47:22 +00004288 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004289 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004290 case MVT::v4f32:
4291 case MVT::v4i32:
4292 case MVT::v8i16:
4293 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00004294 if (isVarArg) {
4295 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00004296 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00004297 // V registers; in fact gcc does this only for arguments that are
4298 // prototyped, not for those that match the ... We do it for all
4299 // arguments, seems to work.
4300 while (ArgOffset % 16 !=0) {
4301 ArgOffset += PtrByteSize;
4302 if (GPR_idx != NumGPRs)
4303 GPR_idx++;
4304 }
4305 // We could elide this store in the case where the object fits
4306 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004307 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004308 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00004309 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4310 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004311 MemOpChains.push_back(Store);
4312 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004313 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00004314 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004315 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004316 MemOpChains.push_back(Load.getValue(1));
4317 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4318 }
4319 ArgOffset += 16;
4320 for (unsigned i=0; i<16; i+=PtrByteSize) {
4321 if (GPR_idx == NumGPRs)
4322 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00004323 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004324 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00004325 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004326 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004327 MemOpChains.push_back(Load.getValue(1));
4328 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4329 }
4330 break;
4331 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004332
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004333 // Non-varargs Altivec params generally go in registers, but have
4334 // stack space allocated at the end.
4335 if (VR_idx != NumVRs) {
4336 // Doesn't have GPR space allocated.
4337 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4338 } else if (nAltivecParamsAtEnd==0) {
4339 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004340 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4341 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004342 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004343 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00004344 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00004345 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004346 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00004347 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004348 // If all Altivec parameters fit in registers, as they usually do,
4349 // they get stack space following the non-Altivec parameters. We
4350 // don't track this here because nobody below needs it.
4351 // If there are more Altivec parameters than fit in registers emit
4352 // the stores here.
4353 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4354 unsigned j = 0;
4355 // Offset is aligned; skip 1st 12 params which go in V registers.
4356 ArgOffset = ((ArgOffset+15)/16)*16;
4357 ArgOffset += 12*16;
4358 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004359 SDValue Arg = OutVals[i];
4360 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00004361 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4362 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004363 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004364 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004365 // We are emitting Altivec params in order.
4366 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4367 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004368 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004369 ArgOffset += 16;
4370 }
4371 }
4372 }
4373 }
4374
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004375 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00004376 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnered728e82006-08-11 17:38:39 +00004377 &MemOpChains[0], MemOpChains.size());
Scott Michelcf0da6c2009-02-17 22:15:04 +00004378
Dale Johannesen90eab672010-03-09 20:15:42 +00004379 // On Darwin, R12 must contain the address of an indirect callee. This does
4380 // not mean the MTCTR instruction must use R12; it's easier to model this as
4381 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004382 if (!isTailCall &&
Dale Johannesen90eab672010-03-09 20:15:42 +00004383 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4384 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4385 !isBLACompatibleAddress(Callee, DAG))
4386 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4387 PPC::R12), Callee));
4388
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004389 // Build a sequence of copy-to-reg nodes chained together with token chain
4390 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004391 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004392 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00004393 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00004394 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004395 InFlag = Chain.getValue(1);
4396 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004397
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004398 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004399 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4400 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004401
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004402 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4403 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4404 Ins, InVals);
Chris Lattneraa40ec12006-05-16 22:56:08 +00004405}
4406
Hal Finkel450128a2011-10-14 19:51:36 +00004407bool
4408PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4409 MachineFunction &MF, bool isVarArg,
4410 const SmallVectorImpl<ISD::OutputArg> &Outs,
4411 LLVMContext &Context) const {
4412 SmallVector<CCValAssign, 16> RVLocs;
4413 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4414 RVLocs, Context);
4415 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4416}
4417
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004418SDValue
4419PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004420 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004421 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004422 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004423 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004424
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004425 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004426 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00004427 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004428 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004429
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004430 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004431 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004432
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004433 // Copy the result values into the output registers.
4434 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4435 CCValAssign &VA = RVLocs[i];
4436 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004437
4438 SDValue Arg = OutVals[i];
4439
4440 switch (VA.getLocInfo()) {
4441 default: llvm_unreachable("Unknown loc info!");
4442 case CCValAssign::Full: break;
4443 case CCValAssign::AExt:
4444 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4445 break;
4446 case CCValAssign::ZExt:
4447 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4448 break;
4449 case CCValAssign::SExt:
4450 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4451 break;
4452 }
4453
4454 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004455 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004456 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004457 }
4458
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004459 RetOps[0] = Chain; // Update chain.
4460
4461 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00004462 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004463 RetOps.push_back(Flag);
4464
4465 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other,
4466 &RetOps[0], RetOps.size());
Chris Lattner4211ca92006-04-14 06:01:58 +00004467}
4468
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004469SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004470 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00004471 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004472 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004473
Jim Laskeye4f4d042006-12-04 22:04:42 +00004474 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004475 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00004476
4477 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00004478 bool isPPC64 = Subtarget.isPPC64();
4479 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004480 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004481
4482 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004483 SDValue Chain = Op.getOperand(0);
4484 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004485
Jim Laskeye4f4d042006-12-04 22:04:42 +00004486 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00004487 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4488 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004489 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004490
Jim Laskeye4f4d042006-12-04 22:04:42 +00004491 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00004492 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004493
Jim Laskeye4f4d042006-12-04 22:04:42 +00004494 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00004495 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004496 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004497}
4498
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004499
4500
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004501SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004502PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004503 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004504 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004505 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004506 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004507
4508 // Get current frame pointer save index. The users of this index will be
4509 // primarily DYNALLOC instructions.
4510 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4511 int RASI = FI->getReturnAddrSaveIndex();
4512
4513 // If the frame pointer save index hasn't been defined yet.
4514 if (!RASI) {
4515 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004516 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004517 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004518 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004519 // Save the result.
4520 FI->setReturnAddrSaveIndex(RASI);
4521 }
4522 return DAG.getFrameIndex(RASI, PtrVT);
4523}
4524
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004525SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004526PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4527 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004528 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004529 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004530 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004531
4532 // Get current frame pointer save index. The users of this index will be
4533 // primarily DYNALLOC instructions.
4534 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4535 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004536
Jim Laskey48850c12006-11-16 22:43:37 +00004537 // If the frame pointer save index hasn't been defined yet.
4538 if (!FPSI) {
4539 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004540 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004541 isDarwinABI);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004542
Jim Laskey48850c12006-11-16 22:43:37 +00004543 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004544 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00004545 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004546 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00004547 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004548 return DAG.getFrameIndex(FPSI, PtrVT);
4549}
Jim Laskey48850c12006-11-16 22:43:37 +00004550
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004551SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004552 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004553 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004554 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004555 SDValue Chain = Op.getOperand(0);
4556 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004557 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004558
Jim Laskey48850c12006-11-16 22:43:37 +00004559 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004560 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004561 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004562 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00004563 DAG.getConstant(0, PtrVT), Size);
4564 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004565 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00004566 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004567 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00004568 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004569 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey48850c12006-11-16 22:43:37 +00004570}
4571
Hal Finkel756810f2013-03-21 21:37:52 +00004572SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
4573 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004574 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004575 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
4576 DAG.getVTList(MVT::i32, MVT::Other),
4577 Op.getOperand(0), Op.getOperand(1));
4578}
4579
4580SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
4581 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004582 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004583 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
4584 Op.getOperand(0), Op.getOperand(1));
4585}
4586
Chris Lattner4211ca92006-04-14 06:01:58 +00004587/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4588/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004589SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00004590 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00004591 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4592 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00004593 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004594
Hal Finkel81f87992013-04-07 22:11:09 +00004595 // We might be able to do better than this under some circumstances, but in
4596 // general, fsel-based lowering of select is a finite-math-only optimization.
4597 // For more information, see section F.3 of the 2.06 ISA specification.
4598 if (!DAG.getTarget().Options.NoInfsFPMath ||
4599 !DAG.getTarget().Options.NoNaNsFPMath)
4600 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004601
Hal Finkel81f87992013-04-07 22:11:09 +00004602 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004603
Owen Anderson53aa7a92009-08-10 22:56:29 +00004604 EVT ResVT = Op.getValueType();
4605 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004606 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4607 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004608 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004609
Chris Lattner4211ca92006-04-14 06:01:58 +00004610 // If the RHS of the comparison is a 0.0, we don't need to do the
4611 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00004612 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00004613 if (isFloatingPointZero(RHS))
4614 switch (CC) {
4615 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004616 case ISD::SETNE:
4617 std::swap(TV, FV);
4618 case ISD::SETEQ:
4619 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4620 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
4621 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
4622 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4623 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4624 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4625 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004626 case ISD::SETULT:
4627 case ISD::SETLT:
4628 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004629 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004630 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00004631 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4632 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004633 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004634 case ISD::SETUGT:
4635 case ISD::SETGT:
4636 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004637 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004638 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00004639 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4640 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004641 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00004642 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004643 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004644
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004645 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00004646 switch (CC) {
4647 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004648 case ISD::SETNE:
4649 std::swap(TV, FV);
4650 case ISD::SETEQ:
4651 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
4652 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4653 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
4654 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
4655 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4656 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4657 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4658 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004659 case ISD::SETULT:
4660 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004661 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004662 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4663 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004664 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004665 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004666 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004667 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004668 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4669 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004670 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004671 case ISD::SETUGT:
4672 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004673 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004674 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4675 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004676 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004677 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00004678 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004679 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00004680 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4681 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00004682 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004683 }
Eli Friedman5806e182009-05-28 04:31:08 +00004684 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00004685}
4686
Chris Lattner57ee7c62007-11-28 18:44:47 +00004687// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen37bc85f2009-06-04 20:53:52 +00004688SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004689 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00004690 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004691 SDValue Src = Op.getOperand(0);
Bill Schmidt2db29ef2013-07-08 14:22:45 +00004692
4693 // If we have a long double here, it must be that we have an undef of
4694 // that type. In this case return an undef of the target type.
4695 if (Src.getValueType() == MVT::ppcf128) {
4696 assert(Src.getOpcode() == ISD::UNDEF && "Unhandled ppcf128!");
4697 return DAG.getNode(ISD::UNDEF, dl,
4698 Op.getValueType().getSimpleVT().SimpleTy);
4699 }
4700
Owen Anderson9f944592009-08-11 20:47:22 +00004701 if (Src.getValueType() == MVT::f32)
4702 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00004703
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004704 SDValue Tmp;
Owen Anderson9f944592009-08-11 20:47:22 +00004705 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004706 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00004707 case MVT::i32:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00004708 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Hal Finkelf6d45f22013-04-01 17:52:07 +00004709 (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ :
4710 PPCISD::FCTIDZ),
Owen Anderson9f944592009-08-11 20:47:22 +00004711 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00004712 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004713 case MVT::i64:
Hal Finkel3f88d082013-04-01 18:42:58 +00004714 assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) &&
4715 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00004716 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
4717 PPCISD::FCTIDUZ,
4718 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00004719 break;
4720 }
Duncan Sands2a287912008-07-19 16:26:02 +00004721
Chris Lattner4211ca92006-04-14 06:01:58 +00004722 // Convert the FP value to an int value through memory.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004723 bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() &&
4724 (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT());
4725 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
4726 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
4727 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00004728
Chris Lattner06a49542007-10-15 20:14:52 +00004729 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004730 SDValue Chain;
4731 if (i32Stack) {
4732 MachineFunction &MF = DAG.getMachineFunction();
4733 MachineMemOperand *MMO =
4734 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
4735 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
4736 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
4737 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
4738 MVT::i32, MMO);
4739 } else
4740 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
4741 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00004742
4743 // Result is a load from the stack slot. If loading 4 bytes, make sure to
4744 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004745 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00004746 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00004747 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkelf6d45f22013-04-01 17:52:07 +00004748 MPI = MachinePointerInfo();
4749 }
4750
4751 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004752 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00004753}
4754
Hal Finkelf6d45f22013-04-01 17:52:07 +00004755SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004756 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004757 SDLoc dl(Op);
Dan Gohmand6819da2008-03-11 01:59:03 +00004758 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00004759 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004760 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00004761
Hal Finkelf6d45f22013-04-01 17:52:07 +00004762 assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) &&
4763 "UINT_TO_FP is supported only with FPCVT");
4764
4765 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00004766 // Otherwise, convert to double-precision and then round.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004767 unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4768 (Op.getOpcode() == ISD::UINT_TO_FP ?
4769 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
4770 (Op.getOpcode() == ISD::UINT_TO_FP ?
4771 PPCISD::FCFIDU : PPCISD::FCFID);
4772 MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4773 MVT::f32 : MVT::f64;
4774
Owen Anderson9f944592009-08-11 20:47:22 +00004775 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00004776 SDValue SINT = Op.getOperand(0);
4777 // When converting to single-precision, we actually need to convert
4778 // to double-precision first and then round to single-precision.
4779 // To avoid double-rounding effects during that operation, we have
4780 // to prepare the input operand. Bits that might be truncated when
4781 // converting to double-precision are replaced by a bit that won't
4782 // be lost at this stage, but is below the single-precision rounding
4783 // position.
4784 //
4785 // However, if -enable-unsafe-fp-math is in effect, accept double
4786 // rounding to avoid the extra overhead.
4787 if (Op.getValueType() == MVT::f32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00004788 !PPCSubTarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00004789 !DAG.getTarget().Options.UnsafeFPMath) {
4790
4791 // Twiddle input to make sure the low 11 bits are zero. (If this
4792 // is the case, we are guaranteed the value will fit into the 53 bit
4793 // mantissa of an IEEE double-precision value without rounding.)
4794 // If any of those low 11 bits were not zero originally, make sure
4795 // bit 12 (value 2048) is set instead, so that the final rounding
4796 // to single-precision gets the correct result.
4797 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4798 SINT, DAG.getConstant(2047, MVT::i64));
4799 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
4800 Round, DAG.getConstant(2047, MVT::i64));
4801 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
4802 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4803 Round, DAG.getConstant(-2048, MVT::i64));
4804
4805 // However, we cannot use that value unconditionally: if the magnitude
4806 // of the input value is small, the bit-twiddling we did above might
4807 // end up visibly changing the output. Fortunately, in that case, we
4808 // don't need to twiddle bits since the original input will convert
4809 // exactly to double-precision floating-point already. Therefore,
4810 // construct a conditional to use the original value if the top 11
4811 // bits are all sign-bit copies, and use the rounded value computed
4812 // above otherwise.
4813 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
4814 SINT, DAG.getConstant(53, MVT::i32));
4815 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
4816 Cond, DAG.getConstant(1, MVT::i64));
4817 Cond = DAG.getSetCC(dl, MVT::i32,
4818 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
4819
4820 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
4821 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00004822
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00004823 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkelf6d45f22013-04-01 17:52:07 +00004824 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
4825
4826 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00004827 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00004828 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00004829 return FP;
4830 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004831
Owen Anderson9f944592009-08-11 20:47:22 +00004832 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00004833 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00004834 // Since we only generate this in 64-bit mode, we can take advantage of
4835 // 64-bit registers. In particular, sign extend the input value into the
4836 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
4837 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00004838 MachineFunction &MF = DAG.getMachineFunction();
4839 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004840 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004841
Hal Finkelbeb296b2013-03-31 10:12:51 +00004842 SDValue Ld;
Hal Finkelf6d45f22013-04-01 17:52:07 +00004843 if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) {
Hal Finkelbeb296b2013-03-31 10:12:51 +00004844 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
4845 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004846
Hal Finkelbeb296b2013-03-31 10:12:51 +00004847 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
4848 MachinePointerInfo::getFixedStack(FrameIdx),
4849 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00004850
Hal Finkelbeb296b2013-03-31 10:12:51 +00004851 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
4852 "Expected an i32 store");
4853 MachineMemOperand *MMO =
4854 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
4855 MachineMemOperand::MOLoad, 4, 4);
4856 SDValue Ops[] = { Store, FIdx };
Hal Finkelf6d45f22013-04-01 17:52:07 +00004857 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
4858 PPCISD::LFIWZX : PPCISD::LFIWAX,
4859 dl, DAG.getVTList(MVT::f64, MVT::Other),
4860 Ops, 2, MVT::i32, MMO);
Hal Finkelbeb296b2013-03-31 10:12:51 +00004861 } else {
Hal Finkelf6d45f22013-04-01 17:52:07 +00004862 assert(PPCSubTarget.isPPC64() &&
4863 "i32->FP without LFIWAX supported only on PPC64");
4864
Hal Finkelbeb296b2013-03-31 10:12:51 +00004865 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
4866 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
4867
4868 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
4869 Op.getOperand(0));
4870
4871 // STD the extended value into the stack slot.
4872 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
4873 MachinePointerInfo::getFixedStack(FrameIdx),
4874 false, false, 0);
4875
4876 // Load the value as a double.
4877 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
4878 MachinePointerInfo::getFixedStack(FrameIdx),
4879 false, false, false, 0);
4880 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004881
Chris Lattner4211ca92006-04-14 06:01:58 +00004882 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00004883 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
4884 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00004885 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00004886 return FP;
4887}
4888
Dan Gohman21cea8a2010-04-17 15:26:15 +00004889SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
4890 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004891 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00004892 /*
4893 The rounding mode is in bits 30:31 of FPSR, and has the following
4894 settings:
4895 00 Round to nearest
4896 01 Round to 0
4897 10 Round to +inf
4898 11 Round to -inf
4899
4900 FLT_ROUNDS, on the other hand, expects the following:
4901 -1 Undefined
4902 0 Round to 0
4903 1 Round to nearest
4904 2 Round to +inf
4905 3 Round to -inf
4906
4907 To perform the conversion, we do:
4908 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
4909 */
4910
4911 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004912 EVT VT = Op.getValueType();
4913 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004914 SDValue MFFSreg, InFlag;
Dale Johannesen5c94cb32008-01-18 19:55:37 +00004915
4916 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00004917 EVT NodeTys[] = {
4918 MVT::f64, // return register
4919 MVT::Glue // unused in this context
4920 };
Dale Johannesen021052a2009-02-04 20:06:27 +00004921 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00004922
4923 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00004924 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004925 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00004926 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00004927 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00004928
4929 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004930 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00004931 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00004932 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004933 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00004934
4935 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004936 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00004937 DAG.getNode(ISD::AND, dl, MVT::i32,
4938 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004939 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00004940 DAG.getNode(ISD::SRL, dl, MVT::i32,
4941 DAG.getNode(ISD::AND, dl, MVT::i32,
4942 DAG.getNode(ISD::XOR, dl, MVT::i32,
4943 CWD, DAG.getConstant(3, MVT::i32)),
4944 DAG.getConstant(3, MVT::i32)),
4945 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00004946
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004947 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00004948 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00004949
Duncan Sands13237ac2008-06-06 12:08:01 +00004950 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00004951 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00004952}
4953
Dan Gohman21cea8a2010-04-17 15:26:15 +00004954SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004955 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00004956 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004957 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00004958 assert(Op.getNumOperands() == 3 &&
4959 VT == Op.getOperand(1).getValueType() &&
4960 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00004961
Chris Lattner601b8652006-09-20 03:47:40 +00004962 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00004963 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004964 SDValue Lo = Op.getOperand(0);
4965 SDValue Hi = Op.getOperand(1);
4966 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004967 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004968
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00004969 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00004970 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00004971 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
4972 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
4973 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
4974 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00004975 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00004976 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
4977 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
4978 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004979 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00004980 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00004981}
4982
Dan Gohman21cea8a2010-04-17 15:26:15 +00004983SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004984 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004985 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00004986 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00004987 assert(Op.getNumOperands() == 3 &&
4988 VT == Op.getOperand(1).getValueType() &&
4989 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00004990
Dan Gohman8d2ead22008-03-07 20:36:53 +00004991 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00004992 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004993 SDValue Lo = Op.getOperand(0);
4994 SDValue Hi = Op.getOperand(1);
4995 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004996 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004997
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00004998 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00004999 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005000 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5001 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5002 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5003 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005004 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005005 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5006 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5007 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005008 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005009 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005010}
5011
Dan Gohman21cea8a2010-04-17 15:26:15 +00005012SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005013 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005014 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005015 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005016 assert(Op.getNumOperands() == 3 &&
5017 VT == Op.getOperand(1).getValueType() &&
5018 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005019
Dan Gohman8d2ead22008-03-07 20:36:53 +00005020 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005021 SDValue Lo = Op.getOperand(0);
5022 SDValue Hi = Op.getOperand(1);
5023 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005024 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005025
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005026 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005027 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005028 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5029 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5030 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5031 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005032 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005033 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5034 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5035 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00005036 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005037 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005038 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005039}
5040
5041//===----------------------------------------------------------------------===//
5042// Vector related lowering.
5043//
5044
Chris Lattner2a099c02006-04-17 06:00:21 +00005045/// BuildSplatI - Build a canonical splati of Val with an element size of
5046/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005047static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005048 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00005049 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005050
Owen Anderson53aa7a92009-08-10 22:56:29 +00005051 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00005052 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00005053 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005054
Owen Anderson9f944592009-08-11 20:47:22 +00005055 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005056
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005057 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5058 if (Val == -1)
5059 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005060
Owen Anderson53aa7a92009-08-10 22:56:29 +00005061 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005062
Chris Lattner2a099c02006-04-17 06:00:21 +00005063 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00005064 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005065 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00005066 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga49de9d2009-02-25 22:49:59 +00005067 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
5068 &Ops[0], Ops.size());
Wesley Peck527da1b2010-11-23 03:31:01 +00005069 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005070}
5071
Hal Finkelcf2e9082013-05-24 23:00:14 +00005072/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5073/// specified intrinsic ID.
5074static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005075 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00005076 EVT DestVT = MVT::Other) {
5077 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5078 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5079 DAG.getConstant(IID, MVT::i32), Op);
5080}
5081
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005082/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00005083/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005084static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005085 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005086 EVT DestVT = MVT::Other) {
5087 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005088 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005089 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005090}
5091
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005092/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5093/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005094static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005095 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005096 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00005097 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005098 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005099 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005100}
5101
5102
Chris Lattner264c9082006-04-17 17:55:10 +00005103/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5104/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005105static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005106 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00005107 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00005108 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5109 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005110
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005111 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00005112 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005113 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00005114 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005115 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00005116}
5117
Chris Lattner19e90552006-04-14 05:19:18 +00005118// If this is a case we can't handle, return null and let the default
5119// expansion code take care of it. If we CAN select this case, and if it
5120// selects to a single instruction, return Op. Otherwise, if we can codegen
5121// this case more efficiently than a constant pool load, lower it to the
5122// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005123SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5124 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005125 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005126 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5127 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00005128
Bob Wilson85cefe82009-03-02 23:24:16 +00005129 // Check if this is a splat of a constant value.
5130 APInt APSplatBits, APSplatUndef;
5131 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005132 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00005133 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00005134 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00005135 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00005136
Bob Wilson530e0382009-03-03 19:26:27 +00005137 unsigned SplatBits = APSplatBits.getZExtValue();
5138 unsigned SplatUndef = APSplatUndef.getZExtValue();
5139 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005140
Bob Wilson530e0382009-03-03 19:26:27 +00005141 // First, handle single instruction cases.
5142
5143 // All zeros?
5144 if (SplatBits == 0) {
5145 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00005146 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5147 SDValue Z = DAG.getConstant(0, MVT::i32);
5148 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00005149 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00005150 }
Bob Wilson530e0382009-03-03 19:26:27 +00005151 return Op;
5152 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00005153
Bob Wilson530e0382009-03-03 19:26:27 +00005154 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5155 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5156 (32-SplatBitSize));
5157 if (SextVal >= -16 && SextVal <= 15)
5158 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005159
5160
Bob Wilson530e0382009-03-03 19:26:27 +00005161 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005162
Bob Wilson530e0382009-03-03 19:26:27 +00005163 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00005164 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5165 // If this value is in the range [17,31] and is odd, use:
5166 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5167 // If this value is in the range [-31,-17] and is odd, use:
5168 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5169 // Note the last two are three-instruction sequences.
5170 if (SextVal >= -32 && SextVal <= 31) {
5171 // To avoid having these optimizations undone by constant folding,
5172 // we convert to a pseudo that will be expanded later into one of
5173 // the above forms.
5174 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt51e79512013-02-20 15:50:31 +00005175 EVT VT = Op.getValueType();
5176 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5177 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5178 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilson530e0382009-03-03 19:26:27 +00005179 }
5180
5181 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5182 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5183 // for fneg/fabs.
5184 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5185 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00005186 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005187
5188 // Make the VSLW intrinsic, computing 0x8000_0000.
5189 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5190 OnesV, DAG, dl);
5191
5192 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00005193 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00005194 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005195 }
5196
5197 // Check to see if this is a wide variety of vsplti*, binop self cases.
5198 static const signed char SplatCsts[] = {
5199 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5200 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5201 };
5202
5203 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5204 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5205 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5206 int i = SplatCsts[idx];
5207
5208 // Figure out what shift amount will be used by altivec if shifted by i in
5209 // this splat size.
5210 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5211
5212 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00005213 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005214 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005215 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5216 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5217 Intrinsic::ppc_altivec_vslw
5218 };
5219 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005220 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005221 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005222
Bob Wilson530e0382009-03-03 19:26:27 +00005223 // vsplti + srl self.
5224 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005225 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005226 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5227 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5228 Intrinsic::ppc_altivec_vsrw
5229 };
5230 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005231 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005232 }
5233
Bob Wilson530e0382009-03-03 19:26:27 +00005234 // vsplti + sra self.
5235 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005236 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005237 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5238 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5239 Intrinsic::ppc_altivec_vsraw
5240 };
5241 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005242 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005243 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005244
Bob Wilson530e0382009-03-03 19:26:27 +00005245 // vsplti + rol self.
5246 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5247 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005248 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005249 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5250 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5251 Intrinsic::ppc_altivec_vrlw
5252 };
5253 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005254 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005255 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005256
Bob Wilson530e0382009-03-03 19:26:27 +00005257 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00005258 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005259 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005260 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00005261 }
Bob Wilson530e0382009-03-03 19:26:27 +00005262 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00005263 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005264 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005265 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00005266 }
Bob Wilson530e0382009-03-03 19:26:27 +00005267 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00005268 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005269 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005270 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5271 }
5272 }
5273
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005274 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00005275}
5276
Chris Lattner071ad012006-04-17 05:28:54 +00005277/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5278/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005279static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005280 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005281 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00005282 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00005283 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00005284 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005285
Chris Lattner071ad012006-04-17 05:28:54 +00005286 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00005287 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 +00005288 OP_VMRGHW,
5289 OP_VMRGLW,
5290 OP_VSPLTISW0,
5291 OP_VSPLTISW1,
5292 OP_VSPLTISW2,
5293 OP_VSPLTISW3,
5294 OP_VSLDOI4,
5295 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00005296 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00005297 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00005298
Chris Lattner071ad012006-04-17 05:28:54 +00005299 if (OpNum == OP_COPY) {
5300 if (LHSID == (1*9+2)*9+3) return LHS;
5301 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5302 return RHS;
5303 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005304
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005305 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005306 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5307 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005308
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005309 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00005310 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005311 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00005312 case OP_VMRGHW:
5313 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5314 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5315 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5316 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5317 break;
5318 case OP_VMRGLW:
5319 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5320 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5321 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5322 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5323 break;
5324 case OP_VSPLTISW0:
5325 for (unsigned i = 0; i != 16; ++i)
5326 ShufIdxs[i] = (i&3)+0;
5327 break;
5328 case OP_VSPLTISW1:
5329 for (unsigned i = 0; i != 16; ++i)
5330 ShufIdxs[i] = (i&3)+4;
5331 break;
5332 case OP_VSPLTISW2:
5333 for (unsigned i = 0; i != 16; ++i)
5334 ShufIdxs[i] = (i&3)+8;
5335 break;
5336 case OP_VSPLTISW3:
5337 for (unsigned i = 0; i != 16; ++i)
5338 ShufIdxs[i] = (i&3)+12;
5339 break;
5340 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005341 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005342 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005343 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005344 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005345 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005346 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00005347 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00005348 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5349 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005350 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00005351 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00005352}
5353
Chris Lattner19e90552006-04-14 05:19:18 +00005354/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5355/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5356/// return the code it can be lowered into. Worst case, it can always be
5357/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005358SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005359 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005360 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005361 SDValue V1 = Op.getOperand(0);
5362 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005363 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005364 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005365
Chris Lattner19e90552006-04-14 05:19:18 +00005366 // Cases that are handled by instructions that take permute immediates
5367 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5368 // selected by the instruction selector.
5369 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005370 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5371 PPC::isSplatShuffleMask(SVOp, 2) ||
5372 PPC::isSplatShuffleMask(SVOp, 4) ||
5373 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5374 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5375 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5376 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5377 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5378 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5379 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5380 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5381 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattner19e90552006-04-14 05:19:18 +00005382 return Op;
5383 }
5384 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005385
Chris Lattner19e90552006-04-14 05:19:18 +00005386 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5387 // and produce a fixed permutation. If any of these match, do not lower to
5388 // VPERM.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005389 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5390 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5391 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5392 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5393 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5394 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5395 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5396 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5397 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattner19e90552006-04-14 05:19:18 +00005398 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005399
Chris Lattner071ad012006-04-17 05:28:54 +00005400 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5401 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005402 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00005403
Chris Lattner071ad012006-04-17 05:28:54 +00005404 unsigned PFIndexes[4];
5405 bool isFourElementShuffle = true;
5406 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5407 unsigned EltNo = 8; // Start out undef.
5408 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005409 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00005410 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005411
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005412 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00005413 if ((ByteSource & 3) != j) {
5414 isFourElementShuffle = false;
5415 break;
5416 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005417
Chris Lattner071ad012006-04-17 05:28:54 +00005418 if (EltNo == 8) {
5419 EltNo = ByteSource/4;
5420 } else if (EltNo != ByteSource/4) {
5421 isFourElementShuffle = false;
5422 break;
5423 }
5424 }
5425 PFIndexes[i] = EltNo;
5426 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005427
5428 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00005429 // perfect shuffle vector to determine if it is cost effective to do this as
5430 // discrete instructions, or whether we should use a vperm.
5431 if (isFourElementShuffle) {
5432 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005433 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00005434 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005435
Chris Lattner071ad012006-04-17 05:28:54 +00005436 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5437 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005438
Chris Lattner071ad012006-04-17 05:28:54 +00005439 // Determining when to avoid vperm is tricky. Many things affect the cost
5440 // of vperm, particularly how many times the perm mask needs to be computed.
5441 // For example, if the perm mask can be hoisted out of a loop or is already
5442 // used (perhaps because there are multiple permutes with the same shuffle
5443 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5444 // the loop requires an extra register.
5445 //
5446 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00005447 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00005448 // available, if this block is within a loop, we should avoid using vperm
5449 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005450 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005451 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005452 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005453
Chris Lattner19e90552006-04-14 05:19:18 +00005454 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5455 // vector that will get spilled to the constant pool.
5456 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005457
Chris Lattner19e90552006-04-14 05:19:18 +00005458 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5459 // that it is in input element units, not in bytes. Convert now.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005460 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005461 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005462
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005463 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005464 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5465 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005466
Chris Lattner19e90552006-04-14 05:19:18 +00005467 for (unsigned j = 0; j != BytesPerElement; ++j)
5468 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson9f944592009-08-11 20:47:22 +00005469 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00005470 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005471
Owen Anderson9f944592009-08-11 20:47:22 +00005472 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga49de9d2009-02-25 22:49:59 +00005473 &ResultMask[0], ResultMask.size());
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005474 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00005475}
5476
Chris Lattner9754d142006-04-18 17:59:36 +00005477/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5478/// altivec comparison. If it is, return true and fill in Opc/isDot with
5479/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005480static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner9754d142006-04-18 17:59:36 +00005481 bool &isDot) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005482 unsigned IntrinsicID =
5483 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00005484 CompareOpc = -1;
5485 isDot = false;
5486 switch (IntrinsicID) {
5487 default: return false;
5488 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00005489 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5490 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5491 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5492 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5493 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5494 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5495 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5496 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5497 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5498 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5499 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5500 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5501 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005502
Chris Lattner4211ca92006-04-14 06:01:58 +00005503 // Normal Comparisons.
5504 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5505 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5506 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5507 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5508 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5509 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5510 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5511 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5512 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5513 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5514 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5515 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5516 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5517 }
Chris Lattner9754d142006-04-18 17:59:36 +00005518 return true;
5519}
5520
5521/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5522/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005523SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005524 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00005525 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5526 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005527 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00005528 int CompareOpc;
5529 bool isDot;
5530 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005531 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005532
Chris Lattner9754d142006-04-18 17:59:36 +00005533 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00005534 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00005535 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00005536 Op.getOperand(1), Op.getOperand(2),
5537 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00005538 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00005539 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005540
Chris Lattner4211ca92006-04-14 06:01:58 +00005541 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005542 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005543 Op.getOperand(2), // LHS
5544 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00005545 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005546 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005547 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00005548 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005549
Chris Lattner4211ca92006-04-14 06:01:58 +00005550 // Now that we have the comparison, emit a copy from the CR to a GPR.
5551 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00005552 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00005553 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00005554 CompNode.getValue(1));
5555
Chris Lattner4211ca92006-04-14 06:01:58 +00005556 // Unpack the result based on how the target uses it.
5557 unsigned BitNo; // Bit # of CR6.
5558 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00005559 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00005560 default: // Can't happen, don't crash on invalid number though.
5561 case 0: // Return the value of the EQ bit of CR6.
5562 BitNo = 0; InvertBit = false;
5563 break;
5564 case 1: // Return the inverted value of the EQ bit of CR6.
5565 BitNo = 0; InvertBit = true;
5566 break;
5567 case 2: // Return the value of the LT bit of CR6.
5568 BitNo = 2; InvertBit = false;
5569 break;
5570 case 3: // Return the inverted value of the LT bit of CR6.
5571 BitNo = 2; InvertBit = true;
5572 break;
5573 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005574
Chris Lattner4211ca92006-04-14 06:01:58 +00005575 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00005576 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5577 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005578 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00005579 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5580 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00005581
Chris Lattner4211ca92006-04-14 06:01:58 +00005582 // If we are supposed to, toggle the bit.
5583 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00005584 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5585 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005586 return Flags;
5587}
5588
Scott Michelcf0da6c2009-02-17 22:15:04 +00005589SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005590 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005591 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00005592 // Create a stack slot that is 16-byte aligned.
5593 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00005594 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00005595 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005596 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005597
Chris Lattner4211ca92006-04-14 06:01:58 +00005598 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00005599 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00005600 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005601 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005602 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00005603 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005604 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005605}
5606
Dan Gohman21cea8a2010-04-17 15:26:15 +00005607SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005608 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005609 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005610 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005611
Owen Anderson9f944592009-08-11 20:47:22 +00005612 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
5613 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005614
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005615 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005616 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005617
Chris Lattner7e4398742006-04-18 03:43:48 +00005618 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00005619 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
5620 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
5621 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005622
Chris Lattner7e4398742006-04-18 03:43:48 +00005623 // Low parts multiplied together, generating 32-bit results (we ignore the
5624 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005625 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00005626 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005627
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005628 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00005629 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00005630 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005631 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005632 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005633 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
5634 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005635 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005636
Owen Anderson9f944592009-08-11 20:47:22 +00005637 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00005638
Chris Lattner96d50482006-04-18 04:28:57 +00005639 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005640 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00005641 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005642 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005643
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005644 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005645 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00005646 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005647 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005648
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005649 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005650 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00005651 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00005652 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005653
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005654 // Merge the results together.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005655 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005656 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005657 Ops[i*2 ] = 2*i+1;
5658 Ops[i*2+1] = 2*i+1+16;
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00005659 }
Owen Anderson9f944592009-08-11 20:47:22 +00005660 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00005661 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005662 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00005663 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005664}
5665
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005666/// LowerOperation - Provide custom lowering hooks for some operations.
5667///
Dan Gohman21cea8a2010-04-17 15:26:15 +00005668SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005669 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005670 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005671 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00005672 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005673 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00005674 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00005675 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00005676 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00005677 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
5678 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005679 case ISD::VASTART:
Dan Gohman31ae5862010-04-17 14:41:14 +00005680 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005681
5682 case ISD::VAARG:
Dan Gohman31ae5862010-04-17 14:41:14 +00005683 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00005684
Jim Laskeye4f4d042006-12-04 22:04:42 +00005685 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00005686 case ISD::DYNAMIC_STACKALLOC:
5687 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng51096af2008-04-19 01:30:48 +00005688
Hal Finkel756810f2013-03-21 21:37:52 +00005689 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
5690 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
5691
Chris Lattner4211ca92006-04-14 06:01:58 +00005692 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005693 case ISD::FP_TO_UINT:
5694 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005695 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00005696 case ISD::UINT_TO_FP:
5697 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00005698 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00005699
Chris Lattner4211ca92006-04-14 06:01:58 +00005700 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00005701 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
5702 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
5703 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00005704
Chris Lattner4211ca92006-04-14 06:01:58 +00005705 // Vector-related lowering.
5706 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5707 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5708 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5709 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005710 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005711
Hal Finkel25c19922013-05-15 21:37:41 +00005712 // For counter-based loop handling.
5713 case ISD::INTRINSIC_W_CHAIN: return SDValue();
5714
Chris Lattnerf6a81562007-12-08 06:59:59 +00005715 // Frame & Return address.
5716 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00005717 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00005718 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00005719}
5720
Duncan Sands6ed40142008-12-01 11:39:25 +00005721void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
5722 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005723 SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00005724 const TargetMachine &TM = getTargetMachine();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005725 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00005726 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00005727 default:
Craig Toppere55c5562012-02-07 02:50:20 +00005728 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkel25c19922013-05-15 21:37:41 +00005729 case ISD::INTRINSIC_W_CHAIN: {
5730 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
5731 Intrinsic::ppc_is_decremented_ctr_nonzero)
5732 break;
5733
5734 assert(N->getValueType(0) == MVT::i1 &&
5735 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00005736 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00005737 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
5738 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
5739 N->getOperand(1));
5740
5741 Results.push_back(NewInt);
5742 Results.push_back(NewInt.getValue(1));
5743 break;
5744 }
Roman Divacky4394e682011-06-28 15:30:42 +00005745 case ISD::VAARG: {
5746 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
5747 || TM.getSubtarget<PPCSubtarget>().isPPC64())
5748 return;
5749
5750 EVT VT = N->getValueType(0);
5751
5752 if (VT == MVT::i64) {
5753 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
5754
5755 Results.push_back(NewNode);
5756 Results.push_back(NewNode.getValue(1));
5757 }
5758 return;
5759 }
Duncan Sands6ed40142008-12-01 11:39:25 +00005760 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00005761 assert(N->getValueType(0) == MVT::ppcf128);
5762 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005763 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005764 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00005765 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00005766 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005767 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00005768 DAG.getIntPtrConstant(1));
5769
Ulrich Weigand874fc622013-03-26 10:56:22 +00005770 // Add the two halves of the long double in round-to-zero mode.
5771 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00005772
5773 // We know the low half is about to be thrown away, so just use something
5774 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00005775 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00005776 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00005777 return;
Duncan Sands2a287912008-07-19 16:26:02 +00005778 }
Duncan Sands6ed40142008-12-01 11:39:25 +00005779 case ISD::FP_TO_SINT:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005780 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00005781 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00005782 }
5783}
5784
5785
Chris Lattner4211ca92006-04-14 06:01:58 +00005786//===----------------------------------------------------------------------===//
5787// Other Lowering Code
5788//===----------------------------------------------------------------------===//
5789
Chris Lattner9b577f12005-08-26 21:23:58 +00005790MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00005791PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman747e55b2009-02-07 16:15:20 +00005792 bool is64bit, unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005793 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesend4eb0522008-08-25 22:34:37 +00005794 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5795
5796 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5797 MachineFunction *F = BB->getParent();
5798 MachineFunction::iterator It = BB;
5799 ++It;
5800
5801 unsigned dest = MI->getOperand(0).getReg();
5802 unsigned ptrA = MI->getOperand(1).getReg();
5803 unsigned ptrB = MI->getOperand(2).getReg();
5804 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00005805 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00005806
5807 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5808 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5809 F->insert(It, loopMBB);
5810 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00005811 exitMBB->splice(exitMBB->begin(), BB,
5812 llvm::next(MachineBasicBlock::iterator(MI)),
5813 BB->end());
5814 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00005815
5816 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005817 unsigned TmpReg = (!BinOpcode) ? incr :
5818 RegInfo.createVirtualRegister(
Dale Johannesenbc698292008-09-02 20:30:23 +00005819 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5820 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00005821
5822 // thisMBB:
5823 // ...
5824 // fallthrough --> loopMBB
5825 BB->addSuccessor(loopMBB);
5826
5827 // loopMBB:
5828 // l[wd]arx dest, ptr
5829 // add r0, dest, incr
5830 // st[wd]cx. r0, ptr
5831 // bne- loopMBB
5832 // fallthrough --> exitMBB
5833 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00005834 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00005835 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005836 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00005837 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
5838 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesend4eb0522008-08-25 22:34:37 +00005839 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00005840 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00005841 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00005842 BB->addSuccessor(loopMBB);
5843 BB->addSuccessor(exitMBB);
5844
5845 // exitMBB:
5846 // ...
5847 BB = exitMBB;
5848 return BB;
5849}
5850
5851MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00005852PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00005853 MachineBasicBlock *BB,
5854 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00005855 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005856 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesena32affb2008-08-28 17:53:09 +00005857 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5858 // In 64 bit mode we have to use 64 bits for addresses, even though the
5859 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
5860 // registers without caring whether they're 32 or 64, but here we're
5861 // doing actual arithmetic on the addresses.
5862 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00005863 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00005864
5865 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5866 MachineFunction *F = BB->getParent();
5867 MachineFunction::iterator It = BB;
5868 ++It;
5869
5870 unsigned dest = MI->getOperand(0).getReg();
5871 unsigned ptrA = MI->getOperand(1).getReg();
5872 unsigned ptrB = MI->getOperand(2).getReg();
5873 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00005874 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00005875
5876 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5877 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5878 F->insert(It, loopMBB);
5879 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00005880 exitMBB->splice(exitMBB->begin(), BB,
5881 llvm::next(MachineBasicBlock::iterator(MI)),
5882 BB->end());
5883 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00005884
5885 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005886 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00005887 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5888 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00005889 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
5890 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
5891 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
5892 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
5893 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
5894 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
5895 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
5896 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
5897 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
5898 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005899 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00005900 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005901 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00005902
5903 // thisMBB:
5904 // ...
5905 // fallthrough --> loopMBB
5906 BB->addSuccessor(loopMBB);
5907
5908 // The 4-byte load must be aligned, while a char or short may be
5909 // anywhere in the word. Hence all this nasty bookkeeping code.
5910 // add ptr1, ptrA, ptrB [copy if ptrA==0]
5911 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00005912 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00005913 // rlwinm ptr, ptr1, 0, 0, 29
5914 // slw incr2, incr, shift
5915 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
5916 // slw mask, mask2, shift
5917 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00005918 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005919 // add tmp, tmpDest, incr2
5920 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00005921 // and tmp3, tmp, mask
5922 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00005923 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00005924 // bne- loopMBB
5925 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005926 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00005927 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00005928 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00005929 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00005930 .addReg(ptrA).addReg(ptrB);
5931 } else {
5932 Ptr1Reg = ptrB;
5933 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00005934 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00005935 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00005936 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00005937 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
5938 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00005939 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00005940 .addReg(Ptr1Reg).addImm(0).addImm(61);
5941 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00005942 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00005943 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00005944 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00005945 .addReg(incr).addReg(ShiftReg);
5946 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00005947 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00005948 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00005949 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
5950 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00005951 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00005952 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00005953 .addReg(Mask2Reg).addReg(ShiftReg);
5954
5955 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00005956 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00005957 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005958 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00005959 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005960 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00005961 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00005962 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00005963 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00005964 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00005965 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00005966 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00005967 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00005968 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00005969 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00005970 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00005971 BB->addSuccessor(loopMBB);
5972 BB->addSuccessor(exitMBB);
5973
5974 // exitMBB:
5975 // ...
5976 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00005977 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
5978 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00005979 return BB;
5980}
5981
Hal Finkel756810f2013-03-21 21:37:52 +00005982llvm::MachineBasicBlock*
5983PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
5984 MachineBasicBlock *MBB) const {
5985 DebugLoc DL = MI->getDebugLoc();
5986 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5987
5988 MachineFunction *MF = MBB->getParent();
5989 MachineRegisterInfo &MRI = MF->getRegInfo();
5990
5991 const BasicBlock *BB = MBB->getBasicBlock();
5992 MachineFunction::iterator I = MBB;
5993 ++I;
5994
5995 // Memory Reference
5996 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
5997 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
5998
5999 unsigned DstReg = MI->getOperand(0).getReg();
6000 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6001 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6002 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6003 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6004
6005 MVT PVT = getPointerTy();
6006 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6007 "Invalid Pointer Size!");
6008 // For v = setjmp(buf), we generate
6009 //
6010 // thisMBB:
6011 // SjLjSetup mainMBB
6012 // bl mainMBB
6013 // v_restore = 1
6014 // b sinkMBB
6015 //
6016 // mainMBB:
6017 // buf[LabelOffset] = LR
6018 // v_main = 0
6019 //
6020 // sinkMBB:
6021 // v = phi(main, restore)
6022 //
6023
6024 MachineBasicBlock *thisMBB = MBB;
6025 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6026 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6027 MF->insert(I, mainMBB);
6028 MF->insert(I, sinkMBB);
6029
6030 MachineInstrBuilder MIB;
6031
6032 // Transfer the remainder of BB and its successor edges to sinkMBB.
6033 sinkMBB->splice(sinkMBB->begin(), MBB,
6034 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
6035 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6036
6037 // Note that the structure of the jmp_buf used here is not compatible
6038 // with that used by libc, and is not designed to be. Specifically, it
6039 // stores only those 'reserved' registers that LLVM does not otherwise
6040 // understand how to spill. Also, by convention, by the time this
6041 // intrinsic is called, Clang has already stored the frame address in the
6042 // first slot of the buffer and stack address in the third. Following the
6043 // X86 target code, we'll store the jump address in the second slot. We also
6044 // need to save the TOC pointer (R2) to handle jumps between shared
6045 // libraries, and that will be stored in the fourth slot. The thread
6046 // identifier (R13) is not affected.
6047
6048 // thisMBB:
6049 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6050 const int64_t TOCOffset = 3 * PVT.getStoreSize();
6051
6052 // Prepare IP either in reg.
6053 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6054 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6055 unsigned BufReg = MI->getOperand(1).getReg();
6056
6057 if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) {
6058 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6059 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006060 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006061 .addReg(BufReg);
6062
6063 MIB.setMemRefs(MMOBegin, MMOEnd);
6064 }
6065
6066 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00006067 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling5e7656b2013-06-07 07:55:53 +00006068 const PPCRegisterInfo *TRI =
6069 static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo());
6070 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00006071
6072 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6073
6074 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6075 .addMBB(mainMBB);
6076 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6077
6078 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6079 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6080
6081 // mainMBB:
6082 // mainDstReg = 0
6083 MIB = BuildMI(mainMBB, DL,
6084 TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
6085
6086 // Store IP
6087 if (PPCSubTarget.isPPC64()) {
6088 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6089 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006090 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006091 .addReg(BufReg);
6092 } else {
6093 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6094 .addReg(LabelReg)
6095 .addImm(LabelOffset)
6096 .addReg(BufReg);
6097 }
6098
6099 MIB.setMemRefs(MMOBegin, MMOEnd);
6100
6101 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6102 mainMBB->addSuccessor(sinkMBB);
6103
6104 // sinkMBB:
6105 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6106 TII->get(PPC::PHI), DstReg)
6107 .addReg(mainDstReg).addMBB(mainMBB)
6108 .addReg(restoreDstReg).addMBB(thisMBB);
6109
6110 MI->eraseFromParent();
6111 return sinkMBB;
6112}
6113
6114MachineBasicBlock *
6115PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6116 MachineBasicBlock *MBB) const {
6117 DebugLoc DL = MI->getDebugLoc();
6118 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6119
6120 MachineFunction *MF = MBB->getParent();
6121 MachineRegisterInfo &MRI = MF->getRegInfo();
6122
6123 // Memory Reference
6124 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6125 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6126
6127 MVT PVT = getPointerTy();
6128 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6129 "Invalid Pointer Size!");
6130
6131 const TargetRegisterClass *RC =
6132 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6133 unsigned Tmp = MRI.createVirtualRegister(RC);
6134 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6135 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6136 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
6137
6138 MachineInstrBuilder MIB;
6139
6140 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6141 const int64_t SPOffset = 2 * PVT.getStoreSize();
6142 const int64_t TOCOffset = 3 * PVT.getStoreSize();
6143
6144 unsigned BufReg = MI->getOperand(0).getReg();
6145
6146 // Reload FP (the jumped-to function may not have had a
6147 // frame pointer, and if so, then its r31 will be restored
6148 // as necessary).
6149 if (PVT == MVT::i64) {
6150 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6151 .addImm(0)
6152 .addReg(BufReg);
6153 } else {
6154 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6155 .addImm(0)
6156 .addReg(BufReg);
6157 }
6158 MIB.setMemRefs(MMOBegin, MMOEnd);
6159
6160 // Reload IP
6161 if (PVT == MVT::i64) {
6162 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006163 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006164 .addReg(BufReg);
6165 } else {
6166 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6167 .addImm(LabelOffset)
6168 .addReg(BufReg);
6169 }
6170 MIB.setMemRefs(MMOBegin, MMOEnd);
6171
6172 // Reload SP
6173 if (PVT == MVT::i64) {
6174 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006175 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006176 .addReg(BufReg);
6177 } else {
6178 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6179 .addImm(SPOffset)
6180 .addReg(BufReg);
6181 }
6182 MIB.setMemRefs(MMOBegin, MMOEnd);
6183
6184 // FIXME: When we also support base pointers, that register must also be
6185 // restored here.
6186
6187 // Reload TOC
6188 if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) {
6189 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006190 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006191 .addReg(BufReg);
6192
6193 MIB.setMemRefs(MMOBegin, MMOEnd);
6194 }
6195
6196 // Jump
6197 BuildMI(*MBB, MI, DL,
6198 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6199 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6200
6201 MI->eraseFromParent();
6202 return MBB;
6203}
6204
Dale Johannesena32affb2008-08-28 17:53:09 +00006205MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00006206PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00006207 MachineBasicBlock *BB) const {
Hal Finkel756810f2013-03-21 21:37:52 +00006208 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6209 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6210 return emitEHSjLjSetJmp(MI, BB);
6211 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6212 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6213 return emitEHSjLjLongJmp(MI, BB);
6214 }
6215
Evan Cheng20350c42006-11-27 23:37:22 +00006216 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00006217
6218 // To "insert" these instructions we actually have to insert their
6219 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00006220 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00006221 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00006222 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00006223
Dan Gohman3b460302008-07-07 23:14:23 +00006224 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00006225
Hal Finkel460e94d2012-06-22 23:10:08 +00006226 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6227 MI->getOpcode() == PPC::SELECT_CC_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00006228 SmallVector<MachineOperand, 2> Cond;
6229 Cond.push_back(MI->getOperand(4));
6230 Cond.push_back(MI->getOperand(1));
6231
Hal Finkel460e94d2012-06-22 23:10:08 +00006232 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00006233 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6234 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
6235 Cond, MI->getOperand(2).getReg(),
6236 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00006237 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6238 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6239 MI->getOpcode() == PPC::SELECT_CC_F4 ||
6240 MI->getOpcode() == PPC::SELECT_CC_F8 ||
6241 MI->getOpcode() == PPC::SELECT_CC_VRRC) {
6242
Evan Cheng32e376f2008-07-12 02:23:19 +00006243
6244 // The incoming instruction knows the destination vreg to set, the
6245 // condition code register to branch on, the true/false values to
6246 // select between, and a branch opcode to use.
6247
6248 // thisMBB:
6249 // ...
6250 // TrueVal = ...
6251 // cmpTY ccX, r1, r2
6252 // bCC copy1MBB
6253 // fallthrough --> copy0MBB
6254 MachineBasicBlock *thisMBB = BB;
6255 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6256 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
6257 unsigned SelectPred = MI->getOperand(4).getImm();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006258 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006259 F->insert(It, copy0MBB);
6260 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006261
6262 // Transfer the remainder of BB and its successor edges to sinkMBB.
6263 sinkMBB->splice(sinkMBB->begin(), BB,
6264 llvm::next(MachineBasicBlock::iterator(MI)),
6265 BB->end());
6266 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6267
Evan Cheng32e376f2008-07-12 02:23:19 +00006268 // Next, add the true and fallthrough blocks as its successors.
6269 BB->addSuccessor(copy0MBB);
6270 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006271
Dan Gohman34396292010-07-06 20:24:04 +00006272 BuildMI(BB, dl, TII->get(PPC::BCC))
6273 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6274
Evan Cheng32e376f2008-07-12 02:23:19 +00006275 // copy0MBB:
6276 // %FalseValue = ...
6277 // # fallthrough to sinkMBB
6278 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006279
Evan Cheng32e376f2008-07-12 02:23:19 +00006280 // Update machine-CFG edges
6281 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006282
Evan Cheng32e376f2008-07-12 02:23:19 +00006283 // sinkMBB:
6284 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6285 // ...
6286 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00006287 BuildMI(*BB, BB->begin(), dl,
6288 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00006289 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
6290 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6291 }
Dale Johannesena32affb2008-08-28 17:53:09 +00006292 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
6293 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
6294 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
6295 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006296 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
6297 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
6298 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
6299 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006300
6301 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
6302 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
6303 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
6304 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006305 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
6306 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
6307 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
6308 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006309
6310 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
6311 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
6312 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
6313 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006314 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
6315 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
6316 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
6317 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006318
6319 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
6320 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
6321 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
6322 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006323 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
6324 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
6325 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
6326 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006327
6328 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006329 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006330 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006331 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006332 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006333 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006334 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006335 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006336
6337 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6338 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6339 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6340 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006341 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6342 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6343 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6344 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006345
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006346 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6347 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6348 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6349 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6350 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6351 BB = EmitAtomicBinary(MI, BB, false, 0);
6352 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6353 BB = EmitAtomicBinary(MI, BB, true, 0);
6354
Evan Cheng32e376f2008-07-12 02:23:19 +00006355 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6356 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6357 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6358
6359 unsigned dest = MI->getOperand(0).getReg();
6360 unsigned ptrA = MI->getOperand(1).getReg();
6361 unsigned ptrB = MI->getOperand(2).getReg();
6362 unsigned oldval = MI->getOperand(3).getReg();
6363 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006364 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006365
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006366 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6367 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6368 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006369 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006370 F->insert(It, loop1MBB);
6371 F->insert(It, loop2MBB);
6372 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006373 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006374 exitMBB->splice(exitMBB->begin(), BB,
6375 llvm::next(MachineBasicBlock::iterator(MI)),
6376 BB->end());
6377 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006378
6379 // thisMBB:
6380 // ...
6381 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006382 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006383
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006384 // loop1MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006385 // l[wd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006386 // cmp[wd] dest, oldval
6387 // bne- midMBB
6388 // loop2MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006389 // st[wd]cx. newval, ptr
6390 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006391 // b exitBB
6392 // midMBB:
6393 // st[wd]cx. dest, ptr
6394 // exitBB:
6395 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006396 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00006397 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006398 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00006399 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006400 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006401 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6402 BB->addSuccessor(loop2MBB);
6403 BB->addSuccessor(midMBB);
6404
6405 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006406 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng32e376f2008-07-12 02:23:19 +00006407 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006408 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006409 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006410 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006411 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006412 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006413
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006414 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006415 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006416 .addReg(dest).addReg(ptrA).addReg(ptrB);
6417 BB->addSuccessor(exitMBB);
6418
Evan Cheng32e376f2008-07-12 02:23:19 +00006419 // exitMBB:
6420 // ...
6421 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00006422 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6423 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6424 // We must use 64-bit registers for addresses when targeting 64-bit,
6425 // since we're actually doing arithmetic on them. Other registers
6426 // can be 32-bit.
6427 bool is64bit = PPCSubTarget.isPPC64();
6428 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6429
6430 unsigned dest = MI->getOperand(0).getReg();
6431 unsigned ptrA = MI->getOperand(1).getReg();
6432 unsigned ptrB = MI->getOperand(2).getReg();
6433 unsigned oldval = MI->getOperand(3).getReg();
6434 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006435 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00006436
6437 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6438 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6439 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6440 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6441 F->insert(It, loop1MBB);
6442 F->insert(It, loop2MBB);
6443 F->insert(It, midMBB);
6444 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006445 exitMBB->splice(exitMBB->begin(), BB,
6446 llvm::next(MachineBasicBlock::iterator(MI)),
6447 BB->end());
6448 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006449
6450 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006451 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006452 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6453 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00006454 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6455 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6456 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6457 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6458 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6459 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6460 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6461 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6462 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6463 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6464 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6465 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6466 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6467 unsigned Ptr1Reg;
6468 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00006469 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00006470 // thisMBB:
6471 // ...
6472 // fallthrough --> loopMBB
6473 BB->addSuccessor(loop1MBB);
6474
6475 // The 4-byte load must be aligned, while a char or short may be
6476 // anywhere in the word. Hence all this nasty bookkeeping code.
6477 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6478 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006479 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00006480 // rlwinm ptr, ptr1, 0, 0, 29
6481 // slw newval2, newval, shift
6482 // slw oldval2, oldval,shift
6483 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6484 // slw mask, mask2, shift
6485 // and newval3, newval2, mask
6486 // and oldval3, oldval2, mask
6487 // loop1MBB:
6488 // lwarx tmpDest, ptr
6489 // and tmp, tmpDest, mask
6490 // cmpw tmp, oldval3
6491 // bne- midMBB
6492 // loop2MBB:
6493 // andc tmp2, tmpDest, mask
6494 // or tmp4, tmp2, newval3
6495 // stwcx. tmp4, ptr
6496 // bne- loop1MBB
6497 // b exitBB
6498 // midMBB:
6499 // stwcx. tmpDest, ptr
6500 // exitBB:
6501 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006502 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00006503 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006504 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006505 .addReg(ptrA).addReg(ptrB);
6506 } else {
6507 Ptr1Reg = ptrB;
6508 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006509 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006510 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006511 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006512 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6513 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006514 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006515 .addReg(Ptr1Reg).addImm(0).addImm(61);
6516 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006517 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006518 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006519 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006520 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006521 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006522 .addReg(oldval).addReg(ShiftReg);
6523 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006524 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00006525 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006526 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6527 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6528 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00006529 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006530 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006531 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006532 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006533 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006534 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006535 .addReg(OldVal2Reg).addReg(MaskReg);
6536
6537 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006538 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006539 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006540 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6541 .addReg(TmpDestReg).addReg(MaskReg);
6542 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00006543 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006544 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006545 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6546 BB->addSuccessor(loop2MBB);
6547 BB->addSuccessor(midMBB);
6548
6549 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006550 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6551 .addReg(TmpDestReg).addReg(MaskReg);
6552 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6553 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6554 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006555 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006556 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006557 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006558 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006559 BB->addSuccessor(loop1MBB);
6560 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006561
Dale Johannesen340d2642008-08-30 00:08:53 +00006562 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006563 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006564 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00006565 BB->addSuccessor(exitMBB);
6566
6567 // exitMBB:
6568 // ...
6569 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006570 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
6571 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00006572 } else if (MI->getOpcode() == PPC::FADDrtz) {
6573 // This pseudo performs an FADD with rounding mode temporarily forced
6574 // to round-to-zero. We emit this via custom inserter since the FPSCR
6575 // is not modeled at the SelectionDAG level.
6576 unsigned Dest = MI->getOperand(0).getReg();
6577 unsigned Src1 = MI->getOperand(1).getReg();
6578 unsigned Src2 = MI->getOperand(2).getReg();
6579 DebugLoc dl = MI->getDebugLoc();
6580
6581 MachineRegisterInfo &RegInfo = F->getRegInfo();
6582 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
6583
6584 // Save FPSCR value.
6585 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
6586
6587 // Set rounding mode to round-to-zero.
6588 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
6589 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
6590
6591 // Perform addition.
6592 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
6593
6594 // Restore FPSCR value.
6595 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkelf8ac57e2013-03-29 19:41:55 +00006596 } else if (MI->getOpcode() == PPC::FRINDrint ||
6597 MI->getOpcode() == PPC::FRINSrint) {
6598 bool isf32 = MI->getOpcode() == PPC::FRINSrint;
6599 unsigned Dest = MI->getOperand(0).getReg();
6600 unsigned Src = MI->getOperand(1).getReg();
6601 DebugLoc dl = MI->getDebugLoc();
6602
6603 MachineRegisterInfo &RegInfo = F->getRegInfo();
6604 unsigned CRReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
6605
6606 // Perform the rounding.
6607 BuildMI(*BB, MI, dl, TII->get(isf32 ? PPC::FRINS : PPC::FRIND), Dest)
6608 .addReg(Src);
6609
6610 // Compare the results.
6611 BuildMI(*BB, MI, dl, TII->get(isf32 ? PPC::FCMPUS : PPC::FCMPUD), CRReg)
6612 .addReg(Dest).addReg(Src);
6613
6614 // If the results were not equal, then set the FPSCR XX bit.
6615 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6616 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6617 F->insert(It, midMBB);
6618 F->insert(It, exitMBB);
6619 exitMBB->splice(exitMBB->begin(), BB,
6620 llvm::next(MachineBasicBlock::iterator(MI)),
6621 BB->end());
6622 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6623
6624 BuildMI(*BB, MI, dl, TII->get(PPC::BCC))
6625 .addImm(PPC::PRED_EQ).addReg(CRReg).addMBB(exitMBB);
6626
6627 BB->addSuccessor(midMBB);
6628 BB->addSuccessor(exitMBB);
6629
6630 BB = midMBB;
6631
6632 // Set the FPSCR XX bit (FE_INEXACT). Note that we cannot just set
6633 // the FI bit here because that will not automatically set XX also,
6634 // and XX is what libm interprets as the FE_INEXACT flag.
6635 BuildMI(BB, dl, TII->get(PPC::MTFSB1)).addImm(/* 38 - 32 = */ 6);
6636 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
6637
6638 BB->addSuccessor(exitMBB);
6639
6640 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00006641 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006642 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00006643 }
Chris Lattner9b577f12005-08-26 21:23:58 +00006644
Dan Gohman34396292010-07-06 20:24:04 +00006645 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00006646 return BB;
6647}
6648
Chris Lattner4211ca92006-04-14 06:01:58 +00006649//===----------------------------------------------------------------------===//
6650// Target Optimization Hooks
6651//===----------------------------------------------------------------------===//
6652
Hal Finkelb0c810f2013-04-03 17:44:56 +00006653SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
6654 DAGCombinerInfo &DCI) const {
Hal Finkel2e103312013-04-03 04:01:11 +00006655 if (DCI.isAfterLegalizeVectorOps())
6656 return SDValue();
6657
Hal Finkelb0c810f2013-04-03 17:44:56 +00006658 EVT VT = Op.getValueType();
6659
6660 if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) ||
6661 (VT == MVT::f64 && PPCSubTarget.hasFRE()) ||
6662 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel2e103312013-04-03 04:01:11 +00006663
6664 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6665 // For the reciprocal, we need to find the zero of the function:
6666 // F(X) = A X - 1 [which has a zero at X = 1/A]
6667 // =>
6668 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
6669 // does not require additional intermediate precision]
6670
6671 // Convergence is quadratic, so we essentially double the number of digits
6672 // correct after every iteration. The minimum architected relative
6673 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6674 // 23 digits and double has 52 digits.
6675 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00006676 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00006677 ++Iterations;
6678
6679 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006680 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006681
6682 SDValue FPOne =
Hal Finkelb0c810f2013-04-03 17:44:56 +00006683 DAG.getConstantFP(1.0, VT.getScalarType());
6684 if (VT.isVector()) {
6685 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00006686 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00006687 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel2e103312013-04-03 04:01:11 +00006688 FPOne, FPOne, FPOne, FPOne);
6689 }
6690
Hal Finkelb0c810f2013-04-03 17:44:56 +00006691 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006692 DCI.AddToWorklist(Est.getNode());
6693
6694 // Newton iterations: Est = Est + Est (1 - Arg * Est)
6695 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00006696 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00006697 DCI.AddToWorklist(NewEst.getNode());
6698
Hal Finkelb0c810f2013-04-03 17:44:56 +00006699 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006700 DCI.AddToWorklist(NewEst.getNode());
6701
Hal Finkelb0c810f2013-04-03 17:44:56 +00006702 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006703 DCI.AddToWorklist(NewEst.getNode());
6704
Hal Finkelb0c810f2013-04-03 17:44:56 +00006705 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006706 DCI.AddToWorklist(Est.getNode());
6707 }
6708
6709 return Est;
6710 }
6711
6712 return SDValue();
6713}
6714
Hal Finkelb0c810f2013-04-03 17:44:56 +00006715SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel2e103312013-04-03 04:01:11 +00006716 DAGCombinerInfo &DCI) const {
6717 if (DCI.isAfterLegalizeVectorOps())
6718 return SDValue();
6719
Hal Finkelb0c810f2013-04-03 17:44:56 +00006720 EVT VT = Op.getValueType();
6721
6722 if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) ||
6723 (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) ||
6724 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel2e103312013-04-03 04:01:11 +00006725
6726 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6727 // For the reciprocal sqrt, we need to find the zero of the function:
6728 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
6729 // =>
6730 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
6731 // As a result, we precompute A/2 prior to the iteration loop.
6732
6733 // Convergence is quadratic, so we essentially double the number of digits
6734 // correct after every iteration. The minimum architected relative
6735 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6736 // 23 digits and double has 52 digits.
6737 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00006738 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00006739 ++Iterations;
6740
6741 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006742 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006743
Hal Finkelb0c810f2013-04-03 17:44:56 +00006744 SDValue FPThreeHalves =
6745 DAG.getConstantFP(1.5, VT.getScalarType());
6746 if (VT.isVector()) {
6747 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00006748 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00006749 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
6750 FPThreeHalves, FPThreeHalves,
6751 FPThreeHalves, FPThreeHalves);
Hal Finkel2e103312013-04-03 04:01:11 +00006752 }
6753
Hal Finkelb0c810f2013-04-03 17:44:56 +00006754 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006755 DCI.AddToWorklist(Est.getNode());
6756
6757 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
6758 // this entire sequence requires only one FP constant.
Hal Finkelb0c810f2013-04-03 17:44:56 +00006759 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006760 DCI.AddToWorklist(HalfArg.getNode());
6761
Hal Finkelb0c810f2013-04-03 17:44:56 +00006762 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00006763 DCI.AddToWorklist(HalfArg.getNode());
6764
6765 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
6766 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00006767 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00006768 DCI.AddToWorklist(NewEst.getNode());
6769
Hal Finkelb0c810f2013-04-03 17:44:56 +00006770 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006771 DCI.AddToWorklist(NewEst.getNode());
6772
Hal Finkelb0c810f2013-04-03 17:44:56 +00006773 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006774 DCI.AddToWorklist(NewEst.getNode());
6775
Hal Finkelb0c810f2013-04-03 17:44:56 +00006776 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00006777 DCI.AddToWorklist(Est.getNode());
6778 }
6779
6780 return Est;
6781 }
6782
6783 return SDValue();
6784}
6785
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00006786// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
6787// not enforce equality of the chain operands.
6788static bool isConsecutiveLS(LSBaseSDNode *LS, LSBaseSDNode *Base,
6789 unsigned Bytes, int Dist,
6790 SelectionDAG &DAG) {
6791 EVT VT = LS->getMemoryVT();
6792 if (VT.getSizeInBits() / 8 != Bytes)
6793 return false;
6794
6795 SDValue Loc = LS->getBasePtr();
6796 SDValue BaseLoc = Base->getBasePtr();
6797 if (Loc.getOpcode() == ISD::FrameIndex) {
6798 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6799 return false;
6800 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6801 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6802 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6803 int FS = MFI->getObjectSize(FI);
6804 int BFS = MFI->getObjectSize(BFI);
6805 if (FS != BFS || FS != (int)Bytes) return false;
6806 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6807 }
6808
6809 // Handle X+C
6810 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6811 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6812 return true;
6813
6814 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6815 const GlobalValue *GV1 = NULL;
6816 const GlobalValue *GV2 = NULL;
6817 int64_t Offset1 = 0;
6818 int64_t Offset2 = 0;
6819 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6820 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6821 if (isGA1 && isGA2 && GV1 == GV2)
6822 return Offset1 == (Offset2 + Dist*Bytes);
6823 return false;
6824}
6825
Hal Finkel7d8a6912013-05-26 18:08:30 +00006826// Return true is there is a nearyby consecutive load to the one provided
6827// (regardless of alignment). We search up and down the chain, looking though
6828// token factors and other loads (but nothing else). As a result, a true
6829// results indicates that it is safe to create a new consecutive load adjacent
6830// to the load provided.
6831static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
6832 SDValue Chain = LD->getChain();
6833 EVT VT = LD->getMemoryVT();
6834
6835 SmallSet<SDNode *, 16> LoadRoots;
6836 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
6837 SmallSet<SDNode *, 16> Visited;
6838
6839 // First, search up the chain, branching to follow all token-factor operands.
6840 // If we find a consecutive load, then we're done, otherwise, record all
6841 // nodes just above the top-level loads and token factors.
6842 while (!Queue.empty()) {
6843 SDNode *ChainNext = Queue.pop_back_val();
6844 if (!Visited.insert(ChainNext))
6845 continue;
6846
6847 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00006848 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00006849 return true;
6850
6851 if (!Visited.count(ChainLD->getChain().getNode()))
6852 Queue.push_back(ChainLD->getChain().getNode());
6853 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
6854 for (SDNode::op_iterator O = ChainNext->op_begin(),
6855 OE = ChainNext->op_end(); O != OE; ++O)
6856 if (!Visited.count(O->getNode()))
6857 Queue.push_back(O->getNode());
6858 } else
6859 LoadRoots.insert(ChainNext);
6860 }
6861
6862 // Second, search down the chain, starting from the top-level nodes recorded
6863 // in the first phase. These top-level nodes are the nodes just above all
6864 // loads and token factors. Starting with their uses, recursively look though
6865 // all loads (just the chain uses) and token factors to find a consecutive
6866 // load.
6867 Visited.clear();
6868 Queue.clear();
6869
6870 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
6871 IE = LoadRoots.end(); I != IE; ++I) {
6872 Queue.push_back(*I);
6873
6874 while (!Queue.empty()) {
6875 SDNode *LoadRoot = Queue.pop_back_val();
6876 if (!Visited.insert(LoadRoot))
6877 continue;
6878
6879 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00006880 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00006881 return true;
6882
6883 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
6884 UE = LoadRoot->use_end(); UI != UE; ++UI)
6885 if (((isa<LoadSDNode>(*UI) &&
6886 cast<LoadSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
6887 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
6888 Queue.push_back(*UI);
6889 }
6890 }
6891
6892 return false;
6893}
6894
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006895SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
6896 DAGCombinerInfo &DCI) const {
Dan Gohman57c732b2010-04-21 01:34:56 +00006897 const TargetMachine &TM = getTargetMachine();
Chris Lattnerf4184352006-03-01 04:57:39 +00006898 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006899 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00006900 switch (N->getOpcode()) {
6901 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00006902 case PPCISD::SHL:
6903 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00006904 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00006905 return N->getOperand(0);
6906 }
6907 break;
6908 case PPCISD::SRL:
6909 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00006910 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00006911 return N->getOperand(0);
6912 }
6913 break;
6914 case PPCISD::SRA:
6915 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00006916 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00006917 C->isAllOnesValue()) // -1 >>s V -> -1.
6918 return N->getOperand(0);
6919 }
6920 break;
Hal Finkel2e103312013-04-03 04:01:11 +00006921 case ISD::FDIV: {
6922 assert(TM.Options.UnsafeFPMath &&
6923 "Reciprocal estimates require UnsafeFPMath");
Scott Michelcf0da6c2009-02-17 22:15:04 +00006924
Hal Finkel2e103312013-04-03 04:01:11 +00006925 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00006926 SDValue RV =
6927 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00006928 if (RV.getNode() != 0) {
6929 DCI.AddToWorklist(RV.getNode());
6930 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
6931 N->getOperand(0), RV);
6932 }
Hal Finkelf96c18e2013-04-04 22:44:12 +00006933 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
6934 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
6935 SDValue RV =
6936 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
6937 DCI);
6938 if (RV.getNode() != 0) {
6939 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006940 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00006941 N->getValueType(0), RV);
6942 DCI.AddToWorklist(RV.getNode());
6943 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
6944 N->getOperand(0), RV);
6945 }
6946 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
6947 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
6948 SDValue RV =
6949 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
6950 DCI);
6951 if (RV.getNode() != 0) {
6952 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006953 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00006954 N->getValueType(0), RV,
6955 N->getOperand(1).getOperand(1));
6956 DCI.AddToWorklist(RV.getNode());
6957 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
6958 N->getOperand(0), RV);
6959 }
Hal Finkel2e103312013-04-03 04:01:11 +00006960 }
6961
Hal Finkelb0c810f2013-04-03 17:44:56 +00006962 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00006963 if (RV.getNode() != 0) {
6964 DCI.AddToWorklist(RV.getNode());
6965 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
6966 N->getOperand(0), RV);
6967 }
6968
6969 }
6970 break;
6971 case ISD::FSQRT: {
6972 assert(TM.Options.UnsafeFPMath &&
6973 "Reciprocal estimates require UnsafeFPMath");
6974
6975 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
6976 // reciprocal sqrt.
Hal Finkelb0c810f2013-04-03 17:44:56 +00006977 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00006978 if (RV.getNode() != 0) {
6979 DCI.AddToWorklist(RV.getNode());
Hal Finkelb0c810f2013-04-03 17:44:56 +00006980 RV = DAGCombineFastRecip(RV, DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00006981 if (RV.getNode() != 0)
6982 return RV;
6983 }
6984
6985 }
6986 break;
Chris Lattnerf4184352006-03-01 04:57:39 +00006987 case ISD::SINT_TO_FP:
Chris Lattnera35f3062006-06-16 17:34:12 +00006988 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattner4a66d692006-03-22 05:30:33 +00006989 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
6990 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
6991 // We allow the src/dst to be either f32/f64, but the intermediate
6992 // type must be i64.
Owen Anderson9f944592009-08-11 20:47:22 +00006993 if (N->getOperand(0).getValueType() == MVT::i64 &&
6994 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006995 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00006996 if (Val.getValueType() == MVT::f32) {
6997 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006998 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00006999 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007000
Owen Anderson9f944592009-08-11 20:47:22 +00007001 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007002 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007003 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007004 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007005 if (N->getValueType(0) == MVT::f32) {
7006 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner72733e52008-01-17 07:00:52 +00007007 DAG.getIntPtrConstant(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007008 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007009 }
7010 return Val;
Owen Anderson9f944592009-08-11 20:47:22 +00007011 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007012 // If the intermediate type is i32, we can avoid the load/store here
7013 // too.
Chris Lattnerf4184352006-03-01 04:57:39 +00007014 }
Chris Lattnerf4184352006-03-01 04:57:39 +00007015 }
7016 }
7017 break;
Chris Lattner27f53452006-03-01 05:50:56 +00007018 case ISD::STORE:
7019 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
7020 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnerf5b46f72008-01-18 16:54:56 +00007021 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00007022 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00007023 N->getOperand(1).getValueType() == MVT::i32 &&
7024 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007025 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007026 if (Val.getValueType() == MVT::f32) {
7027 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007028 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007029 }
Owen Anderson9f944592009-08-11 20:47:22 +00007030 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007031 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007032
Hal Finkel60c75102013-04-01 15:37:53 +00007033 SDValue Ops[] = {
7034 N->getOperand(0), Val, N->getOperand(2),
7035 DAG.getValueType(N->getOperand(1).getValueType())
7036 };
7037
7038 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
7039 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
7040 cast<StoreSDNode>(N)->getMemoryVT(),
7041 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00007042 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00007043 return Val;
7044 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007045
Chris Lattnera7976d32006-07-10 20:56:58 +00007046 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00007047 if (cast<StoreSDNode>(N)->isUnindexed() &&
7048 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00007049 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00007050 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00007051 N->getOperand(1).getValueType() == MVT::i16 ||
7052 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00007053 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007054 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007055 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00007056 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00007057 if (BSwapOp.getValueType() == MVT::i16)
7058 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00007059
Dan Gohman48b185d2009-09-25 20:36:54 +00007060 SDValue Ops[] = {
7061 N->getOperand(0), BSwapOp, N->getOperand(2),
7062 DAG.getValueType(N->getOperand(1).getValueType())
7063 };
7064 return
7065 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
7066 Ops, array_lengthof(Ops),
7067 cast<StoreSDNode>(N)->getMemoryVT(),
7068 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00007069 }
7070 break;
Hal Finkelcf2e9082013-05-24 23:00:14 +00007071 case ISD::LOAD: {
7072 LoadSDNode *LD = cast<LoadSDNode>(N);
7073 EVT VT = LD->getValueType(0);
7074 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
7075 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
7076 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
7077 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
7078 DCI.getDAGCombineLevel() == AfterLegalizeTypes &&
7079 LD->getAlignment() < ABIAlignment) {
7080 // This is a type-legal unaligned Altivec load.
7081 SDValue Chain = LD->getChain();
7082 SDValue Ptr = LD->getBasePtr();
7083
7084 // This implements the loading of unaligned vectors as described in
7085 // the venerable Apple Velocity Engine overview. Specifically:
7086 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
7087 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
7088 //
7089 // The general idea is to expand a sequence of one or more unaligned
7090 // loads into a alignment-based permutation-control instruction (lvsl),
7091 // a series of regular vector loads (which always truncate their
7092 // input address to an aligned address), and a series of permutations.
7093 // The results of these permutations are the requested loaded values.
7094 // The trick is that the last "extra" load is not taken from the address
7095 // you might suspect (sizeof(vector) bytes after the last requested
7096 // load), but rather sizeof(vector) - 1 bytes after the last
7097 // requested vector. The point of this is to avoid a page fault if the
7098 // base address happend to be aligned. This works because if the base
7099 // address is aligned, then adding less than a full vector length will
7100 // cause the last vector in the sequence to be (re)loaded. Otherwise,
7101 // the next vector will be fetched as you might suspect was necessary.
7102
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007103 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00007104 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007105 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
7106 // optimization later.
Hal Finkelcf2e9082013-05-24 23:00:14 +00007107 SDValue PermCntl = BuildIntrinsicOp(Intrinsic::ppc_altivec_lvsl, Ptr,
7108 DAG, dl, MVT::v16i8);
7109
7110 // Refine the alignment of the original load (a "new" load created here
7111 // which was identical to the first except for the alignment would be
7112 // merged with the existing node regardless).
7113 MachineFunction &MF = DAG.getMachineFunction();
7114 MachineMemOperand *MMO =
7115 MF.getMachineMemOperand(LD->getPointerInfo(),
7116 LD->getMemOperand()->getFlags(),
7117 LD->getMemoryVT().getStoreSize(),
7118 ABIAlignment);
7119 LD->refineAlignment(MMO);
7120 SDValue BaseLoad = SDValue(LD, 0);
7121
7122 // Note that the value of IncOffset (which is provided to the next
7123 // load's pointer info offset value, and thus used to calculate the
7124 // alignment), and the value of IncValue (which is actually used to
7125 // increment the pointer value) are different! This is because we
7126 // require the next load to appear to be aligned, even though it
7127 // is actually offset from the base pointer by a lesser amount.
7128 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +00007129 int IncValue = IncOffset;
7130
7131 // Walk (both up and down) the chain looking for another load at the real
7132 // (aligned) offset (the alignment of the other load does not matter in
7133 // this case). If found, then do not use the offset reduction trick, as
7134 // that will prevent the loads from being later combined (as they would
7135 // otherwise be duplicates).
7136 if (!findConsecutiveLoad(LD, DAG))
7137 --IncValue;
7138
Hal Finkelcf2e9082013-05-24 23:00:14 +00007139 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
7140 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
7141
Hal Finkelcf2e9082013-05-24 23:00:14 +00007142 SDValue ExtraLoad =
7143 DAG.getLoad(VT, dl, Chain, Ptr,
7144 LD->getPointerInfo().getWithOffset(IncOffset),
7145 LD->isVolatile(), LD->isNonTemporal(),
7146 LD->isInvariant(), ABIAlignment);
7147
7148 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7149 BaseLoad.getValue(1), ExtraLoad.getValue(1));
7150
7151 if (BaseLoad.getValueType() != MVT::v4i32)
7152 BaseLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, BaseLoad);
7153
7154 if (ExtraLoad.getValueType() != MVT::v4i32)
7155 ExtraLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ExtraLoad);
7156
7157 SDValue Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
7158 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
7159
7160 if (VT != MVT::v4i32)
7161 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
7162
7163 // Now we need to be really careful about how we update the users of the
7164 // original load. We cannot just call DCI.CombineTo (or
7165 // DAG.ReplaceAllUsesWith for that matter), because the load still has
7166 // uses created here (the permutation for example) that need to stay.
7167 SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
7168 while (UI != UE) {
7169 SDUse &Use = UI.getUse();
7170 SDNode *User = *UI;
7171 // Note: BaseLoad is checked here because it might not be N, but a
7172 // bitcast of N.
7173 if (User == Perm.getNode() || User == BaseLoad.getNode() ||
7174 User == TF.getNode() || Use.getResNo() > 1) {
7175 ++UI;
7176 continue;
7177 }
7178
7179 SDValue To = Use.getResNo() ? TF : Perm;
7180 ++UI;
7181
7182 SmallVector<SDValue, 8> Ops;
7183 for (SDNode::op_iterator O = User->op_begin(),
7184 OE = User->op_end(); O != OE; ++O) {
7185 if (*O == Use)
7186 Ops.push_back(To);
7187 else
7188 Ops.push_back(*O);
7189 }
7190
7191 DAG.UpdateNodeOperands(User, Ops.data(), Ops.size());
7192 }
7193
7194 return SDValue(N, 0);
7195 }
7196 }
7197 break;
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00007198 case ISD::INTRINSIC_WO_CHAIN:
7199 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() ==
7200 Intrinsic::ppc_altivec_lvsl &&
7201 N->getOperand(1)->getOpcode() == ISD::ADD) {
7202 SDValue Add = N->getOperand(1);
7203
7204 if (DAG.MaskedValueIsZero(Add->getOperand(1),
7205 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
7206 Add.getValueType().getScalarType().getSizeInBits()))) {
7207 SDNode *BasePtr = Add->getOperand(0).getNode();
7208 for (SDNode::use_iterator UI = BasePtr->use_begin(),
7209 UE = BasePtr->use_end(); UI != UE; ++UI) {
7210 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
7211 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
7212 Intrinsic::ppc_altivec_lvsl) {
7213 // We've found another LVSL, and this address if an aligned
7214 // multiple of that one. The results will be the same, so use the
7215 // one we've just found instead.
7216
7217 return SDValue(*UI, 0);
7218 }
7219 }
7220 }
7221 }
Chris Lattnera7976d32006-07-10 20:56:58 +00007222 case ISD::BSWAP:
7223 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +00007224 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +00007225 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007226 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
7227 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00007228 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00007229 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007230 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00007231 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +00007232 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007233 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +00007234 LD->getChain(), // Chain
7235 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007236 DAG.getValueType(N->getValueType(0)) // VT
7237 };
Dan Gohman48b185d2009-09-25 20:36:54 +00007238 SDValue BSLoad =
7239 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +00007240 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
7241 MVT::i64 : MVT::i32, MVT::Other),
Hal Finkel93492fa2013-03-28 19:43:12 +00007242 Ops, 3, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00007243
Scott Michelcf0da6c2009-02-17 22:15:04 +00007244 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007245 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +00007246 if (N->getValueType(0) == MVT::i16)
7247 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007248
Chris Lattnera7976d32006-07-10 20:56:58 +00007249 // First, combine the bswap away. This makes the value produced by the
7250 // load dead.
7251 DCI.CombineTo(N, ResVal);
7252
7253 // Next, combine the load away, we give it a bogus result value but a real
7254 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +00007255 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00007256
Chris Lattnera7976d32006-07-10 20:56:58 +00007257 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007258 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +00007259 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007260
Chris Lattner27f53452006-03-01 05:50:56 +00007261 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +00007262 case PPCISD::VCMP: {
7263 // If a VCMPo node already exists with exactly the same operands as this
7264 // node, use its result instead of this node (VCMPo computes both a CR6 and
7265 // a normal output).
7266 //
7267 if (!N->getOperand(0).hasOneUse() &&
7268 !N->getOperand(1).hasOneUse() &&
7269 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00007270
Chris Lattnerd4058a52006-03-31 06:02:07 +00007271 // Scan all of the users of the LHS, looking for VCMPo's that match.
7272 SDNode *VCMPoNode = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007273
Gabor Greiff304a7a2008-08-28 21:40:38 +00007274 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +00007275 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
7276 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007277 if (UI->getOpcode() == PPCISD::VCMPo &&
7278 UI->getOperand(1) == N->getOperand(1) &&
7279 UI->getOperand(2) == N->getOperand(2) &&
7280 UI->getOperand(0) == N->getOperand(0)) {
7281 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +00007282 break;
7283 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007284
Chris Lattner518834c2006-04-18 18:28:22 +00007285 // If there is no VCMPo node, or if the flag value has a single use, don't
7286 // transform this.
7287 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
7288 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007289
7290 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +00007291 // chain, this transformation is more complex. Note that multiple things
7292 // could use the value result, which we should ignore.
7293 SDNode *FlagUser = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007294 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner518834c2006-04-18 18:28:22 +00007295 FlagUser == 0; ++UI) {
7296 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007297 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +00007298 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007299 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +00007300 FlagUser = User;
7301 break;
7302 }
7303 }
7304 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007305
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00007306 // If the user is a MFOCRF instruction, we know this is safe.
7307 // Otherwise we give up for right now.
7308 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007309 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +00007310 }
7311 break;
7312 }
Chris Lattner9754d142006-04-18 17:59:36 +00007313 case ISD::BR_CC: {
7314 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00007315 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +00007316 // lowering is done pre-legalize, because the legalizer lowers the predicate
7317 // compare down to code that is difficult to reassemble.
7318 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007319 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +00007320
7321 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
7322 // value. If so, pass-through the AND to get to the intrinsic.
7323 if (LHS.getOpcode() == ISD::AND &&
7324 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
7325 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
7326 Intrinsic::ppc_is_decremented_ctr_nonzero &&
7327 isa<ConstantSDNode>(LHS.getOperand(1)) &&
7328 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
7329 isZero())
7330 LHS = LHS.getOperand(0);
7331
7332 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
7333 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
7334 Intrinsic::ppc_is_decremented_ctr_nonzero &&
7335 isa<ConstantSDNode>(RHS)) {
7336 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
7337 "Counter decrement comparison is not EQ or NE");
7338
7339 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
7340 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
7341 (CC == ISD::SETNE && !Val);
7342
7343 // We now need to make the intrinsic dead (it cannot be instruction
7344 // selected).
7345 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
7346 assert(LHS.getNode()->hasOneUse() &&
7347 "Counter decrement has more than one use");
7348
7349 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
7350 N->getOperand(0), N->getOperand(4));
7351 }
7352
Chris Lattner9754d142006-04-18 17:59:36 +00007353 int CompareOpc;
7354 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007355
Chris Lattner9754d142006-04-18 17:59:36 +00007356 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
7357 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
7358 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
7359 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00007360
Chris Lattner9754d142006-04-18 17:59:36 +00007361 // If this is a comparison against something other than 0/1, then we know
7362 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +00007363 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00007364 if (Val != 0 && Val != 1) {
7365 if (CC == ISD::SETEQ) // Cond never true, remove branch.
7366 return N->getOperand(0);
7367 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +00007368 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +00007369 N->getOperand(0), N->getOperand(4));
7370 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007371
Chris Lattner9754d142006-04-18 17:59:36 +00007372 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007373
Chris Lattner9754d142006-04-18 17:59:36 +00007374 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007375 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007376 LHS.getOperand(2), // LHS of compare
7377 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +00007378 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00007379 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00007380 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesenf80493b2009-02-05 22:07:54 +00007381 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007382
Chris Lattner9754d142006-04-18 17:59:36 +00007383 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007384 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +00007385 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +00007386 default: // Can't happen, don't crash on invalid number though.
7387 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007388 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +00007389 break;
7390 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007391 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +00007392 break;
7393 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007394 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +00007395 break;
7396 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00007397 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +00007398 break;
7399 }
7400
Owen Anderson9f944592009-08-11 20:47:22 +00007401 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
7402 DAG.getConstant(CompOpc, MVT::i32),
7403 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +00007404 N->getOperand(4), CompNode.getValue(1));
7405 }
7406 break;
7407 }
Chris Lattnerf4184352006-03-01 04:57:39 +00007408 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007409
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007410 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +00007411}
7412
Chris Lattner4211ca92006-04-14 06:01:58 +00007413//===----------------------------------------------------------------------===//
7414// Inline Assembly Support
7415//===----------------------------------------------------------------------===//
7416
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007417void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Scott Michelcf0da6c2009-02-17 22:15:04 +00007418 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +00007419 APInt &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +00007420 const SelectionDAG &DAG,
Chris Lattnerc5287c02006-04-02 06:26:07 +00007421 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00007422 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +00007423 switch (Op.getOpcode()) {
7424 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +00007425 case PPCISD::LBRX: {
7426 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +00007427 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +00007428 KnownZero = 0xFFFF0000;
7429 break;
7430 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00007431 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00007432 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +00007433 default: break;
7434 case Intrinsic::ppc_altivec_vcmpbfp_p:
7435 case Intrinsic::ppc_altivec_vcmpeqfp_p:
7436 case Intrinsic::ppc_altivec_vcmpequb_p:
7437 case Intrinsic::ppc_altivec_vcmpequh_p:
7438 case Intrinsic::ppc_altivec_vcmpequw_p:
7439 case Intrinsic::ppc_altivec_vcmpgefp_p:
7440 case Intrinsic::ppc_altivec_vcmpgtfp_p:
7441 case Intrinsic::ppc_altivec_vcmpgtsb_p:
7442 case Intrinsic::ppc_altivec_vcmpgtsh_p:
7443 case Intrinsic::ppc_altivec_vcmpgtsw_p:
7444 case Intrinsic::ppc_altivec_vcmpgtub_p:
7445 case Intrinsic::ppc_altivec_vcmpgtuh_p:
7446 case Intrinsic::ppc_altivec_vcmpgtuw_p:
7447 KnownZero = ~1U; // All bits but the low one are known to be zero.
7448 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007449 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00007450 }
7451 }
7452}
7453
7454
Chris Lattnerd6855142007-03-25 02:14:49 +00007455/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +00007456/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007457PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00007458PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
7459 if (Constraint.size() == 1) {
7460 switch (Constraint[0]) {
7461 default: break;
7462 case 'b':
7463 case 'r':
7464 case 'f':
7465 case 'v':
7466 case 'y':
7467 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +00007468 case 'Z':
7469 // FIXME: While Z does indicate a memory constraint, it specifically
7470 // indicates an r+r address (used in conjunction with the 'y' modifier
7471 // in the replacement string). Currently, we're forcing the base
7472 // register to be r0 in the asm printer (which is interpreted as zero)
7473 // and forming the complete address in the second register. This is
7474 // suboptimal.
7475 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +00007476 }
7477 }
7478 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +00007479}
7480
John Thompsone8360b72010-10-29 17:29:13 +00007481/// Examine constraint type and operand type and determine a weight value.
7482/// This object must already have been set up with the operand type
7483/// and the current alternative constraint selected.
7484TargetLowering::ConstraintWeight
7485PPCTargetLowering::getSingleConstraintMatchWeight(
7486 AsmOperandInfo &info, const char *constraint) const {
7487 ConstraintWeight weight = CW_Invalid;
7488 Value *CallOperandVal = info.CallOperandVal;
7489 // If we don't have a value, we can't do a match,
7490 // but allow it at the lowest weight.
7491 if (CallOperandVal == NULL)
7492 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00007493 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00007494 // Look at the constraint type.
7495 switch (*constraint) {
7496 default:
7497 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7498 break;
7499 case 'b':
7500 if (type->isIntegerTy())
7501 weight = CW_Register;
7502 break;
7503 case 'f':
7504 if (type->isFloatTy())
7505 weight = CW_Register;
7506 break;
7507 case 'd':
7508 if (type->isDoubleTy())
7509 weight = CW_Register;
7510 break;
7511 case 'v':
7512 if (type->isVectorTy())
7513 weight = CW_Register;
7514 break;
7515 case 'y':
7516 weight = CW_Register;
7517 break;
Hal Finkel4f24c622012-11-05 18:18:42 +00007518 case 'Z':
7519 weight = CW_Memory;
7520 break;
John Thompsone8360b72010-10-29 17:29:13 +00007521 }
7522 return weight;
7523}
7524
Scott Michelcf0da6c2009-02-17 22:15:04 +00007525std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner584a11a2006-11-02 01:44:04 +00007526PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00007527 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +00007528 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +00007529 // GCC RS6000 Constraint Letters
7530 switch (Constraint[0]) {
7531 case 'b': // R1-R31
Hal Finkel638a9fa2013-03-19 18:51:05 +00007532 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
7533 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
7534 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00007535 case 'r': // R0-R31
Owen Anderson9f944592009-08-11 20:47:22 +00007536 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +00007537 return std::make_pair(0U, &PPC::G8RCRegClass);
7538 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00007539 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00007540 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +00007541 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00007542 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +00007543 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00007544 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007545 case 'v':
Craig Topperabadc662012-04-20 06:31:50 +00007546 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00007547 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +00007548 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00007549 }
7550 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007551
Chris Lattner584a11a2006-11-02 01:44:04 +00007552 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner01513612006-01-31 19:20:21 +00007553}
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007554
Chris Lattner584a11a2006-11-02 01:44:04 +00007555
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007556/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +00007557/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +00007558void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00007559 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007560 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00007561 SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007562 SDValue Result(0,0);
Eric Christopher0713a9d2011-06-08 23:55:35 +00007563
Eric Christopherde9399b2011-06-02 23:16:42 +00007564 // Only support length 1 constraints.
7565 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +00007566
Eric Christopherde9399b2011-06-02 23:16:42 +00007567 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007568 switch (Letter) {
7569 default: break;
7570 case 'I':
7571 case 'J':
7572 case 'K':
7573 case 'L':
7574 case 'M':
7575 case 'N':
7576 case 'O':
7577 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +00007578 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007579 if (!CST) return; // Must be an immediate to match.
Dan Gohmaneffb8942008-09-12 16:56:44 +00007580 unsigned Value = CST->getZExtValue();
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007581 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007582 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007583 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007584 if ((short)Value == (int)Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007585 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007586 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007587 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
7588 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007589 if ((short)Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007590 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007591 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007592 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007593 if ((Value >> 16) == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007594 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007595 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007596 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007597 if (Value > 31)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007598 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007599 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007600 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007601 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007602 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007603 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007604 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007605 if (Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007606 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007607 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007608 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00007609 if ((short)-Value == (int)-Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007610 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00007611 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007612 }
7613 break;
7614 }
7615 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007616
Gabor Greiff304a7a2008-08-28 21:40:38 +00007617 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00007618 Ops.push_back(Result);
7619 return;
7620 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007621
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007622 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +00007623 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +00007624}
Evan Cheng2dd2c652006-03-13 23:20:37 +00007625
Chris Lattner1eb94d92007-03-30 23:15:24 +00007626// isLegalAddressingMode - Return true if the addressing mode represented
7627// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007628bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00007629 Type *Ty) const {
Chris Lattner1eb94d92007-03-30 23:15:24 +00007630 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelcf0da6c2009-02-17 22:15:04 +00007631
Chris Lattner1eb94d92007-03-30 23:15:24 +00007632 // PPC allows a sign-extended 16-bit immediate field.
7633 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
7634 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007635
Chris Lattner1eb94d92007-03-30 23:15:24 +00007636 // No global is ever allowed as a base.
7637 if (AM.BaseGV)
7638 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007639
7640 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +00007641 switch (AM.Scale) {
7642 case 0: // "r+i" or just "i", depending on HasBaseReg.
7643 break;
7644 case 1:
7645 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
7646 return false;
7647 // Otherwise we have r+r or r+i.
7648 break;
7649 case 2:
7650 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
7651 return false;
7652 // Allow 2*r as r+r.
7653 break;
Chris Lattner19ccd622007-04-09 22:10:05 +00007654 default:
7655 // No other scales are supported.
7656 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +00007657 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007658
Chris Lattner1eb94d92007-03-30 23:15:24 +00007659 return true;
7660}
7661
Dan Gohman21cea8a2010-04-17 15:26:15 +00007662SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
7663 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00007664 MachineFunction &MF = DAG.getMachineFunction();
7665 MachineFrameInfo *MFI = MF.getFrameInfo();
7666 MFI->setReturnAddressIsTaken(true);
7667
Andrew Trickef9de2a2013-05-25 02:42:55 +00007668 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007669 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +00007670
Dale Johannesen81bfca72010-05-03 22:59:34 +00007671 // Make sure the function does not optimize away the store of the RA to
7672 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +00007673 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +00007674 FuncInfo->setLRStoreRequired();
7675 bool isPPC64 = PPCSubTarget.isPPC64();
7676 bool isDarwinABI = PPCSubTarget.isDarwinABI();
7677
7678 if (Depth > 0) {
7679 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7680 SDValue Offset =
Wesley Peck527da1b2010-11-23 03:31:01 +00007681
Anton Korobeynikov2f931282011-01-10 12:39:04 +00007682 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen81bfca72010-05-03 22:59:34 +00007683 isPPC64? MVT::i64 : MVT::i32);
7684 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
7685 DAG.getNode(ISD::ADD, dl, getPointerTy(),
7686 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00007687 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007688 }
Chris Lattnerf6a81562007-12-08 06:59:59 +00007689
Chris Lattnerf6a81562007-12-08 06:59:59 +00007690 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007691 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007692 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00007693 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +00007694}
7695
Dan Gohman21cea8a2010-04-17 15:26:15 +00007696SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
7697 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007698 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007699 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00007700
Owen Anderson53aa7a92009-08-10 22:56:29 +00007701 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00007702 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007703
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00007704 MachineFunction &MF = DAG.getMachineFunction();
7705 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +00007706 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +00007707
7708 // Naked functions never have a frame pointer, and so we use r1. For all
7709 // other functions, this decision must be delayed until during PEI.
7710 unsigned FrameReg;
7711 if (MF.getFunction()->getAttributes().hasAttribute(
7712 AttributeSet::FunctionIndex, Attribute::Naked))
7713 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
7714 else
7715 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
7716
Dale Johannesen81bfca72010-05-03 22:59:34 +00007717 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
7718 PtrVT);
7719 while (Depth--)
7720 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00007721 FrameAddr, MachinePointerInfo(), false, false,
7722 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00007723 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00007724}
Dan Gohmanc14e5222008-10-21 03:41:46 +00007725
7726bool
7727PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7728 // The PowerPC target isn't yet aware of offsets.
7729 return false;
7730}
Tilmann Schellerb93960d2009-07-03 06:45:56 +00007731
Evan Chengd9929f02010-04-01 20:10:42 +00007732/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +00007733/// and store operations as a result of memset, memcpy, and memmove
7734/// lowering. If DstAlign is zero that means it's safe to destination
7735/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
7736/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +00007737/// probably because the source does not need to be loaded. If 'IsMemset' is
7738/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
7739/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
7740/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +00007741/// It returns EVT::Other if the type should be determined using generic
7742/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +00007743EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
7744 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00007745 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00007746 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +00007747 MachineFunction &MF) const {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00007748 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +00007749 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00007750 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00007751 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00007752 }
7753}
Hal Finkel88ed4e32012-04-01 19:23:08 +00007754
Hal Finkel8d7fbc92013-03-15 15:27:13 +00007755bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
7756 bool *Fast) const {
7757 if (DisablePPCUnaligned)
7758 return false;
7759
7760 // PowerPC supports unaligned memory access for simple non-vector types.
7761 // Although accessing unaligned addresses is not as efficient as accessing
7762 // aligned addresses, it is generally more efficient than manual expansion,
7763 // and generally only traps for software emulation when crossing page
7764 // boundaries.
7765
7766 if (!VT.isSimple())
7767 return false;
7768
7769 if (VT.getSimpleVT().isVector())
7770 return false;
7771
7772 if (VT == MVT::ppcf128)
7773 return false;
7774
7775 if (Fast)
7776 *Fast = true;
7777
7778 return true;
7779}
7780
Hal Finkel0a479ae2012-06-22 00:49:52 +00007781/// isFMAFasterThanMulAndAdd - Return true if an FMA operation is faster than
7782/// a pair of mul and add instructions. fmuladd intrinsics will be expanded to
7783/// FMAs when this method returns true (and FMAs are legal), otherwise fmuladd
7784/// is expanded to mul + add.
7785bool PPCTargetLowering::isFMAFasterThanMulAndAdd(EVT VT) const {
7786 if (!VT.isSimple())
7787 return false;
7788
7789 switch (VT.getSimpleVT().SimpleTy) {
7790 case MVT::f32:
7791 case MVT::f64:
7792 case MVT::v4f32:
7793 return true;
7794 default:
7795 break;
7796 }
7797
7798 return false;
7799}
7800
Hal Finkel88ed4e32012-04-01 19:23:08 +00007801Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel4e9f1a82012-06-10 19:32:29 +00007802 if (DisableILPPref)
7803 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +00007804
Hal Finkel4e9f1a82012-06-10 19:32:29 +00007805 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +00007806}
7807