blob: 3ef83f5ecc7b11181065ba8d0cb9bbe82a3689dd [file] [log] [blame]
Nate Begeman0b71e002005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattnerf22556d2005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf22556d2005-08-16 17:14:42 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattnerf22556d2005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6f3b9542005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Jim Laskey48850c12006-11-16 22:43:37 +000016#include "PPCMachineFunctionInfo.h"
Bill Wendlingdd3fe942010-03-12 02:00:43 +000017#include "PPCPerfectShuffle.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000018#include "PPCTargetMachine.h"
Bill Schmidt22d40dc2013-05-13 19:34:37 +000019#include "PPCTargetObjectFile.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000020#include "llvm/ADT/STLExtras.h"
Chris Lattner4f2e4e02007-03-06 00:59:59 +000021#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner9b577f12005-08-26 21:23:58 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000026#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000027#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/Constants.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/Intrinsics.h"
Chris Lattnerce645542006-11-10 02:08:47 +000033#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
Craig Topperb25fda92012-03-17 18:46:09 +000035#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000036#include "llvm/Support/raw_ostream.h"
Craig Topperb25fda92012-03-17 18:46:09 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000038using namespace llvm;
39
Hal Finkel595817e2012-06-04 02:21:00 +000040static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
41cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000042
Hal Finkel4e9f1a82012-06-10 19:32:29 +000043static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
44cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
45
Hal Finkel8d7fbc92013-03-15 15:27:13 +000046static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
47cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
48
Hal Finkel940ab932014-02-28 00:27:01 +000049// FIXME: Remove this once the bug has been fixed!
50extern cl::opt<bool> ANDIGlueBug;
51
Chris Lattner5e693ed2009-07-28 03:13:23 +000052static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) {
53 if (TM.getSubtargetImpl()->isDarwin())
Bill Wendlingbbcaa402010-03-15 21:09:38 +000054 return new TargetLoweringObjectFileMachO();
Bill Wendlingdd3fe942010-03-12 02:00:43 +000055
Bill Schmidt22d40dc2013-05-13 19:34:37 +000056 if (TM.getSubtargetImpl()->isSVR4ABI())
57 return new PPC64LinuxTargetObjectFile();
58
Bruno Cardoso Lopes62e6a8b2009-08-13 23:30:21 +000059 return new TargetLoweringObjectFileELF();
Chris Lattner5e693ed2009-07-28 03:13:23 +000060}
61
Chris Lattner584a11a2006-11-02 01:44:04 +000062PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Chris Lattner5e693ed2009-07-28 03:13:23 +000063 : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) {
Evan Cheng39e90022012-07-02 22:39:56 +000064 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
Scott Michelcf0da6c2009-02-17 22:15:04 +000065
Nate Begeman4dd38312005-10-21 00:02:42 +000066 setPow2DivIsCheap();
Dale Johannesenc31eb202008-07-31 18:13:12 +000067
Chris Lattnera028e7a2005-09-27 22:18:25 +000068 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000069 setUseUnderscoreSetJmp(true);
70 setUseUnderscoreLongJmp(true);
Scott Michelcf0da6c2009-02-17 22:15:04 +000071
Chris Lattnerd10babf2010-10-10 18:34:00 +000072 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
73 // arguments are at least 4/8 bytes aligned.
Evan Cheng39e90022012-07-02 22:39:56 +000074 bool isPPC64 = Subtarget->isPPC64();
75 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peck527da1b2010-11-23 03:31:01 +000076
Chris Lattnerf22556d2005-08-16 17:14:42 +000077 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +000078 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
79 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
80 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +000081
Evan Cheng5d9fd972006-10-04 00:56:09 +000082 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson9f944592009-08-11 20:47:22 +000083 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
84 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sands95d46ef2008-01-23 20:39:46 +000085
Owen Anderson9f944592009-08-11 20:47:22 +000086 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +000087
Chris Lattnerc9fa36d2006-11-10 23:58:45 +000088 // PowerPC has pre-inc load and store's.
Owen Anderson9f944592009-08-11 20:47:22 +000089 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
90 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
91 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
92 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
93 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
94 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
95 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
96 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
97 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
98 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Cheng36a8fbf2006-11-09 19:11:50 +000099
Hal Finkel940ab932014-02-28 00:27:01 +0000100 if (Subtarget->useCRBits()) {
101 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
102
Hal Finkel6a56b212014-03-05 22:14:00 +0000103 if (isPPC64 || Subtarget->hasFPCVT()) {
104 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
105 AddPromotedToType (ISD::SINT_TO_FP, MVT::i1,
106 isPPC64 ? MVT::i64 : MVT::i32);
107 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
108 AddPromotedToType (ISD::UINT_TO_FP, MVT::i1,
109 isPPC64 ? MVT::i64 : MVT::i32);
110 } else {
111 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Custom);
112 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Custom);
113 }
Hal Finkel940ab932014-02-28 00:27:01 +0000114
115 // PowerPC does not support direct load / store of condition registers
116 setOperationAction(ISD::LOAD, MVT::i1, Custom);
117 setOperationAction(ISD::STORE, MVT::i1, Custom);
118
119 // FIXME: Remove this once the ANDI glue bug is fixed:
120 if (ANDIGlueBug)
121 setOperationAction(ISD::TRUNCATE, MVT::i1, Custom);
122
123 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
124 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
125 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
126 setTruncStoreAction(MVT::i32, MVT::i1, Expand);
127 setTruncStoreAction(MVT::i16, MVT::i1, Expand);
128 setTruncStoreAction(MVT::i8, MVT::i1, Expand);
129
130 addRegisterClass(MVT::i1, &PPC::CRBITRCRegClass);
131 }
132
Dale Johannesen666323e2007-10-10 01:01:31 +0000133 // This is used in the ppcf128->int sequence. Note it has different semantics
134 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson9f944592009-08-11 20:47:22 +0000135 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesenf864ac92007-10-06 01:24:11 +0000136
Roman Divacky1faf5b02012-08-16 18:19:29 +0000137 // We do not currently implement these libm ops for PowerPC.
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000138 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
139 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
140 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
141 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
142 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidt92e26642013-04-03 13:05:44 +0000143 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson0b9b9da2011-12-08 19:32:14 +0000144
Chris Lattnerf22556d2005-08-16 17:14:42 +0000145 // PowerPC has no SREM/UREM instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000146 setOperationAction(ISD::SREM, MVT::i32, Expand);
147 setOperationAction(ISD::UREM, MVT::i32, Expand);
148 setOperationAction(ISD::SREM, MVT::i64, Expand);
149 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman71f0d7d2007-10-08 17:28:24 +0000150
151 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson9f944592009-08-11 20:47:22 +0000152 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
153 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
154 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
155 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
156 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
157 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
158 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
159 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000160
Dan Gohman482732a2007-10-11 23:21:31 +0000161 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000162 setOperationAction(ISD::FSIN , MVT::f64, Expand);
163 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000164 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000165 setOperationAction(ISD::FREM , MVT::f64, Expand);
166 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000167 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson9f944592009-08-11 20:47:22 +0000168 setOperationAction(ISD::FSIN , MVT::f32, Expand);
169 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000170 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000171 setOperationAction(ISD::FREM , MVT::f32, Expand);
172 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000173 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +0000174
Owen Anderson9f944592009-08-11 20:47:22 +0000175 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000176
Chris Lattnerf22556d2005-08-16 17:14:42 +0000177 // If we're enabling GP optimizations, use hardware square root
Hal Finkel2e103312013-04-03 04:01:11 +0000178 if (!Subtarget->hasFSQRT() &&
179 !(TM.Options.UnsafeFPMath &&
180 Subtarget->hasFRSQRTE() && Subtarget->hasFRE()))
Owen Anderson9f944592009-08-11 20:47:22 +0000181 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel2e103312013-04-03 04:01:11 +0000182
183 if (!Subtarget->hasFSQRT() &&
184 !(TM.Options.UnsafeFPMath &&
185 Subtarget->hasFRSQRTES() && Subtarget->hasFRES()))
Owen Anderson9f944592009-08-11 20:47:22 +0000186 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000187
Hal Finkeldbc78e12013-08-19 05:01:02 +0000188 if (Subtarget->hasFCPSGN()) {
189 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Legal);
190 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Legal);
191 } else {
192 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
193 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
194 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000195
Hal Finkelc20a08d2013-03-29 08:57:48 +0000196 if (Subtarget->hasFPRND()) {
197 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
198 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
199 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000200 setOperationAction(ISD::FROUND, MVT::f64, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000201
202 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
203 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
204 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
Hal Finkel2b7b2f32013-08-08 04:31:34 +0000205 setOperationAction(ISD::FROUND, MVT::f32, Legal);
Hal Finkelc20a08d2013-03-29 08:57:48 +0000206 }
207
Nate Begeman2fba8a32006-01-14 03:14:10 +0000208 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson9f944592009-08-11 20:47:22 +0000209 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000210 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000211 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
212 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000213 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000214 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000215 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
216 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000217
Hal Finkela4d07482013-03-28 13:29:47 +0000218 if (Subtarget->hasPOPCNTD()) {
Hal Finkel290376d2013-04-01 15:58:15 +0000219 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkela4d07482013-03-28 13:29:47 +0000220 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
221 } else {
222 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
223 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
224 }
225
Nate Begeman1b8121b2006-01-11 21:21:00 +0000226 // PowerPC does not have ROTR
Owen Anderson9f944592009-08-11 20:47:22 +0000227 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
228 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000229
Hal Finkel940ab932014-02-28 00:27:01 +0000230 if (!Subtarget->useCRBits()) {
231 // PowerPC does not have Select
232 setOperationAction(ISD::SELECT, MVT::i32, Expand);
233 setOperationAction(ISD::SELECT, MVT::i64, Expand);
234 setOperationAction(ISD::SELECT, MVT::f32, Expand);
235 setOperationAction(ISD::SELECT, MVT::f64, Expand);
236 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000237
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000238 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson9f944592009-08-11 20:47:22 +0000239 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
240 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begemana162f202006-01-31 08:17:29 +0000241
Nate Begeman7e7f4392006-02-01 07:19:44 +0000242 // PowerPC wants to optimize integer setcc a bit
Hal Finkel940ab932014-02-28 00:27:01 +0000243 if (!Subtarget->useCRBits())
244 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000245
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000246 // PowerPC does not have BRCOND which requires SetCC
Hal Finkel940ab932014-02-28 00:27:01 +0000247 if (!Subtarget->useCRBits())
248 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Cheng0d41d192006-10-30 08:02:39 +0000249
Owen Anderson9f944592009-08-11 20:47:22 +0000250 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000251
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000252 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson9f944592009-08-11 20:47:22 +0000253 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000254
Jim Laskey6267b2c2005-08-17 00:40:22 +0000255 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson9f944592009-08-11 20:47:22 +0000256 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
257 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskey6267b2c2005-08-17 00:40:22 +0000258
Wesley Peck527da1b2010-11-23 03:31:01 +0000259 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
260 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
261 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
262 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattnerc46fc242005-12-23 05:13:35 +0000263
Chris Lattner84b49d52006-04-28 21:56:10 +0000264 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000265 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskeye0008e22007-02-22 14:56:36 +0000266
Hal Finkel1996f3d2013-03-27 19:10:42 +0000267 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel756810f2013-03-21 21:37:52 +0000268 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
269 // support continuation, user-level threading, and etc.. As a result, no
270 // other SjLj exception interfaces are implemented and please don't build
271 // your own exception handling based on them.
272 // LLVM/Clang supports zero-cost DWARF exception handling.
273 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
274 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000275
276 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman4e56db62005-12-10 02:36:00 +0000277 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000278 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
279 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000280 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000281 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
282 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
283 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
284 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilsonf84f7102009-11-04 21:31:18 +0000285 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000286 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
287 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000288
Nate Begemanf69d13b2008-08-11 17:36:31 +0000289 // TRAP is legal.
Owen Anderson9f944592009-08-11 20:47:22 +0000290 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling95e1af22008-09-17 00:30:57 +0000291
292 // TRAMPOLINE is custom lowered.
Duncan Sandsa0984362011-09-06 13:37:06 +0000293 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
294 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling95e1af22008-09-17 00:30:57 +0000295
Nate Begemane74795c2006-01-25 18:21:52 +0000296 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000297 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000298
Evan Cheng39e90022012-07-02 22:39:56 +0000299 if (Subtarget->isSVR4ABI()) {
300 if (isPPC64) {
Hal Finkele44eb282012-03-24 03:53:55 +0000301 // VAARG always uses double-word chunks, so promote anything smaller.
302 setOperationAction(ISD::VAARG, MVT::i1, Promote);
303 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
304 setOperationAction(ISD::VAARG, MVT::i8, Promote);
305 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
306 setOperationAction(ISD::VAARG, MVT::i16, Promote);
307 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
308 setOperationAction(ISD::VAARG, MVT::i32, Promote);
309 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
310 setOperationAction(ISD::VAARG, MVT::Other, Expand);
311 } else {
312 // VAARG is custom lowered with the 32-bit SVR4 ABI.
313 setOperationAction(ISD::VAARG, MVT::Other, Custom);
314 setOperationAction(ISD::VAARG, MVT::i64, Custom);
315 }
Roman Divacky4394e682011-06-28 15:30:42 +0000316 } else
Owen Anderson9f944592009-08-11 20:47:22 +0000317 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000318
Roman Divackyc3825df2013-07-25 21:36:47 +0000319 if (Subtarget->isSVR4ABI() && !isPPC64)
320 // VACOPY is custom lowered with the 32-bit SVR4 ABI.
321 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
322 else
323 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
324
Chris Lattner5bd514d2006-01-15 09:02:48 +0000325 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000326 setOperationAction(ISD::VAEND , MVT::Other, Expand);
327 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
328 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
329 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
330 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattnerab4df8342006-10-18 01:18:48 +0000331
Chris Lattner6961fc72006-03-26 10:06:40 +0000332 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000333 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000334
Hal Finkel25c19922013-05-15 21:37:41 +0000335 // To handle counter-based loop conditions.
336 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
337
Dale Johannesen160be0f2008-11-07 22:54:33 +0000338 // Comparisons that require checking two conditions.
Owen Anderson9f944592009-08-11 20:47:22 +0000339 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
340 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
341 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
342 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
343 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
344 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
345 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
346 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
347 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
348 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
349 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
350 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000351
Evan Cheng39e90022012-07-02 22:39:56 +0000352 if (Subtarget->has64BitSupport()) {
Nate Begeman0b71e002005-10-18 00:28:58 +0000353 // They also have instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000354 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
355 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
356 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
357 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen37bc85f2009-06-04 20:53:52 +0000358 // This is just the low 32 bits of a (signed) fp->i64 conversion.
359 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson9f944592009-08-11 20:47:22 +0000360 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000361
Hal Finkelf6d45f22013-04-01 17:52:07 +0000362 if (PPCSubTarget.hasLFIWAX() || Subtarget->isPPC64())
Hal Finkele53429a2013-03-31 01:58:02 +0000363 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begeman762bf802005-10-25 23:48:36 +0000364 } else {
Chris Lattner595088a2005-11-17 07:30:41 +0000365 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson9f944592009-08-11 20:47:22 +0000366 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begemane74dfbb2005-10-18 00:56:42 +0000367 }
368
Hal Finkelf6d45f22013-04-01 17:52:07 +0000369 // With the instructions enabled under FPCVT, we can do everything.
370 if (PPCSubTarget.hasFPCVT()) {
371 if (Subtarget->has64BitSupport()) {
372 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
373 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
374 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
375 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
376 }
377
378 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
379 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
380 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
381 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
382 }
383
Evan Cheng39e90022012-07-02 22:39:56 +0000384 if (Subtarget->use64BitRegs()) {
Chris Lattnerb1935762007-10-19 04:08:28 +0000385 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperabadc662012-04-20 06:31:50 +0000386 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman0b71e002005-10-18 00:28:58 +0000387 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000388 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman8d2ead22008-03-07 20:36:53 +0000389 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000390 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
391 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
392 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman0b71e002005-10-18 00:28:58 +0000393 } else {
Chris Lattnerb1935762007-10-19 04:08:28 +0000394 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson9f944592009-08-11 20:47:22 +0000395 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
396 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
397 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begeman60952142005-09-06 22:03:27 +0000398 }
Evan Cheng19264272006-03-01 01:11:20 +0000399
Evan Cheng39e90022012-07-02 22:39:56 +0000400 if (Subtarget->hasAltivec()) {
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000401 // First set operation action for all vector types to expand. Then we
402 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson9f944592009-08-11 20:47:22 +0000403 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
404 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
405 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands13237ac2008-06-06 12:08:01 +0000406
Chris Lattner06a21ba2006-04-16 01:37:57 +0000407 // add/sub are legal for all supported vector VT's.
Duncan Sands13237ac2008-06-06 12:08:01 +0000408 setOperationAction(ISD::ADD , VT, Legal);
409 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000410
Chris Lattner95c7adc2006-04-04 17:25:31 +0000411 // We promote all shuffles to v16i8.
Duncan Sands13237ac2008-06-06 12:08:01 +0000412 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000413 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattner06a21ba2006-04-16 01:37:57 +0000414
415 // We promote all non-typed operations to v4i32.
Duncan Sands13237ac2008-06-06 12:08:01 +0000416 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000417 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000418 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000419 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000420 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000421 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000422 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000423 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000424 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000425 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands13237ac2008-06-06 12:08:01 +0000426 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson9f944592009-08-11 20:47:22 +0000427 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000428
Chris Lattner06a21ba2006-04-16 01:37:57 +0000429 // No other operations are legal.
Duncan Sands13237ac2008-06-06 12:08:01 +0000430 setOperationAction(ISD::MUL , VT, Expand);
431 setOperationAction(ISD::SDIV, VT, Expand);
432 setOperationAction(ISD::SREM, VT, Expand);
433 setOperationAction(ISD::UDIV, VT, Expand);
434 setOperationAction(ISD::UREM, VT, Expand);
435 setOperationAction(ISD::FDIV, VT, Expand);
Hal Finkele3930222013-07-08 17:30:25 +0000436 setOperationAction(ISD::FREM, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000437 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topperc8a2adf2012-11-15 08:02:19 +0000438 setOperationAction(ISD::FSQRT, VT, Expand);
439 setOperationAction(ISD::FLOG, VT, Expand);
440 setOperationAction(ISD::FLOG10, VT, Expand);
441 setOperationAction(ISD::FLOG2, VT, Expand);
442 setOperationAction(ISD::FEXP, VT, Expand);
443 setOperationAction(ISD::FEXP2, VT, Expand);
444 setOperationAction(ISD::FSIN, VT, Expand);
445 setOperationAction(ISD::FCOS, VT, Expand);
446 setOperationAction(ISD::FABS, VT, Expand);
447 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topperc4343f22012-11-14 08:11:25 +0000448 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000449 setOperationAction(ISD::FCEIL, VT, Expand);
450 setOperationAction(ISD::FTRUNC, VT, Expand);
451 setOperationAction(ISD::FRINT, VT, Expand);
452 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000453 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
454 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
455 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
456 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
457 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
458 setOperationAction(ISD::UDIVREM, VT, Expand);
459 setOperationAction(ISD::SDIVREM, VT, Expand);
460 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
461 setOperationAction(ISD::FPOW, VT, Expand);
462 setOperationAction(ISD::CTPOP, VT, Expand);
463 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000464 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000465 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000466 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000467 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000468 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
469
470 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
471 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
472 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
473 setTruncStoreAction(VT, InnerVT, Expand);
474 }
475 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
476 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
477 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000478 }
479
Chris Lattner95c7adc2006-04-04 17:25:31 +0000480 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
481 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000482 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000483
Owen Anderson9f944592009-08-11 20:47:22 +0000484 setOperationAction(ISD::AND , MVT::v4i32, Legal);
485 setOperationAction(ISD::OR , MVT::v4i32, Legal);
486 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
487 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Hal Finkel940ab932014-02-28 00:27:01 +0000488 setOperationAction(ISD::SELECT, MVT::v4i32,
489 Subtarget->useCRBits() ? Legal : Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000490 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000491 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
492 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
493 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
494 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000495 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
496 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
497 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
498 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000499
Craig Topperabadc662012-04-20 06:31:50 +0000500 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
501 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
502 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
503 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000504
Owen Anderson9f944592009-08-11 20:47:22 +0000505 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000506 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000507
Hal Finkel27774d92014-03-13 07:58:58 +0000508 if (TM.Options.UnsafeFPMath || Subtarget->hasVSX()) {
Hal Finkel2e103312013-04-03 04:01:11 +0000509 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
510 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
511 }
512
Owen Anderson9f944592009-08-11 20:47:22 +0000513 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
514 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
515 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000516
Owen Anderson9f944592009-08-11 20:47:22 +0000517 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
518 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000519
Owen Anderson9f944592009-08-11 20:47:22 +0000520 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
521 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
522 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
523 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000524
525 // Altivec does not contain unordered floating-point compare instructions
526 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
527 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
528 setCondCodeAction(ISD::SETUGT, MVT::v4f32, Expand);
529 setCondCodeAction(ISD::SETUGE, MVT::v4f32, Expand);
530 setCondCodeAction(ISD::SETULT, MVT::v4f32, Expand);
531 setCondCodeAction(ISD::SETULE, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000532
533 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
534 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000535
536 if (Subtarget->hasVSX()) {
537 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
Hal Finkel82569b62014-03-27 22:22:48 +0000538 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
Hal Finkel27774d92014-03-13 07:58:58 +0000539
540 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
541 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
542 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
543 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
544 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
545
546 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
547
548 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
549 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
550
551 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
552 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
553
Hal Finkel732f0f72014-03-26 12:49:28 +0000554 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
555 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal);
556 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
557 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
558 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
559
Hal Finkel27774d92014-03-13 07:58:58 +0000560 // Share the Altivec comparison restrictions.
561 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand);
562 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand);
563 setCondCodeAction(ISD::SETUGT, MVT::v2f64, Expand);
564 setCondCodeAction(ISD::SETUGE, MVT::v2f64, Expand);
565 setCondCodeAction(ISD::SETULT, MVT::v2f64, Expand);
566 setCondCodeAction(ISD::SETULE, MVT::v2f64, Expand);
567
568 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand);
569 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand);
570
Hal Finkel9281c9a2014-03-26 18:26:30 +0000571 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
572 setOperationAction(ISD::STORE, MVT::v2f64, Legal);
573
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000574 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal);
575
Hal Finkel19be5062014-03-29 05:29:01 +0000576 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000577
578 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass);
579 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass);
Hal Finkela6c8b512014-03-26 16:12:58 +0000580
581 // VSX v2i64 only supports non-arithmetic operations.
582 setOperationAction(ISD::ADD, MVT::v2i64, Expand);
583 setOperationAction(ISD::SUB, MVT::v2i64, Expand);
584
Hal Finkelad801b72014-03-27 21:26:33 +0000585 setOperationAction(ISD::SHL, MVT::v2i64, Expand);
586 setOperationAction(ISD::SRA, MVT::v2i64, Expand);
587 setOperationAction(ISD::SRL, MVT::v2i64, Expand);
588
Hal Finkel777c9dd2014-03-29 16:04:40 +0000589 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
590
Hal Finkel9281c9a2014-03-26 18:26:30 +0000591 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
592 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64);
593 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
594 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64);
595
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000596 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal);
597
Hal Finkel7279f4b2014-03-26 19:13:54 +0000598 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
599 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
600 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
601 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
602
Hal Finkel5c0d1452014-03-30 13:22:59 +0000603 // Vector operation legalization checks the result type of
604 // SIGN_EXTEND_INREG, overall legalization checks the inner type.
605 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal);
606 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal);
607 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
608 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
609
Hal Finkela6c8b512014-03-26 16:12:58 +0000610 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000611 }
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000612 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000613
Hal Finkel70381a72012-08-04 14:10:46 +0000614 if (Subtarget->has64BitSupport()) {
Hal Finkel322e41a2012-04-01 20:08:17 +0000615 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel70381a72012-08-04 14:10:46 +0000616 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
617 }
Hal Finkel322e41a2012-04-01 20:08:17 +0000618
Eli Friedman7dfa7912011-08-29 18:23:02 +0000619 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
620 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkel1b5ff082012-12-25 17:22:53 +0000621 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
622 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000623
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000624 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidta76bf5a2013-04-23 18:49:44 +0000625 // Altivec instructions set fields to all zeros or all ones.
626 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000627
Evan Cheng39e90022012-07-02 22:39:56 +0000628 if (isPPC64) {
Chris Lattner454436d2006-10-18 01:20:43 +0000629 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000630 setExceptionPointerRegister(PPC::X3);
631 setExceptionSelectorRegister(PPC::X4);
632 } else {
Chris Lattner454436d2006-10-18 01:20:43 +0000633 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskeye0008e22007-02-22 14:56:36 +0000634 setExceptionPointerRegister(PPC::R3);
635 setExceptionSelectorRegister(PPC::R4);
636 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000637
Chris Lattnerf4184352006-03-01 04:57:39 +0000638 // We have target-specific dag combine patterns for the following nodes:
639 setTargetDAGCombine(ISD::SINT_TO_FP);
Hal Finkelcf2e9082013-05-24 23:00:14 +0000640 setTargetDAGCombine(ISD::LOAD);
Chris Lattner27f53452006-03-01 05:50:56 +0000641 setTargetDAGCombine(ISD::STORE);
Chris Lattner9754d142006-04-18 17:59:36 +0000642 setTargetDAGCombine(ISD::BR_CC);
Hal Finkel940ab932014-02-28 00:27:01 +0000643 if (Subtarget->useCRBits())
644 setTargetDAGCombine(ISD::BRCOND);
Chris Lattnera7976d32006-07-10 20:56:58 +0000645 setTargetDAGCombine(ISD::BSWAP);
Hal Finkelbc2ee4c2013-05-25 04:05:05 +0000646 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000647
Hal Finkel46043ed2014-03-01 21:36:57 +0000648 setTargetDAGCombine(ISD::SIGN_EXTEND);
649 setTargetDAGCombine(ISD::ZERO_EXTEND);
650 setTargetDAGCombine(ISD::ANY_EXTEND);
651
Hal Finkel940ab932014-02-28 00:27:01 +0000652 if (Subtarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000653 setTargetDAGCombine(ISD::TRUNCATE);
654 setTargetDAGCombine(ISD::SETCC);
655 setTargetDAGCombine(ISD::SELECT_CC);
656 }
657
Hal Finkel2e103312013-04-03 04:01:11 +0000658 // Use reciprocal estimates.
659 if (TM.Options.UnsafeFPMath) {
660 setTargetDAGCombine(ISD::FDIV);
661 setTargetDAGCombine(ISD::FSQRT);
662 }
663
Dale Johannesen10432e52007-10-19 00:59:18 +0000664 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng39e90022012-07-02 22:39:56 +0000665 if (Subtarget->isDarwin()) {
Duncan Sands53c954f2008-01-10 10:28:30 +0000666 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000667 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
668 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands53c954f2008-01-10 10:28:30 +0000669 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
670 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesenda2d8062008-09-04 00:47:13 +0000671 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
672 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
673 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
674 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
675 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesen10432e52007-10-19 00:59:18 +0000676 }
677
Hal Finkel940ab932014-02-28 00:27:01 +0000678 // With 32 condition bits, we don't need to sink (and duplicate) compares
679 // aggressively in CodeGenPrep.
680 if (Subtarget->useCRBits())
681 setHasMultipleConditionRegisters();
682
Hal Finkel65298572011-10-17 18:53:03 +0000683 setMinFunctionAlignment(2);
684 if (PPCSubTarget.isDarwin())
685 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000686
Evan Cheng39e90022012-07-02 22:39:56 +0000687 if (isPPC64 && Subtarget->isJITCodeModel())
688 // Temporary workaround for the inability of PPC64 JIT to handle jump
689 // tables.
690 setSupportJumpTables(false);
691
Eli Friedman30a49e92011-08-03 21:06:02 +0000692 setInsertFencesForAtomic(true);
693
Hal Finkel21442b22013-09-11 23:05:25 +0000694 if (Subtarget->enableMachineScheduler())
695 setSchedulingPreference(Sched::Source);
696 else
697 setSchedulingPreference(Sched::Hybrid);
Hal Finkel6f0ae782011-11-22 16:21:04 +0000698
Chris Lattnerf22556d2005-08-16 17:14:42 +0000699 computeRegisterProperties();
Hal Finkel742b5352012-08-28 16:12:39 +0000700
701 // The Freescale cores does better with aggressive inlining of memcpy and
702 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
703 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
704 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000705 MaxStoresPerMemset = 32;
706 MaxStoresPerMemsetOptSize = 16;
707 MaxStoresPerMemcpy = 32;
708 MaxStoresPerMemcpyOptSize = 8;
709 MaxStoresPerMemmove = 32;
710 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel742b5352012-08-28 16:12:39 +0000711
712 setPrefFunctionAlignment(4);
Hal Finkel742b5352012-08-28 16:12:39 +0000713 }
Chris Lattnerf22556d2005-08-16 17:14:42 +0000714}
715
Hal Finkel262a2242013-09-12 23:20:06 +0000716/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
717/// the desired ByVal argument alignment.
718static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign,
719 unsigned MaxMaxAlign) {
720 if (MaxAlign == MaxMaxAlign)
721 return;
722 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
723 if (MaxMaxAlign >= 32 && VTy->getBitWidth() >= 256)
724 MaxAlign = 32;
725 else if (VTy->getBitWidth() >= 128 && MaxAlign < 16)
726 MaxAlign = 16;
727 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
728 unsigned EltAlign = 0;
729 getMaxByValAlign(ATy->getElementType(), EltAlign, MaxMaxAlign);
730 if (EltAlign > MaxAlign)
731 MaxAlign = EltAlign;
732 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
733 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
734 unsigned EltAlign = 0;
735 getMaxByValAlign(STy->getElementType(i), EltAlign, MaxMaxAlign);
736 if (EltAlign > MaxAlign)
737 MaxAlign = EltAlign;
738 if (MaxAlign == MaxMaxAlign)
739 break;
740 }
741 }
742}
743
Dale Johannesencbde4c22008-02-28 22:31:51 +0000744/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
745/// function arguments in the caller parameter area.
Chris Lattner229907c2011-07-18 04:54:35 +0000746unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dale Johannesencbde4c22008-02-28 22:31:51 +0000747 // Darwin passes everything on 4 byte boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000748 if (PPCSubTarget.isDarwin())
Dale Johannesencbde4c22008-02-28 22:31:51 +0000749 return 4;
Roman Divackyb9663cc2012-04-02 15:49:30 +0000750
751 // 16byte and wider vectors are passed on 16byte boundary.
Roman Divackyb9663cc2012-04-02 15:49:30 +0000752 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
Hal Finkel262a2242013-09-12 23:20:06 +0000753 unsigned Align = PPCSubTarget.isPPC64() ? 8 : 4;
754 if (PPCSubTarget.hasAltivec() || PPCSubTarget.hasQPX())
755 getMaxByValAlign(Ty, Align, PPCSubTarget.hasQPX() ? 32 : 16);
756 return Align;
Dale Johannesencbde4c22008-02-28 22:31:51 +0000757}
758
Chris Lattner347ed8a2006-01-09 23:52:17 +0000759const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
760 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000761 default: return nullptr;
Evan Cheng32e376f2008-07-12 02:23:19 +0000762 case PPCISD::FSEL: return "PPCISD::FSEL";
763 case PPCISD::FCFID: return "PPCISD::FCFID";
764 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
765 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel2e103312013-04-03 04:01:11 +0000766 case PPCISD::FRE: return "PPCISD::FRE";
767 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng32e376f2008-07-12 02:23:19 +0000768 case PPCISD::STFIWX: return "PPCISD::STFIWX";
769 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
770 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
771 case PPCISD::VPERM: return "PPCISD::VPERM";
772 case PPCISD::Hi: return "PPCISD::Hi";
773 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000774 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller79fef932009-12-18 13:00:15 +0000775 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
776 case PPCISD::LOAD: return "PPCISD::LOAD";
777 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000778 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
779 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
780 case PPCISD::SRL: return "PPCISD::SRL";
781 case PPCISD::SRA: return "PPCISD::SRA";
782 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000783 case PPCISD::CALL: return "PPCISD::CALL";
784 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000785 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000786 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng32e376f2008-07-12 02:23:19 +0000787 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel756810f2013-03-21 21:37:52 +0000788 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
789 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000790 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng32e376f2008-07-12 02:23:19 +0000791 case PPCISD::VCMP: return "PPCISD::VCMP";
792 case PPCISD::VCMPo: return "PPCISD::VCMPo";
793 case PPCISD::LBRX: return "PPCISD::LBRX";
794 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng32e376f2008-07-12 02:23:19 +0000795 case PPCISD::LARX: return "PPCISD::LARX";
796 case PPCISD::STCX: return "PPCISD::STCX";
797 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +0000798 case PPCISD::BDNZ: return "PPCISD::BDNZ";
799 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000800 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000801 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000802 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +0000803 case PPCISD::CR6SET: return "PPCISD::CR6SET";
804 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34627e32012-11-27 17:35:46 +0000805 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
806 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
807 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Roman Divacky32143e22013-12-20 18:08:54 +0000808 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000809 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
810 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000811 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000812 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
813 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
814 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000815 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
816 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
817 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
818 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
819 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +0000820 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +0000821 case PPCISD::SC: return "PPCISD::SC";
Chris Lattner347ed8a2006-01-09 23:52:17 +0000822 }
823}
824
Matt Arsenault758659232013-05-18 00:21:46 +0000825EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000826 if (!VT.isVector())
Hal Finkel940ab932014-02-28 00:27:01 +0000827 return PPCSubTarget.useCRBits() ? MVT::i1 : MVT::i32;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000828 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000829}
830
Chris Lattner4211ca92006-04-14 06:01:58 +0000831//===----------------------------------------------------------------------===//
832// Node matching predicates, for use by the tblgen matching code.
833//===----------------------------------------------------------------------===//
834
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000835/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000836static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000837 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000838 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000839 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000840 // Maybe this has already been legalized into the constant pool?
841 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000842 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000843 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000844 }
845 return false;
846}
847
Chris Lattnere8b83b42006-04-06 17:23:16 +0000848/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
849/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000850static bool isConstantOrUndef(int Op, int Val) {
851 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000852}
853
854/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
855/// VPKUHUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000856bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000857 if (!isUnary) {
858 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000859 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000860 return false;
861 } else {
862 for (unsigned i = 0; i != 8; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000863 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
864 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000865 return false;
866 }
Chris Lattner1d338192006-04-06 18:26:28 +0000867 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000868}
869
870/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
871/// VPKUWUM instruction.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000872bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000873 if (!isUnary) {
874 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000875 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
876 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000877 return false;
878 } else {
879 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000880 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
881 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
882 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
883 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000884 return false;
885 }
Chris Lattner1d338192006-04-06 18:26:28 +0000886 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000887}
888
Chris Lattnerf38e0332006-04-06 22:02:42 +0000889/// isVMerge - Common function, used to match vmrg* shuffles.
890///
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000891static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +0000892 unsigned LHSStart, unsigned RHSStart) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000893 if (N->getValueType(0) != MVT::v16i8)
894 return false;
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000895 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
896 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000897
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000898 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
899 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000900 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000901 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000902 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000903 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000904 return false;
905 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000906 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +0000907}
908
909/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
910/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000911bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000912 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000913 if (!isUnary)
914 return isVMerge(N, UnitSize, 8, 24);
915 return isVMerge(N, UnitSize, 8, 8);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000916}
917
918/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
919/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peck527da1b2010-11-23 03:31:01 +0000920bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000921 bool isUnary) {
Chris Lattnerf38e0332006-04-06 22:02:42 +0000922 if (!isUnary)
923 return isVMerge(N, UnitSize, 0, 16);
924 return isVMerge(N, UnitSize, 0, 0);
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000925}
926
927
Chris Lattner1d338192006-04-06 18:26:28 +0000928/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
929/// amount, otherwise return -1.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000930int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000931 if (N->getValueType(0) != MVT::v16i8)
Hal Finkela775e512014-04-08 19:00:27 +0000932 return -1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000933
934 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +0000935
Chris Lattner1d338192006-04-06 18:26:28 +0000936 // Find the first non-undef value in the shuffle mask.
937 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000938 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +0000939 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000940
Chris Lattner1d338192006-04-06 18:26:28 +0000941 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000942
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000943 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +0000944 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000945 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +0000946 if (ShiftAmt < i) return -1;
947 ShiftAmt -= i;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000948
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000949 if (!isUnary) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000950 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000951 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000952 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000953 return -1;
954 } else {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000955 // Check the rest of the elements to see if they are consecutive.
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000956 for (++i; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000957 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000958 return -1;
959 }
Chris Lattner1d338192006-04-06 18:26:28 +0000960 return ShiftAmt;
961}
Chris Lattnerffc47562006-03-20 06:33:01 +0000962
963/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
964/// specifies a splat of a single element that is suitable for input to
965/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000966bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +0000967 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +0000968 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000969
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000970 // This is a splat operation if each element of the permute is the same, and
971 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000972 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +0000973
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000974 // FIXME: Handle UNDEF elements too!
975 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +0000976 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000977
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000978 // Check that the indices are consecutive, in the case of a multi-byte element
979 // splatted with a v16i8 mask.
980 for (unsigned i = 1; i != EltSize; ++i)
981 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000982 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000983
Chris Lattner95c7adc2006-04-04 17:25:31 +0000984 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000985 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +0000986 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000987 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +0000988 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +0000989 }
Chris Lattner95c7adc2006-04-04 17:25:31 +0000990 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +0000991}
992
Evan Cheng581d2792007-07-30 07:51:22 +0000993/// isAllNegativeZeroVector - Returns true if all elements of build_vector
994/// are -0.0.
995bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000996 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
997
998 APInt APVal, APUndef;
999 unsigned BitSize;
1000 bool HasAnyUndefs;
Wesley Peck527da1b2010-11-23 03:31:01 +00001001
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00001002 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001003 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001004 return CFP->getValueAPF().isNegZero();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001005
Evan Cheng581d2792007-07-30 07:51:22 +00001006 return false;
1007}
1008
Chris Lattnerffc47562006-03-20 06:33:01 +00001009/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
1010/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner95c7adc2006-04-04 17:25:31 +00001011unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001012 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1013 assert(isSplatShuffleMask(SVOp, EltSize));
1014 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +00001015}
1016
Chris Lattner74cf9ff2006-04-12 17:37:20 +00001017/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001018/// by using a vspltis[bhw] instruction of the specified element size, return
1019/// the constant being splatted. The ByteSize field indicates the number of
1020/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001021SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001022 SDValue OpVal(nullptr, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001023
1024 // If ByteSize of the splat is bigger than the element size of the
1025 // build_vector, then we have a case where we are checking for a splat where
1026 // multiple elements of the buildvector are folded together into a single
1027 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
1028 unsigned EltSize = 16/N->getNumOperands();
1029 if (EltSize < ByteSize) {
1030 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001031 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001032 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001033
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001034 // See if all of the elements in the buildvector agree across.
1035 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1036 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1037 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001038 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001039
Scott Michelcf0da6c2009-02-17 22:15:04 +00001040
Craig Topper062a2ba2014-04-25 05:30:21 +00001041 if (!UniquedVals[i&(Multiple-1)].getNode())
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001042 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
1043 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001044 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001045 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001046
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001047 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
1048 // either constant or undef values that are identical for each chunk. See
1049 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001050
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001051 // Check to see if all of the leading entries are either 0 or -1. If
1052 // neither, then this won't fit into the immediate field.
1053 bool LeadingZero = true;
1054 bool LeadingOnes = true;
1055 for (unsigned i = 0; i != Multiple-1; ++i) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001056 if (!UniquedVals[i].getNode()) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001057
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001058 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
1059 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
1060 }
1061 // Finally, check the least significant entry.
1062 if (LeadingZero) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001063 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001064 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00001065 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001066 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +00001067 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001068 }
1069 if (LeadingOnes) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001070 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001071 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +00001072 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001073 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +00001074 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001075 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001076
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001077 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001078 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001079
Chris Lattner2771e2c2006-03-25 06:12:06 +00001080 // Check to see if this buildvec has a single non-undef value in its elements.
1081 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1082 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Craig Topper062a2ba2014-04-25 05:30:21 +00001083 if (!OpVal.getNode())
Chris Lattner2771e2c2006-03-25 06:12:06 +00001084 OpVal = N->getOperand(i);
1085 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001086 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001087 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001088
Craig Topper062a2ba2014-04-25 05:30:21 +00001089 if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001090
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001091 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001092 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001093 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001094 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001095 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001096 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001097 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001098 }
1099
1100 // If the splat value is larger than the element value, then we can never do
1101 // this splat. The only case that we could fit the replicated bits into our
1102 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001103 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001104
Chris Lattner2771e2c2006-03-25 06:12:06 +00001105 // If the element value is larger than the splat value, cut it in half and
1106 // check to see if the two halves are equal. Continue doing this until we
1107 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
1108 while (ValSizeInBytes > ByteSize) {
1109 ValSizeInBytes >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001110
Chris Lattner2771e2c2006-03-25 06:12:06 +00001111 // If the top half equals the bottom half, we're still ok.
Chris Lattner39cc7172006-04-05 17:39:25 +00001112 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
1113 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001114 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001115 }
1116
1117 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001118 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001119
Evan Chengb1ddc982006-03-26 09:52:32 +00001120 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001121 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001122
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001123 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001124 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +00001125 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001126 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001127}
1128
Chris Lattner4211ca92006-04-14 06:01:58 +00001129//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001130// Addressing Mode Selection
1131//===----------------------------------------------------------------------===//
1132
1133/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1134/// or 64-bit immediate, and if the value can be accurately represented as a
1135/// sign extension from a 16-bit value. If so, this returns true and the
1136/// immediate.
1137static bool isIntS16Immediate(SDNode *N, short &Imm) {
1138 if (N->getOpcode() != ISD::Constant)
1139 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001140
Dan Gohmaneffb8942008-09-12 16:56:44 +00001141 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001142 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001143 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001144 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001145 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001146}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001147static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001148 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001149}
1150
1151
1152/// SelectAddressRegReg - Given the specified addressed, check to see if it
1153/// can be represented as an indexed [r+r] operation. Returns false if it
1154/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001155bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1156 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001157 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001158 short imm = 0;
1159 if (N.getOpcode() == ISD::ADD) {
1160 if (isIntS16Immediate(N.getOperand(1), imm))
1161 return false; // r+i
1162 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1163 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001164
Chris Lattnera801fced2006-11-08 02:15:41 +00001165 Base = N.getOperand(0);
1166 Index = N.getOperand(1);
1167 return true;
1168 } else if (N.getOpcode() == ISD::OR) {
1169 if (isIntS16Immediate(N.getOperand(1), imm))
1170 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001171
Chris Lattnera801fced2006-11-08 02:15:41 +00001172 // If this is an or of disjoint bitfields, we can codegen this as an add
1173 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1174 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001175 APInt LHSKnownZero, LHSKnownOne;
1176 APInt RHSKnownZero, RHSKnownOne;
1177 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001178 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001179
Dan Gohmanf19609a2008-02-27 01:23:58 +00001180 if (LHSKnownZero.getBoolValue()) {
1181 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanf19609a2008-02-27 01:23:58 +00001182 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001183 // If all of the bits are known zero on the LHS or RHS, the add won't
1184 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001185 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001186 Base = N.getOperand(0);
1187 Index = N.getOperand(1);
1188 return true;
1189 }
1190 }
1191 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001192
Chris Lattnera801fced2006-11-08 02:15:41 +00001193 return false;
1194}
1195
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001196// If we happen to be doing an i64 load or store into a stack slot that has
1197// less than a 4-byte alignment, then the frame-index elimination may need to
1198// use an indexed load or store instruction (because the offset may not be a
1199// multiple of 4). The extra register needed to hold the offset comes from the
1200// register scavenger, and it is possible that the scavenger will need to use
1201// an emergency spill slot. As a result, we need to make sure that a spill slot
1202// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1203// stack slot.
1204static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1205 // FIXME: This does not handle the LWA case.
1206 if (VT != MVT::i64)
1207 return;
1208
Hal Finkel7ab3db52013-07-10 15:29:01 +00001209 // NOTE: We'll exclude negative FIs here, which come from argument
1210 // lowering, because there are no known test cases triggering this problem
1211 // using packed structures (or similar). We can remove this exclusion if
1212 // we find such a test case. The reason why this is so test-case driven is
1213 // because this entire 'fixup' is only to prevent crashes (from the
1214 // register scavenger) on not-really-valid inputs. For example, if we have:
1215 // %a = alloca i1
1216 // %b = bitcast i1* %a to i64*
1217 // store i64* a, i64 b
1218 // then the store should really be marked as 'align 1', but is not. If it
1219 // were marked as 'align 1' then the indexed form would have been
1220 // instruction-selected initially, and the problem this 'fixup' is preventing
1221 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001222 if (FrameIdx < 0)
1223 return;
1224
1225 MachineFunction &MF = DAG.getMachineFunction();
1226 MachineFrameInfo *MFI = MF.getFrameInfo();
1227
1228 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1229 if (Align >= 4)
1230 return;
1231
1232 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1233 FuncInfo->setHasNonRISpills();
1234}
1235
Chris Lattnera801fced2006-11-08 02:15:41 +00001236/// Returns true if the address N can be represented by a base register plus
1237/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001238/// represented as reg+reg. If Aligned is true, only accept displacements
1239/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001240bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001241 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001242 SelectionDAG &DAG,
1243 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001244 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001245 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001246 // If this can be more profitably realized as r+r, fail.
1247 if (SelectAddressRegReg(N, Disp, Base, DAG))
1248 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001249
Chris Lattnera801fced2006-11-08 02:15:41 +00001250 if (N.getOpcode() == ISD::ADD) {
1251 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001252 if (isIntS16Immediate(N.getOperand(1), imm) &&
1253 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001254 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001255 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1256 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001257 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001258 } else {
1259 Base = N.getOperand(0);
1260 }
1261 return true; // [r+i]
1262 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1263 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001264 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001265 && "Cannot handle constant offsets yet!");
1266 Disp = N.getOperand(1).getOperand(0); // The global address.
1267 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001268 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001269 Disp.getOpcode() == ISD::TargetConstantPool ||
1270 Disp.getOpcode() == ISD::TargetJumpTable);
1271 Base = N.getOperand(0);
1272 return true; // [&g+r]
1273 }
1274 } else if (N.getOpcode() == ISD::OR) {
1275 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001276 if (isIntS16Immediate(N.getOperand(1), imm) &&
1277 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001278 // If this is an or of disjoint bitfields, we can codegen this as an add
1279 // (for better address arithmetic) if the LHS and RHS of the OR are
1280 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001281 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001282 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001283
Dan Gohmanf19609a2008-02-27 01:23:58 +00001284 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001285 // If all of the bits are known zero on the LHS or RHS, the add won't
1286 // carry.
1287 Base = N.getOperand(0);
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001288 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001289 return true;
1290 }
1291 }
1292 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1293 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001294
Chris Lattnera801fced2006-11-08 02:15:41 +00001295 // If this address fits entirely in a 16-bit sext immediate field, codegen
1296 // this as "d, 0"
1297 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001298 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001299 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkelf70c41e2013-03-21 23:45:03 +00001300 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1301 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001302 return true;
1303 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001304
1305 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001306 if ((CN->getValueType(0) == MVT::i32 ||
1307 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1308 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001309 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001310
Chris Lattnera801fced2006-11-08 02:15:41 +00001311 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001312 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001313
Owen Anderson9f944592009-08-11 20:47:22 +00001314 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1315 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001316 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001317 return true;
1318 }
1319 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001320
Chris Lattnera801fced2006-11-08 02:15:41 +00001321 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001322 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001323 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001324 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1325 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001326 Base = N;
1327 return true; // [r+0]
1328}
1329
1330/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1331/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001332bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1333 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001334 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001335 // Check to see if we can easily represent this as an [r+r] address. This
1336 // will fail if it thinks that the address is more profitably represented as
1337 // reg+imm, e.g. where imm = 0.
1338 if (SelectAddressRegReg(N, Base, Index, DAG))
1339 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001340
Chris Lattnera801fced2006-11-08 02:15:41 +00001341 // If the operand is an addition, always emit this as [r+r], since this is
1342 // better (for code size, and execution, as the memop does the add for free)
1343 // than emitting an explicit add.
1344 if (N.getOpcode() == ISD::ADD) {
1345 Base = N.getOperand(0);
1346 Index = N.getOperand(1);
1347 return true;
1348 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001349
Chris Lattnera801fced2006-11-08 02:15:41 +00001350 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkelf70c41e2013-03-21 23:45:03 +00001351 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1352 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001353 Index = N;
1354 return true;
1355}
1356
Chris Lattnera801fced2006-11-08 02:15:41 +00001357/// getPreIndexedAddressParts - returns true by value, base pointer and
1358/// offset pointer and addressing mode by reference if the node's address
1359/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001360bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1361 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001362 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001363 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001364 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001365
Ulrich Weigande90b0222013-03-22 14:58:48 +00001366 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001367 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001368 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001369 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001370 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1371 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001372 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001373 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001374 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001375 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001376 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001377 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001378 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001379 } else
1380 return false;
1381
Chris Lattner68371252006-11-14 01:38:31 +00001382 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands13237ac2008-06-06 12:08:01 +00001383 if (VT.isVector())
Chris Lattner68371252006-11-14 01:38:31 +00001384 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001385
Ulrich Weigande90b0222013-03-22 14:58:48 +00001386 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1387
1388 // Common code will reject creating a pre-inc form if the base pointer
1389 // is a frame index, or if N is a store and the base pointer is either
1390 // the same as or a predecessor of the value being stored. Check for
1391 // those situations here, and try with swapped Base/Offset instead.
1392 bool Swap = false;
1393
1394 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1395 Swap = true;
1396 else if (!isLoad) {
1397 SDValue Val = cast<StoreSDNode>(N)->getValue();
1398 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1399 Swap = true;
1400 }
1401
1402 if (Swap)
1403 std::swap(Base, Offset);
1404
Hal Finkelca542be2012-06-20 15:43:03 +00001405 AM = ISD::PRE_INC;
1406 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001407 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001408
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001409 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001410 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001411 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001412 return false;
1413 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001414 // LDU/STU need an address with at least 4-byte alignment.
1415 if (Alignment < 4)
1416 return false;
1417
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001418 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001419 return false;
1420 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001421
Chris Lattnerb314b152006-11-11 00:08:42 +00001422 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001423 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1424 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001425 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001426 LD->getExtensionType() == ISD::SEXTLOAD &&
1427 isa<ConstantSDNode>(Offset))
1428 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001429 }
1430
Chris Lattnerce645542006-11-10 02:08:47 +00001431 AM = ISD::PRE_INC;
1432 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001433}
1434
1435//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001436// LowerOperation implementation
1437//===----------------------------------------------------------------------===//
1438
Chris Lattneredb9d842010-11-15 02:46:57 +00001439/// GetLabelAccessInfo - Return true if we should reference labels using a
1440/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1441static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Craig Topper062a2ba2014-04-25 05:30:21 +00001442 unsigned &LoOpFlags,
1443 const GlobalValue *GV = nullptr) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001444 HiOpFlags = PPCII::MO_HA;
1445 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001446
Chris Lattneredb9d842010-11-15 02:46:57 +00001447 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1448 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peck527da1b2010-11-23 03:31:01 +00001449 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattneredb9d842010-11-15 02:46:57 +00001450 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattnerdd6df842010-11-15 03:13:19 +00001451 if (isPIC) {
1452 HiOpFlags |= PPCII::MO_PIC_FLAG;
1453 LoOpFlags |= PPCII::MO_PIC_FLAG;
1454 }
1455
1456 // If this is a reference to a global value that requires a non-lazy-ptr, make
1457 // sure that instruction lowering adds it.
1458 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1459 HiOpFlags |= PPCII::MO_NLP_FLAG;
1460 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001461
Chris Lattnerdd6df842010-11-15 03:13:19 +00001462 if (GV->hasHiddenVisibility()) {
1463 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1464 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1465 }
1466 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001467
Chris Lattneredb9d842010-11-15 02:46:57 +00001468 return isPIC;
1469}
1470
1471static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1472 SelectionDAG &DAG) {
1473 EVT PtrVT = HiPart.getValueType();
1474 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001475 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001476
1477 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1478 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001479
Chris Lattneredb9d842010-11-15 02:46:57 +00001480 // With PIC, the first instruction is actually "GR+hi(&G)".
1481 if (isPIC)
1482 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1483 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001484
Chris Lattneredb9d842010-11-15 02:46:57 +00001485 // Generate non-pic code that has direct accesses to the constant pool.
1486 // The address of the global is just (hi(&g)+lo(&g)).
1487 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1488}
1489
Scott Michelcf0da6c2009-02-17 22:15:04 +00001490SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001491 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001492 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001493 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001494 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001495
Roman Divackyace47072012-08-24 16:26:02 +00001496 // 64-bit SVR4 ABI code is always position-independent.
1497 // The actual address of the GlobalValue is stored in the TOC.
1498 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1499 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001500 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001501 DAG.getRegister(PPC::X2, MVT::i64));
1502 }
1503
Chris Lattneredb9d842010-11-15 02:46:57 +00001504 unsigned MOHiFlag, MOLoFlag;
1505 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1506 SDValue CPIHi =
1507 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1508 SDValue CPILo =
1509 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1510 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001511}
1512
Dan Gohman21cea8a2010-04-17 15:26:15 +00001513SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001514 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001515 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001516
Roman Divackyace47072012-08-24 16:26:02 +00001517 // 64-bit SVR4 ABI code is always position-independent.
1518 // The actual address of the GlobalValue is stored in the TOC.
1519 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1520 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001521 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001522 DAG.getRegister(PPC::X2, MVT::i64));
1523 }
1524
Chris Lattneredb9d842010-11-15 02:46:57 +00001525 unsigned MOHiFlag, MOLoFlag;
1526 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1527 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1528 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1529 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001530}
1531
Dan Gohman21cea8a2010-04-17 15:26:15 +00001532SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1533 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001534 EVT PtrVT = Op.getValueType();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001535
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001536 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peck527da1b2010-11-23 03:31:01 +00001537
Chris Lattneredb9d842010-11-15 02:46:57 +00001538 unsigned MOHiFlag, MOLoFlag;
1539 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001540 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1541 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001542 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1543}
1544
Roman Divackye3f15c982012-06-04 17:36:38 +00001545SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1546 SelectionDAG &DAG) const {
1547
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001548 // FIXME: TLS addresses currently use medium model code sequences,
1549 // which is the most useful form. Eventually support for small and
1550 // large models could be added if users need it, at the cost of
1551 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001552 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001553 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001554 const GlobalValue *GV = GA->getGlobal();
1555 EVT PtrVT = getPointerTy();
1556 bool is64bit = PPCSubTarget.isPPC64();
1557
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001558 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001559
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001560 if (Model == TLSModel::LocalExec) {
1561 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001562 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001563 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001564 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001565 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1566 is64bit ? MVT::i64 : MVT::i32);
1567 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1568 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1569 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001570
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001571 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001572 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001573 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1574 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00001575 SDValue GOTPtr;
1576 if (is64bit) {
1577 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1578 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1579 PtrVT, GOTReg, TGA);
1580 } else
1581 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001582 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00001583 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001584 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001585 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001586
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001587 if (Model == TLSModel::GeneralDynamic) {
1588 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1589 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1590 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1591 GOTReg, TGA);
1592 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1593 GOTEntryHi, TGA);
1594
1595 // We need a chain node, and don't have one handy. The underlying
1596 // call has no side effects, so using the function entry node
1597 // suffices.
1598 SDValue Chain = DAG.getEntryNode();
1599 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1600 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1601 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1602 PtrVT, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001603 // The return value from GET_TLS_ADDR really is in X3 already, but
1604 // some hacks are needed here to tie everything together. The extra
1605 // copies dissolve during subsequent transforms.
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001606 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1607 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1608 }
1609
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001610 if (Model == TLSModel::LocalDynamic) {
1611 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1612 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1613 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1614 GOTReg, TGA);
1615 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1616 GOTEntryHi, TGA);
1617
1618 // We need a chain node, and don't have one handy. The underlying
1619 // call has no side effects, so using the function entry node
1620 // suffices.
1621 SDValue Chain = DAG.getEntryNode();
1622 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1623 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1624 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1625 PtrVT, ParmReg, TGA);
1626 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1627 // some hacks are needed here to tie everything together. The extra
1628 // copies dissolve during subsequent transforms.
1629 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1630 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt9ed4dbc2012-12-13 20:57:10 +00001631 Chain, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001632 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1633 }
1634
1635 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00001636}
1637
Chris Lattneredb9d842010-11-15 02:46:57 +00001638SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1639 SelectionDAG &DAG) const {
1640 EVT PtrVT = Op.getValueType();
1641 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001642 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00001643 const GlobalValue *GV = GSDN->getGlobal();
1644
Chris Lattneredb9d842010-11-15 02:46:57 +00001645 // 64-bit SVR4 ABI code is always position-independent.
1646 // The actual address of the GlobalValue is stored in the TOC.
1647 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1648 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1649 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1650 DAG.getRegister(PPC::X2, MVT::i64));
1651 }
1652
Chris Lattnerdd6df842010-11-15 03:13:19 +00001653 unsigned MOHiFlag, MOLoFlag;
1654 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00001655
Chris Lattnerdd6df842010-11-15 03:13:19 +00001656 SDValue GAHi =
1657 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1658 SDValue GALo =
1659 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00001660
Chris Lattnerdd6df842010-11-15 03:13:19 +00001661 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00001662
Chris Lattnerdd6df842010-11-15 03:13:19 +00001663 // If the global reference is actually to a non-lazy-pointer, we have to do an
1664 // extra load to get the address of the global.
1665 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1666 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001667 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00001668 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00001669}
1670
Dan Gohman21cea8a2010-04-17 15:26:15 +00001671SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00001672 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001673 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001674
Hal Finkel777c9dd2014-03-29 16:04:40 +00001675 if (Op.getValueType() == MVT::v2i64) {
1676 // When the operands themselves are v2i64 values, we need to do something
1677 // special because VSX has no underlying comparison operations for these.
1678 if (Op.getOperand(0).getValueType() == MVT::v2i64) {
1679 // Equality can be handled by casting to the legal type for Altivec
1680 // comparisons, everything else needs to be expanded.
1681 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
1682 return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
1683 DAG.getSetCC(dl, MVT::v4i32,
1684 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)),
1685 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)),
1686 CC));
1687 }
1688
1689 return SDValue();
1690 }
1691
1692 // We handle most of these in the usual way.
1693 return Op;
1694 }
1695
Chris Lattner4211ca92006-04-14 06:01:58 +00001696 // If we're comparing for equality to zero, expose the fact that this is
1697 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1698 // fold the new nodes.
1699 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1700 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001701 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001702 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001703 if (VT.bitsLT(MVT::i32)) {
1704 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001705 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001706 }
Duncan Sands13237ac2008-06-06 12:08:01 +00001707 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001708 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1709 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00001710 DAG.getConstant(Log2b, MVT::i32));
1711 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00001712 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001713 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00001714 // optimized. FIXME: revisit this when we can custom lower all setcc
1715 // optimizations.
1716 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001717 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001718 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001719
Chris Lattner4211ca92006-04-14 06:01:58 +00001720 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00001721 // by xor'ing the rhs with the lhs, which is faster than setting a
1722 // condition register, reading it back out, and masking the correct bit. The
1723 // normal approach here uses sub to do this instead of xor. Using xor exposes
1724 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001725 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00001726 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001727 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001728 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00001729 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001730 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00001731 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001732 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001733}
1734
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001735SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001736 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00001737 SDNode *Node = Op.getNode();
1738 EVT VT = Node->getValueType(0);
1739 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1740 SDValue InChain = Node->getOperand(0);
1741 SDValue VAListPtr = Node->getOperand(1);
1742 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001743 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001744
Roman Divacky4394e682011-06-28 15:30:42 +00001745 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1746
1747 // gpr_index
1748 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1749 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1750 false, false, 0);
1751 InChain = GprIndex.getValue(1);
1752
1753 if (VT == MVT::i64) {
1754 // Check if GprIndex is even
1755 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1756 DAG.getConstant(1, MVT::i32));
1757 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1758 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1759 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1760 DAG.getConstant(1, MVT::i32));
1761 // Align GprIndex to be even if it isn't
1762 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1763 GprIndex);
1764 }
1765
1766 // fpr index is 1 byte after gpr
1767 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1768 DAG.getConstant(1, MVT::i32));
1769
1770 // fpr
1771 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1772 FprPtr, MachinePointerInfo(SV), MVT::i8,
1773 false, false, 0);
1774 InChain = FprIndex.getValue(1);
1775
1776 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1777 DAG.getConstant(8, MVT::i32));
1778
1779 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1780 DAG.getConstant(4, MVT::i32));
1781
1782 // areas
1783 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001784 MachinePointerInfo(), false, false,
1785 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001786 InChain = OverflowArea.getValue(1);
1787
1788 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001789 MachinePointerInfo(), false, false,
1790 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001791 InChain = RegSaveArea.getValue(1);
1792
1793 // select overflow_area if index > 8
1794 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1795 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1796
Roman Divacky4394e682011-06-28 15:30:42 +00001797 // adjustment constant gpr_index * 4/8
1798 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1799 VT.isInteger() ? GprIndex : FprIndex,
1800 DAG.getConstant(VT.isInteger() ? 4 : 8,
1801 MVT::i32));
1802
1803 // OurReg = RegSaveArea + RegConstant
1804 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1805 RegConstant);
1806
1807 // Floating types are 32 bytes into RegSaveArea
1808 if (VT.isFloatingPoint())
1809 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1810 DAG.getConstant(32, MVT::i32));
1811
1812 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1813 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1814 VT.isInteger() ? GprIndex : FprIndex,
1815 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1816 MVT::i32));
1817
1818 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1819 VT.isInteger() ? VAListPtr : FprPtr,
1820 MachinePointerInfo(SV),
1821 MVT::i8, false, false, 0);
1822
1823 // determine if we should load from reg_save_area or overflow_area
1824 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1825
1826 // increase overflow_area by 4/8 if gpr/fpr > 8
1827 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1828 DAG.getConstant(VT.isInteger() ? 4 : 8,
1829 MVT::i32));
1830
1831 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1832 OverflowAreaPlusN);
1833
1834 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1835 OverflowAreaPtr,
1836 MachinePointerInfo(),
1837 MVT::i32, false, false, 0);
1838
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00001839 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001840 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001841}
1842
Roman Divackyc3825df2013-07-25 21:36:47 +00001843SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
1844 const PPCSubtarget &Subtarget) const {
1845 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
1846
1847 // We have to copy the entire va_list struct:
1848 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
1849 return DAG.getMemcpy(Op.getOperand(0), Op,
1850 Op.getOperand(1), Op.getOperand(2),
1851 DAG.getConstant(12, MVT::i32), 8, false, true,
1852 MachinePointerInfo(), MachinePointerInfo());
1853}
1854
Duncan Sandsa0984362011-09-06 13:37:06 +00001855SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1856 SelectionDAG &DAG) const {
1857 return Op.getOperand(0);
1858}
1859
1860SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1861 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00001862 SDValue Chain = Op.getOperand(0);
1863 SDValue Trmp = Op.getOperand(1); // trampoline
1864 SDValue FPtr = Op.getOperand(2); // nested function
1865 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00001866 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00001867
Owen Anderson53aa7a92009-08-10 22:56:29 +00001868 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00001869 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00001870 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001871 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00001872 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00001873
Scott Michelcf0da6c2009-02-17 22:15:04 +00001874 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00001875 TargetLowering::ArgListEntry Entry;
1876
1877 Entry.Ty = IntPtrTy;
1878 Entry.Node = Trmp; Args.push_back(Entry);
1879
1880 // TrampSize == (isPPC64 ? 48 : 40);
1881 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00001882 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00001883 Args.push_back(Entry);
1884
1885 Entry.Node = FPtr; Args.push_back(Entry);
1886 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001887
Bill Wendling95e1af22008-09-17 00:30:57 +00001888 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Justin Holewinskiaa583972012-05-25 16:35:28 +00001889 TargetLowering::CallLoweringInfo CLI(Chain,
1890 Type::getVoidTy(*DAG.getContext()),
1891 false, false, false, false, 0,
1892 CallingConv::C,
Evan Cheng65f9d192012-02-28 18:51:51 +00001893 /*isTailCall=*/false,
Justin Holewinskiaa583972012-05-25 16:35:28 +00001894 /*doesNotRet=*/false,
1895 /*isReturnValueUsed=*/true,
Bill Wendling95e1af22008-09-17 00:30:57 +00001896 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00001897 Args, DAG, dl);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001898 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bill Wendling95e1af22008-09-17 00:30:57 +00001899
Duncan Sandsa0984362011-09-06 13:37:06 +00001900 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00001901}
1902
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001903SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001904 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001905 MachineFunction &MF = DAG.getMachineFunction();
1906 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1907
Andrew Trickef9de2a2013-05-25 02:42:55 +00001908 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001909
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001910 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001911 // vastart just stores the address of the VarArgsFrameIndex slot into the
1912 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001913 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00001914 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001915 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00001916 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1917 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00001918 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001919 }
1920
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00001921 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001922 // We suppose the given va_list is already allocated.
1923 //
1924 // typedef struct {
1925 // char gpr; /* index into the array of 8 GPRs
1926 // * stored in the register save area
1927 // * gpr=0 corresponds to r3,
1928 // * gpr=1 to r4, etc.
1929 // */
1930 // char fpr; /* index into the array of 8 FPRs
1931 // * stored in the register save area
1932 // * fpr=0 corresponds to f1,
1933 // * fpr=1 to f2, etc.
1934 // */
1935 // char *overflow_arg_area;
1936 // /* location on stack that holds
1937 // * the next overflow argument
1938 // */
1939 // char *reg_save_area;
1940 // /* where r3:r10 and f1:f8 (if saved)
1941 // * are stored
1942 // */
1943 // } va_list[1];
1944
1945
Dan Gohman31ae5862010-04-17 14:41:14 +00001946 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1947 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001948
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001949
Owen Anderson53aa7a92009-08-10 22:56:29 +00001950 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001951
Dan Gohman31ae5862010-04-17 14:41:14 +00001952 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1953 PtrVT);
1954 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1955 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001956
Duncan Sands13237ac2008-06-06 12:08:01 +00001957 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001958 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001959
Duncan Sands13237ac2008-06-06 12:08:01 +00001960 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001961 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001962
1963 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001964 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001965
Dan Gohman2d489b52008-02-06 22:27:42 +00001966 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001967
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001968 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00001969 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00001970 Op.getOperand(1),
1971 MachinePointerInfo(SV),
1972 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001973 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001974 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001975 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001976
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001977 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001978 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00001979 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1980 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00001981 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001982 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001983 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001984
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001985 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001986 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00001987 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1988 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001989 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001990 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00001991 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001992
1993 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00001994 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1995 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00001996 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001997
Chris Lattner4211ca92006-04-14 06:01:58 +00001998}
1999
Chris Lattner4f2e4e02007-03-06 00:59:59 +00002000#include "PPCGenCallingConv.inc"
2001
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002002// Function whose sole purpose is to kill compiler warnings
2003// stemming from unused functions included from PPCGenCallingConv.inc.
2004CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00002005 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002006}
2007
Bill Schmidt230b4512013-06-12 16:39:22 +00002008bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
2009 CCValAssign::LocInfo &LocInfo,
2010 ISD::ArgFlagsTy &ArgFlags,
2011 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002012 return true;
2013}
2014
Bill Schmidt230b4512013-06-12 16:39:22 +00002015bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
2016 MVT &LocVT,
2017 CCValAssign::LocInfo &LocInfo,
2018 ISD::ArgFlagsTy &ArgFlags,
2019 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002020 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002021 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2022 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2023 };
2024 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002025
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002026 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
2027
2028 // Skip one register if the first unallocated register has an even register
2029 // number and there are still argument registers available which have not been
2030 // allocated yet. RegNum is actually an index into ArgRegs, which means we
2031 // need to skip a register if RegNum is odd.
2032 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
2033 State.AllocateReg(ArgRegs[RegNum]);
2034 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002035
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002036 // Always return false here, as this function only makes sure that the first
2037 // unallocated register has an odd register number and does not actually
2038 // allocate a register for the current argument.
2039 return false;
2040}
2041
Bill Schmidt230b4512013-06-12 16:39:22 +00002042bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
2043 MVT &LocVT,
2044 CCValAssign::LocInfo &LocInfo,
2045 ISD::ArgFlagsTy &ArgFlags,
2046 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002047 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002048 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2049 PPC::F8
2050 };
2051
2052 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002053
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002054 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
2055
2056 // If there is only one Floating-point register left we need to put both f64
2057 // values of a split ppc_fp128 value on the stack.
2058 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
2059 State.AllocateReg(ArgRegs[RegNum]);
2060 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002061
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002062 // Always return false here, as this function only makes sure that the two f64
2063 // values a ppc_fp128 value is split into are both passed in registers or both
2064 // passed on the stack and does not actually allocate a register for the
2065 // current argument.
2066 return false;
2067}
2068
Chris Lattner43df5b32007-02-25 05:34:32 +00002069/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002070/// on Darwin.
Craig Topper840beec2014-04-04 05:16:06 +00002071static const MCPhysReg *GetFPR() {
2072 static const MCPhysReg FPR[] = {
Chris Lattner43df5b32007-02-25 05:34:32 +00002073 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002074 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner43df5b32007-02-25 05:34:32 +00002075 };
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002076
Chris Lattner43df5b32007-02-25 05:34:32 +00002077 return FPR;
2078}
2079
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002080/// CalculateStackSlotSize - Calculates the size reserved for this argument on
2081/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002082static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002083 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00002084 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002085 if (Flags.isByVal())
2086 ArgSize = Flags.getByValSize();
2087 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2088
2089 return ArgSize;
2090}
2091
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002092SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002093PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002094 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002095 const SmallVectorImpl<ISD::InputArg>
2096 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002097 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002098 SmallVectorImpl<SDValue> &InVals)
2099 const {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002100 if (PPCSubTarget.isSVR4ABI()) {
2101 if (PPCSubTarget.isPPC64())
2102 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
2103 dl, DAG, InVals);
2104 else
2105 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2106 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002107 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002108 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2109 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002110 }
2111}
2112
2113SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002114PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002115 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002116 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002117 const SmallVectorImpl<ISD::InputArg>
2118 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002119 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002120 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002121
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002122 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002123 // +-----------------------------------+
2124 // +--> | Back chain |
2125 // | +-----------------------------------+
2126 // | | Floating-point register save area |
2127 // | +-----------------------------------+
2128 // | | General register save area |
2129 // | +-----------------------------------+
2130 // | | CR save word |
2131 // | +-----------------------------------+
2132 // | | VRSAVE save word |
2133 // | +-----------------------------------+
2134 // | | Alignment padding |
2135 // | +-----------------------------------+
2136 // | | Vector register save area |
2137 // | +-----------------------------------+
2138 // | | Local variable space |
2139 // | +-----------------------------------+
2140 // | | Parameter list area |
2141 // | +-----------------------------------+
2142 // | | LR save word |
2143 // | +-----------------------------------+
2144 // SP--> +--- | Back chain |
2145 // +-----------------------------------+
2146 //
2147 // Specifications:
2148 // System V Application Binary Interface PowerPC Processor Supplement
2149 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002150
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002151 MachineFunction &MF = DAG.getMachineFunction();
2152 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002153 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002154
Owen Anderson53aa7a92009-08-10 22:56:29 +00002155 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002156 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002157 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2158 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002159 unsigned PtrByteSize = 4;
2160
2161 // Assign locations to all of the incoming arguments.
2162 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002163 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002164 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002165
2166 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002167 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002168
Bill Schmidtef17c142013-02-06 17:33:58 +00002169 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002170
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002171 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2172 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002173
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002174 // Arguments stored in registers.
2175 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002176 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002177 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002178
Owen Anderson9f944592009-08-11 20:47:22 +00002179 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002180 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002181 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002182 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002183 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002184 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002185 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002186 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00002187 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002188 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002189 case MVT::f64:
Hal Finkel19be5062014-03-29 05:29:01 +00002190 if (PPCSubTarget.hasVSX())
2191 RC = &PPC::VSFRCRegClass;
2192 else
2193 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002194 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002195 case MVT::v16i8:
2196 case MVT::v8i16:
2197 case MVT::v4i32:
2198 case MVT::v4f32:
Hal Finkel7811c612014-03-28 19:58:11 +00002199 RC = &PPC::VRRCRegClass;
2200 break;
Hal Finkel27774d92014-03-13 07:58:58 +00002201 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002202 case MVT::v2i64:
Hal Finkel7811c612014-03-28 19:58:11 +00002203 RC = &PPC::VSHRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002204 break;
2205 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002206
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002207 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002208 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002209 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2210 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2211
2212 if (ValVT == MVT::i1)
2213 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002214
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002215 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002216 } else {
2217 // Argument stored in memory.
2218 assert(VA.isMemLoc());
2219
Hal Finkel940ab932014-02-28 00:27:01 +00002220 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002221 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002222 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002223
2224 // Create load nodes to retrieve arguments from the stack.
2225 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002226 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2227 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002228 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002229 }
2230 }
2231
2232 // Assign locations to all of the incoming aggregate by value arguments.
2233 // Aggregates passed by value are stored in the local variable space of the
2234 // caller's stack frame, right above the parameter list area.
2235 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002236 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002237 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002238
2239 // Reserve stack space for the allocations in CCInfo.
2240 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2241
Bill Schmidtef17c142013-02-06 17:33:58 +00002242 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002243
2244 // Area that is at least reserved in the caller of this function.
2245 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00002246
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002247 // Set the size that is at least reserved in caller of this function. Tail
2248 // call optimized function's reserved stack space needs to be aligned so that
2249 // taking the difference between two stack areas will result in an aligned
2250 // stack.
2251 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2252
2253 MinReservedArea =
2254 std::max(MinReservedArea,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002255 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peck527da1b2010-11-23 03:31:01 +00002256
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002257 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002258 getStackAlignment();
2259 unsigned AlignMask = TargetAlign-1;
2260 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peck527da1b2010-11-23 03:31:01 +00002261
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002262 FI->setMinReservedArea(MinReservedArea);
2263
2264 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002265
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002266 // If the function takes variable number of arguments, make a frame index for
2267 // the start of the first vararg value... for expansion of llvm.va_start.
2268 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +00002269 static const MCPhysReg GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002270 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2271 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2272 };
2273 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2274
Craig Topper840beec2014-04-04 05:16:06 +00002275 static const MCPhysReg FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002276 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2277 PPC::F8
2278 };
2279 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2280
Dan Gohman31ae5862010-04-17 14:41:14 +00002281 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2282 NumGPArgRegs));
2283 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2284 NumFPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002285
2286 // Make room for NumGPArgRegs and NumFPArgRegs.
2287 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson9f944592009-08-11 20:47:22 +00002288 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002289
Dan Gohman31ae5862010-04-17 14:41:14 +00002290 FuncInfo->setVarArgsStackOffset(
2291 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002292 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002293
Dan Gohman31ae5862010-04-17 14:41:14 +00002294 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2295 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002296
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002297 // The fixed integer arguments of a variadic function are stored to the
2298 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2299 // the result of va_next.
2300 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2301 // Get an existing live-in vreg, or add a new one.
2302 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2303 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002304 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002305
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002306 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002307 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2308 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002309 MemOps.push_back(Store);
2310 // Increment the address by four for the next argument to store
2311 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2312 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2313 }
2314
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002315 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2316 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002317 // The double arguments are stored to the VarArgsFrameIndex
2318 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002319 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2320 // Get an existing live-in vreg, or add a new one.
2321 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2322 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002323 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002324
Owen Anderson9f944592009-08-11 20:47:22 +00002325 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002326 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2327 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002328 MemOps.push_back(Store);
2329 // Increment the address by eight for the next argument to store
Owen Anderson9f944592009-08-11 20:47:22 +00002330 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002331 PtrVT);
2332 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2333 }
2334 }
2335
2336 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002337 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002338
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002339 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002340}
2341
Bill Schmidt57d6de52012-10-23 15:51:16 +00002342// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2343// value to MVT::i64 and then truncate to the correct register size.
2344SDValue
2345PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2346 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002347 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002348 if (Flags.isSExt())
2349 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2350 DAG.getValueType(ObjectVT));
2351 else if (Flags.isZExt())
2352 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2353 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002354
Hal Finkel940ab932014-02-28 00:27:01 +00002355 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002356}
2357
2358// Set the size that is at least reserved in caller of this function. Tail
2359// call optimized functions' reserved stack space needs to be aligned so that
2360// taking the difference between two stack areas will result in an aligned
2361// stack.
2362void
2363PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2364 unsigned nAltivecParamsAtEnd,
2365 unsigned MinReservedArea,
2366 bool isPPC64) const {
2367 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2368 // Add the Altivec parameters at the end, if needed.
2369 if (nAltivecParamsAtEnd) {
2370 MinReservedArea = ((MinReservedArea+15)/16)*16;
2371 MinReservedArea += 16*nAltivecParamsAtEnd;
2372 }
2373 MinReservedArea =
2374 std::max(MinReservedArea,
2375 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2376 unsigned TargetAlign
2377 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2378 getStackAlignment();
2379 unsigned AlignMask = TargetAlign-1;
2380 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2381 FI->setMinReservedArea(MinReservedArea);
2382}
2383
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002384SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002385PPCTargetLowering::LowerFormalArguments_64SVR4(
2386 SDValue Chain,
2387 CallingConv::ID CallConv, bool isVarArg,
2388 const SmallVectorImpl<ISD::InputArg>
2389 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002390 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002391 SmallVectorImpl<SDValue> &InVals) const {
2392 // TODO: add description of PPC stack frame format, or at least some docs.
2393 //
2394 MachineFunction &MF = DAG.getMachineFunction();
2395 MachineFrameInfo *MFI = MF.getFrameInfo();
2396 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2397
2398 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2399 // Potential tail calls could cause overwriting of argument stack slots.
2400 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2401 (CallConv == CallingConv::Fast));
2402 unsigned PtrByteSize = 8;
2403
2404 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2405 // Area that is at least reserved in caller of this function.
2406 unsigned MinReservedArea = ArgOffset;
2407
Craig Topper840beec2014-04-04 05:16:06 +00002408 static const MCPhysReg GPR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002409 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2410 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2411 };
2412
Craig Topper840beec2014-04-04 05:16:06 +00002413 static const MCPhysReg *FPR = GetFPR();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002414
Craig Topper840beec2014-04-04 05:16:06 +00002415 static const MCPhysReg VR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002416 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2417 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2418 };
Craig Topper840beec2014-04-04 05:16:06 +00002419 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00002420 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
2421 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
2422 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002423
2424 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2425 const unsigned Num_FPR_Regs = 13;
2426 const unsigned Num_VR_Regs = array_lengthof(VR);
2427
2428 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2429
2430 // Add DAG nodes to load the arguments or copy them out of registers. On
2431 // entry to a function on PPC, the arguments start after the linkage area,
2432 // although the first ones are often in registers.
2433
2434 SmallVector<SDValue, 8> MemOps;
2435 unsigned nAltivecParamsAtEnd = 0;
2436 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002437 unsigned CurArgIdx = 0;
2438 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002439 SDValue ArgVal;
2440 bool needsLoad = false;
2441 EVT ObjectVT = Ins[ArgNo].VT;
Hal Finkel940ab932014-02-28 00:27:01 +00002442 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002443 unsigned ArgSize = ObjSize;
2444 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt6631e942013-02-20 17:31:41 +00002445 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2446 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002447
2448 unsigned CurArgOffset = ArgOffset;
2449
2450 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2451 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
Hal Finkel27774d92014-03-13 07:58:58 +00002452 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8 ||
Hal Finkela6c8b512014-03-26 16:12:58 +00002453 ObjectVT==MVT::v2f64 || ObjectVT==MVT::v2i64) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002454 if (isVarArg) {
2455 MinReservedArea = ((MinReservedArea+15)/16)*16;
2456 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2457 Flags,
2458 PtrByteSize);
2459 } else
2460 nAltivecParamsAtEnd++;
2461 } else
2462 // Calculate min reserved area.
2463 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2464 Flags,
2465 PtrByteSize);
2466
2467 // FIXME the codegen can be much improved in some cases.
2468 // We do not have to keep everything in memory.
2469 if (Flags.isByVal()) {
2470 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2471 ObjSize = Flags.getByValSize();
2472 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002473 // Empty aggregate parameters do not take up registers. Examples:
2474 // struct { } a;
2475 // union { } b;
2476 // int c[0];
2477 // etc. However, we have to provide a place-holder in InVals, so
2478 // pretend we have an 8-byte item at the current address for that
2479 // purpose.
2480 if (!ObjSize) {
2481 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2482 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2483 InVals.push_back(FIN);
2484 continue;
2485 }
Hal Finkel262a2242013-09-12 23:20:06 +00002486
2487 unsigned BVAlign = Flags.getByValAlign();
2488 if (BVAlign > 8) {
2489 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
2490 CurArgOffset = ArgOffset;
2491 }
2492
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002493 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt48081ca2012-10-16 13:30:53 +00002494 if (ObjSize < PtrByteSize)
2495 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002496 // The value of the object is its address.
2497 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2498 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2499 InVals.push_back(FIN);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002500
2501 if (ObjSize < 8) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002502 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002503 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002504 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002505 SDValue Store;
2506
2507 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2508 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2509 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2510 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002511 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002512 ObjType, false, false, 0);
2513 } else {
2514 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2515 // store the whole register as-is to the parameter save area
2516 // slot. The address of the parameter was already calculated
2517 // above (InVals.push_back(FIN)) to be the right-justified
2518 // offset within the slot. For this store, we need a new
2519 // frame index that points at the beginning of the slot.
2520 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2521 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2522 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002523 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002524 false, false, 0);
2525 }
2526
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002527 MemOps.push_back(Store);
2528 ++GPR_idx;
2529 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002530 // Whether we copied from a register or not, advance the offset
2531 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002532 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002533 continue;
2534 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002535
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002536 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2537 // Store whatever pieces of the object are in registers
2538 // to memory. ArgOffset will be the address of the beginning
2539 // of the object.
2540 if (GPR_idx != Num_GPR_Regs) {
2541 unsigned VReg;
2542 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2543 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2544 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2545 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002546 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002547 MachinePointerInfo(FuncArg, j),
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002548 false, false, 0);
2549 MemOps.push_back(Store);
2550 ++GPR_idx;
2551 ArgOffset += PtrByteSize;
2552 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00002553 ArgOffset += ArgSize - j;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002554 break;
2555 }
2556 }
2557 continue;
2558 }
2559
2560 switch (ObjectVT.getSimpleVT().SimpleTy) {
2561 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00002562 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002563 case MVT::i32:
2564 case MVT::i64:
2565 if (GPR_idx != Num_GPR_Regs) {
2566 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2567 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2568
Hal Finkel940ab932014-02-28 00:27:01 +00002569 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002570 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2571 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002572 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002573
2574 ++GPR_idx;
2575 } else {
2576 needsLoad = true;
2577 ArgSize = PtrByteSize;
2578 }
2579 ArgOffset += 8;
2580 break;
2581
2582 case MVT::f32:
2583 case MVT::f64:
2584 // Every 8 bytes of argument space consumes one of the GPRs available for
2585 // argument passing.
2586 if (GPR_idx != Num_GPR_Regs) {
2587 ++GPR_idx;
2588 }
2589 if (FPR_idx != Num_FPR_Regs) {
2590 unsigned VReg;
2591
2592 if (ObjectVT == MVT::f32)
2593 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2594 else
Hal Finkel19be5062014-03-29 05:29:01 +00002595 VReg = MF.addLiveIn(FPR[FPR_idx], PPCSubTarget.hasVSX() ?
2596 &PPC::VSFRCRegClass :
2597 &PPC::F8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002598
2599 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2600 ++FPR_idx;
2601 } else {
2602 needsLoad = true;
Bill Schmidt22162472012-10-11 15:38:20 +00002603 ArgSize = PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002604 }
2605
2606 ArgOffset += 8;
2607 break;
2608 case MVT::v4f32:
2609 case MVT::v4i32:
2610 case MVT::v8i16:
2611 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00002612 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002613 case MVT::v2i64:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002614 // Note that vector arguments in registers don't reserve stack space,
2615 // except in varargs functions.
2616 if (VR_idx != Num_VR_Regs) {
Hal Finkel7811c612014-03-28 19:58:11 +00002617 unsigned VReg = (ObjectVT == MVT::v2f64 || ObjectVT == MVT::v2i64) ?
2618 MF.addLiveIn(VSRH[VR_idx], &PPC::VSHRCRegClass) :
2619 MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002620 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2621 if (isVarArg) {
2622 while ((ArgOffset % 16) != 0) {
2623 ArgOffset += PtrByteSize;
2624 if (GPR_idx != Num_GPR_Regs)
2625 GPR_idx++;
2626 }
2627 ArgOffset += 16;
2628 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2629 }
2630 ++VR_idx;
2631 } else {
2632 // Vectors are aligned.
2633 ArgOffset = ((ArgOffset+15)/16)*16;
2634 CurArgOffset = ArgOffset;
2635 ArgOffset += 16;
2636 needsLoad = true;
2637 }
2638 break;
2639 }
2640
2641 // We need to load the argument to a virtual register if we determined
2642 // above that we ran out of physical registers of the appropriate type.
2643 if (needsLoad) {
2644 int FI = MFI->CreateFixedObject(ObjSize,
2645 CurArgOffset + (ArgSize - ObjSize),
2646 isImmutable);
2647 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2648 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2649 false, false, false, 0);
2650 }
2651
2652 InVals.push_back(ArgVal);
2653 }
2654
2655 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002656 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002657 // taking the difference between two stack areas will result in an aligned
2658 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002659 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002660
2661 // If the function takes variable number of arguments, make a frame index for
2662 // the start of the first vararg value... for expansion of llvm.va_start.
2663 if (isVarArg) {
2664 int Depth = ArgOffset;
2665
2666 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00002667 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002668 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2669
2670 // If this function is vararg, store any remaining integer argument regs
2671 // to their spots on the stack so that they may be loaded by deferencing the
2672 // result of va_next.
2673 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2674 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2675 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2676 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2677 MachinePointerInfo(), false, false, 0);
2678 MemOps.push_back(Store);
2679 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00002680 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002681 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2682 }
2683 }
2684
2685 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002686 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002687
2688 return Chain;
2689}
2690
2691SDValue
2692PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002693 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002694 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002695 const SmallVectorImpl<ISD::InputArg>
2696 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002697 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002698 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002699 // TODO: add description of PPC stack frame format, or at least some docs.
2700 //
2701 MachineFunction &MF = DAG.getMachineFunction();
2702 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002703 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002704
Owen Anderson53aa7a92009-08-10 22:56:29 +00002705 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002706 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002707 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002708 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2709 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00002710 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey48850c12006-11-16 22:43:37 +00002711
Anton Korobeynikov2f931282011-01-10 12:39:04 +00002712 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002713 // Area that is at least reserved in caller of this function.
2714 unsigned MinReservedArea = ArgOffset;
2715
Craig Topper840beec2014-04-04 05:16:06 +00002716 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002717 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2718 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2719 };
Craig Topper840beec2014-04-04 05:16:06 +00002720 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00002721 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2722 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2723 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00002724
Craig Topper840beec2014-04-04 05:16:06 +00002725 static const MCPhysReg *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002726
Craig Topper840beec2014-04-04 05:16:06 +00002727 static const MCPhysReg VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002728 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2729 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2730 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00002731
Owen Andersone2f23a32007-09-07 04:06:50 +00002732 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002733 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00002734 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00002735
2736 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002737
Craig Topper840beec2014-04-04 05:16:06 +00002738 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002739
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002740 // In 32-bit non-varargs functions, the stack space for vectors is after the
2741 // stack space for non-vectors. We do not use this space unless we have
2742 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00002743 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002744 // that out...for the pathological case, compute VecArgOffset as the
2745 // start of the vector parameter area. Computing VecArgOffset is the
2746 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002747 unsigned VecArgOffset = ArgOffset;
2748 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002749 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002750 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002751 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002752 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002753
Duncan Sandsd97eea32008-03-21 09:14:45 +00002754 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002755 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00002756 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002757 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002758 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2759 VecArgOffset += ArgSize;
2760 continue;
2761 }
2762
Owen Anderson9f944592009-08-11 20:47:22 +00002763 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002764 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002765 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002766 case MVT::i32:
2767 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002768 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002769 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002770 case MVT::i64: // PPC64
2771 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002772 // FIXME: We are guaranteed to be !isPPC64 at this point.
2773 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002774 VecArgOffset += 8;
2775 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002776 case MVT::v4f32:
2777 case MVT::v4i32:
2778 case MVT::v8i16:
2779 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002780 // Nothing to do, we're only looking at Nonvector args here.
2781 break;
2782 }
2783 }
2784 }
2785 // We've found where the vector parameter area in memory is. Skip the
2786 // first 12 parameters; these don't use that memory.
2787 VecArgOffset = ((VecArgOffset+15)/16)*16;
2788 VecArgOffset += 12*16;
2789
Chris Lattner4302e8f2006-05-16 18:18:50 +00002790 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00002791 // entry to a function on PPC, the arguments start after the linkage area,
2792 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00002793
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002794 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002795 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00002796 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002797 unsigned CurArgIdx = 0;
2798 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002799 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002800 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002801 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00002802 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00002803 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002804 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt38b6cb52013-05-08 17:22:33 +00002805 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2806 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002807
Chris Lattner318f0d22006-05-16 18:51:52 +00002808 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002809
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002810 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00002811 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2812 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002813 if (isVarArg || isPPC64) {
2814 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002815 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002816 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002817 PtrByteSize);
2818 } else nAltivecParamsAtEnd++;
2819 } else
2820 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002821 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00002822 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002823 PtrByteSize);
2824
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002825 // FIXME the codegen can be much improved in some cases.
2826 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002827 if (Flags.isByVal()) {
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002828 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00002829 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002830 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002831 // Objects of size 1 and 2 are right justified, everything else is
2832 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00002833 if (ObjSize==1 || ObjSize==2) {
2834 CurArgOffset = CurArgOffset + (4 - ObjSize);
2835 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002836 // The value of the object is its address.
Evan Cheng0664a672010-07-03 00:40:23 +00002837 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002838 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002839 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002840 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00002841 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002842 unsigned VReg;
2843 if (isPPC64)
2844 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2845 else
2846 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002847 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002848 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002849 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002850 MachinePointerInfo(FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002851 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00002852 MemOps.push_back(Store);
2853 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00002854 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002855
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002856 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00002857
Dale Johannesen21a8f142008-03-08 01:41:42 +00002858 continue;
2859 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002860 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2861 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002862 // to memory. ArgOffset will be the address of the beginning
2863 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002864 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00002865 unsigned VReg;
2866 if (isPPC64)
2867 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2868 else
2869 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00002870 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002871 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002872 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002873 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002874 MachinePointerInfo(FuncArg, j),
David Greene87a5abe2010-02-15 16:56:53 +00002875 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002876 MemOps.push_back(Store);
2877 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002878 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00002879 } else {
2880 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2881 break;
2882 }
2883 }
2884 continue;
2885 }
2886
Owen Anderson9f944592009-08-11 20:47:22 +00002887 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002888 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002889 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002890 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00002891 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00002892 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002893 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002894 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Hal Finkel7f908e82014-03-06 00:45:19 +00002895
2896 if (ObjectVT == MVT::i1)
2897 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal);
2898
Bill Wendling968f32c2008-03-07 20:49:02 +00002899 ++GPR_idx;
2900 } else {
2901 needsLoad = true;
2902 ArgSize = PtrByteSize;
2903 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002904 // All int arguments reserve stack space in the Darwin ABI.
2905 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00002906 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002907 }
Bill Wendling968f32c2008-03-07 20:49:02 +00002908 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00002909 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00002910 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002911 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00002912 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00002913
Hal Finkel940ab932014-02-28 00:27:01 +00002914 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00002915 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00002916 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002917 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00002918
Chris Lattnerec78cad2006-06-26 22:48:35 +00002919 ++GPR_idx;
2920 } else {
2921 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00002922 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002923 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002924 // All int arguments reserve stack space in the Darwin ABI.
2925 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00002926 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002927
Owen Anderson9f944592009-08-11 20:47:22 +00002928 case MVT::f32:
2929 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00002930 // Every 4 bytes of argument space consumes one of the GPRs available for
2931 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002932 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002933 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00002934 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002935 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00002936 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002937 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002938 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002939
Owen Anderson9f944592009-08-11 20:47:22 +00002940 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00002941 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002942 else
Devang Patelf3292b22011-02-21 23:21:26 +00002943 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002944
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002945 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002946 ++FPR_idx;
2947 } else {
2948 needsLoad = true;
2949 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002950
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002951 // All FP arguments reserve stack space in the Darwin ABI.
2952 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00002953 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002954 case MVT::v4f32:
2955 case MVT::v4i32:
2956 case MVT::v8i16:
2957 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00002958 // Note that vector arguments in registers don't reserve stack space,
2959 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00002960 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00002961 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002962 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00002963 if (isVarArg) {
2964 while ((ArgOffset % 16) != 0) {
2965 ArgOffset += PtrByteSize;
2966 if (GPR_idx != Num_GPR_Regs)
2967 GPR_idx++;
2968 }
2969 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002970 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00002971 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002972 ++VR_idx;
2973 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002974 if (!isVarArg && !isPPC64) {
2975 // Vectors go after all the nonvectors.
2976 CurArgOffset = VecArgOffset;
2977 VecArgOffset += 16;
2978 } else {
2979 // Vectors are aligned.
2980 ArgOffset = ((ArgOffset+15)/16)*16;
2981 CurArgOffset = ArgOffset;
2982 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00002983 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00002984 needsLoad = true;
2985 }
2986 break;
2987 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002988
Chris Lattner4302e8f2006-05-16 18:18:50 +00002989 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002990 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002991 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00002992 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002993 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00002994 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002995 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002996 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002997 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00002998 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002999
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003000 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003001 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003002
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003003 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003004 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003005 // taking the difference between two stack areas will result in an aligned
3006 // stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003007 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003008
Chris Lattner4302e8f2006-05-16 18:18:50 +00003009 // If the function takes variable number of arguments, make a frame index for
3010 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003011 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003012 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003013
Dan Gohman31ae5862010-04-17 14:41:14 +00003014 FuncInfo->setVarArgsFrameIndex(
3015 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00003016 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00003017 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003018
Chris Lattner4302e8f2006-05-16 18:18:50 +00003019 // If this function is vararg, store any remaining integer argument regs
3020 // to their spots on the stack so that they may be loaded by deferencing the
3021 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003022 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00003023 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00003024
Chris Lattner2cca3852006-11-18 01:57:19 +00003025 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00003026 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003027 else
Devang Patelf3292b22011-02-21 23:21:26 +00003028 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003029
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003030 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00003031 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3032 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003033 MemOps.push_back(Store);
3034 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003035 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00003036 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003037 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003038 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003039
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003040 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003041 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003042
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003043 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003044}
3045
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003046/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
3047/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003048static unsigned
3049CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
3050 bool isPPC64,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003051 bool isVarArg,
3052 unsigned CC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003053 const SmallVectorImpl<ISD::OutputArg>
3054 &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003055 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003056 unsigned &nAltivecParamsAtEnd) {
3057 // Count how many bytes are to be pushed on the stack, including the linkage
3058 // area, and parameter passing area. We start with 24/48 bytes, which is
3059 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003060 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003061 unsigned NumOps = Outs.size();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003062 unsigned PtrByteSize = isPPC64 ? 8 : 4;
3063
3064 // Add up all the space actually used.
3065 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
3066 // they all go in registers, but we must reserve stack space for them for
3067 // possible use by the caller. In varargs or 64-bit calls, parameters are
3068 // assigned stack space in order, with padding so Altivec parameters are
3069 // 16-byte aligned.
3070 nAltivecParamsAtEnd = 0;
3071 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003072 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003073 EVT ArgVT = Outs[i].VT;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003074 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00003075 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
Hal Finkel27774d92014-03-13 07:58:58 +00003076 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8 ||
Hal Finkela6c8b512014-03-26 16:12:58 +00003077 ArgVT==MVT::v2f64 || ArgVT==MVT::v2i64) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003078 if (!isVarArg && !isPPC64) {
3079 // Non-varargs Altivec parameters go after all the non-Altivec
3080 // parameters; handle those later so we know how much padding we need.
3081 nAltivecParamsAtEnd++;
3082 continue;
3083 }
3084 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
3085 NumBytes = ((NumBytes+15)/16)*16;
3086 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003087 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003088 }
3089
3090 // Allow for Altivec parameters at the end, if needed.
3091 if (nAltivecParamsAtEnd) {
3092 NumBytes = ((NumBytes+15)/16)*16;
3093 NumBytes += 16*nAltivecParamsAtEnd;
3094 }
3095
3096 // The prolog code of the callee may store up to 8 GPR argument registers to
3097 // the stack, allowing va_start to index over them in memory if its varargs.
3098 // Because we cannot tell if this is needed on the caller side, we have to
3099 // conservatively assume that it is needed. As such, make sure we have at
3100 // least enough stack space for the caller to store the 8 GPRs.
3101 NumBytes = std::max(NumBytes,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003102 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003103
3104 // Tail call needs the stack to be aligned.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003105 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
3106 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
3107 getFrameLowering()->getStackAlignment();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003108 unsigned AlignMask = TargetAlign-1;
3109 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
3110 }
3111
3112 return NumBytes;
3113}
3114
3115/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003116/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00003117static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003118 unsigned ParamSize) {
3119
Dale Johannesen86dcae12009-11-24 01:09:07 +00003120 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003121
3122 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
3123 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3124 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3125 // Remember only if the new adjustement is bigger.
3126 if (SPDiff < FI->getTailCallSPDelta())
3127 FI->setTailCallSPDelta(SPDiff);
3128
3129 return SPDiff;
3130}
3131
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003132/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3133/// for tail call optimization. Targets which want to do tail call
3134/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003135bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003136PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003137 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003138 bool isVarArg,
3139 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003140 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003141 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00003142 return false;
3143
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003144 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003145 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003146 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003147
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003148 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00003149 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003150 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
3151 // Functions containing by val parameters are not supported.
3152 for (unsigned i = 0; i != Ins.size(); i++) {
3153 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3154 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003155 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003156
Alp Tokerf907b892013-12-05 05:44:44 +00003157 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003158 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
3159 return true;
3160
3161 // At the moment we can only do local tail calls (in same module, hidden
3162 // or protected) if we are generating PIC.
3163 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
3164 return G->getGlobal()->hasHiddenVisibility()
3165 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003166 }
3167
3168 return false;
3169}
3170
Chris Lattnereb755fc2006-05-17 19:00:46 +00003171/// isCallCompatibleAddress - Return the immediate to use if the specified
3172/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003173static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00003174 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003175 if (!C) return nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003176
Dan Gohmaneffb8942008-09-12 16:56:44 +00003177 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003178 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00003179 SignExtend32<26>(Addr) != Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +00003180 return nullptr; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003181
Dan Gohmaneffb8942008-09-12 16:56:44 +00003182 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00003183 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003184}
3185
Dan Gohmand78c4002008-05-13 00:00:25 +00003186namespace {
3187
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003188struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003189 SDValue Arg;
3190 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003191 int FrameIdx;
3192
3193 TailCallArgumentInfo() : FrameIdx(0) {}
3194};
3195
Dan Gohmand78c4002008-05-13 00:00:25 +00003196}
3197
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003198/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3199static void
3200StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003201 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003202 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3203 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003204 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003205 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003206 SDValue Arg = TailCallArgs[i].Arg;
3207 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003208 int FI = TailCallArgs[i].FrameIdx;
3209 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003210 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003211 MachinePointerInfo::getFixedStack(FI),
3212 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003213 }
3214}
3215
3216/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3217/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003218static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003219 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003220 SDValue Chain,
3221 SDValue OldRetAddr,
3222 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003223 int SPDiff,
3224 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003225 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003226 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003227 if (SPDiff) {
3228 // Calculate the new stack slot for the return address.
3229 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003230 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003231 isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003232 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003233 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003234 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003235 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003236 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003237 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003238 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003239
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003240 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3241 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003242 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003243 int NewFPLoc =
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003244 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene1fbe0542009-11-12 20:49:22 +00003245 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003246 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003247 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3248 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003249 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003250 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003251 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003252 }
3253 return Chain;
3254}
3255
3256/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3257/// the position of the argument.
3258static void
3259CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003260 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003261 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003262 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003263 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003264 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003265 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003266 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003267 TailCallArgumentInfo Info;
3268 Info.Arg = Arg;
3269 Info.FrameIdxOp = FIN;
3270 Info.FrameIdx = FI;
3271 TailCallArguments.push_back(Info);
3272}
3273
3274/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3275/// stack slot. Returns the chain as result and the loaded frame pointers in
3276/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003277SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003278 int SPDiff,
3279 SDValue Chain,
3280 SDValue &LROpOut,
3281 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003282 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003283 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003284 if (SPDiff) {
3285 // Load the LR and FP stack slot for later adjusting.
Owen Anderson9f944592009-08-11 20:47:22 +00003286 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003287 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003288 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003289 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003290 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003291
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003292 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3293 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003294 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003295 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003296 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003297 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003298 Chain = SDValue(FPOpOut.getNode(), 1);
3299 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003300 }
3301 return Chain;
3302}
3303
Dale Johannesen85d41a12008-03-04 23:17:14 +00003304/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003305/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003306/// specified by the specific parameter attribute. The copy will be passed as
3307/// a byval function parameter.
3308/// Sometimes what we are copying is the end of a larger object, the part that
3309/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003310static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003311CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003312 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003313 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003314 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003315 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003316 false, false, MachinePointerInfo(),
3317 MachinePointerInfo());
Dale Johannesen85d41a12008-03-04 23:17:14 +00003318}
Chris Lattner43df5b32007-02-25 05:34:32 +00003319
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003320/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3321/// tail calls.
3322static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003323LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3324 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003325 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003326 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3327 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003328 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003329 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003330 if (!isTailCall) {
3331 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003332 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003333 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003334 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003335 else
Owen Anderson9f944592009-08-11 20:47:22 +00003336 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003337 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003338 DAG.getConstant(ArgOffset, PtrVT));
3339 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003340 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3341 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003342 // Calculate and remember argument location.
3343 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3344 TailCallArguments);
3345}
3346
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003347static
3348void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003349 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003350 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003351 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003352 MachineFunction &MF = DAG.getMachineFunction();
3353
3354 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3355 // might overwrite each other in case of tail call optimization.
3356 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003357 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003358 InFlag = SDValue();
3359 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3360 MemOpChains2, dl);
3361 if (!MemOpChains2.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003362 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003363
3364 // Store the return address to the appropriate stack slot.
3365 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3366 isPPC64, isDarwinABI, dl);
3367
3368 // Emit callseq_end just before tailcall node.
3369 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003370 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003371 InFlag = Chain.getValue(1);
3372}
3373
3374static
3375unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003376 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003377 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3378 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003379 const PPCSubtarget &PPCSubTarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003380
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003381 bool isPPC64 = PPCSubTarget.isPPC64();
3382 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3383
Owen Anderson53aa7a92009-08-10 22:56:29 +00003384 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003385 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003386 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003387
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003388 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003389
Torok Edwin31e90d22010-08-04 20:47:44 +00003390 bool needIndirectCall = true;
3391 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003392 // If this is an absolute destination address, use the munged value.
3393 Callee = SDValue(Dest, 0);
Torok Edwin31e90d22010-08-04 20:47:44 +00003394 needIndirectCall = false;
3395 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003396
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003397 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3398 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3399 // Use indirect calls for ALL functions calls in JIT mode, since the
3400 // far-call stubs may be outside relocation limits for a BL instruction.
3401 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3402 unsigned OpFlags = 0;
3403 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003404 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003405 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003406 (G->getGlobal()->isDeclaration() ||
3407 G->getGlobal()->isWeakForLinker())) {
3408 // PC-relative references to external symbols should go through $stub,
3409 // unless we're building with the leopard linker or later, which
3410 // automatically synthesizes these stubs.
3411 OpFlags = PPCII::MO_DARWIN_STUB;
3412 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003413
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003414 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3415 // every direct call is) turn it into a TargetGlobalAddress /
3416 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin31e90d22010-08-04 20:47:44 +00003417 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003418 Callee.getValueType(),
3419 0, OpFlags);
Torok Edwin31e90d22010-08-04 20:47:44 +00003420 needIndirectCall = false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003421 }
Torok Edwin31e90d22010-08-04 20:47:44 +00003422 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003423
Torok Edwin31e90d22010-08-04 20:47:44 +00003424 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003425 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003426
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003427 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyaaba17e2011-07-24 08:22:56 +00003428 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00003429 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003430 // PC-relative references to external symbols should go through $stub,
3431 // unless we're building with the leopard linker or later, which
3432 // automatically synthesizes these stubs.
3433 OpFlags = PPCII::MO_DARWIN_STUB;
3434 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003435
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003436 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3437 OpFlags);
3438 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003439 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003440
Torok Edwin31e90d22010-08-04 20:47:44 +00003441 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003442 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3443 // to do the call, we can't use PPCISD::CALL.
3444 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003445
3446 if (isSVR4ABI && isPPC64) {
3447 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3448 // entry point, but to the function descriptor (the function entry point
3449 // address is part of the function descriptor though).
3450 // The function descriptor is a three doubleword structure with the
3451 // following fields: function entry point, TOC base address and
3452 // environment pointer.
3453 // Thus for a call through a function pointer, the following actions need
3454 // to be performed:
3455 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003456 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003457 // 2. Load the address of the function entry point from the function
3458 // descriptor.
3459 // 3. Load the TOC of the callee from the function descriptor into r2.
3460 // 4. Load the environment pointer from the function descriptor into
3461 // r11.
3462 // 5. Branch to the function entry point address.
3463 // 6. On return of the callee, the TOC of the caller needs to be
3464 // restored (this is done in FinishCall()).
3465 //
3466 // All those operations are flagged together to ensure that no other
3467 // operations can be scheduled in between. E.g. without flagging the
3468 // operations together, a TOC access in the caller could be scheduled
3469 // between the load of the callee TOC and the branch to the callee, which
3470 // results in the TOC access going through the TOC of the callee instead
3471 // of going through the TOC of the caller, which leads to incorrect code.
3472
3473 // Load the address of the function entry point from the function
3474 // descriptor.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003475 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00003476 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs,
3477 ArrayRef<SDValue>(MTCTROps, InFlag.getNode() ? 3 : 2));
Tilmann Scheller79fef932009-12-18 13:00:15 +00003478 Chain = LoadFuncPtr.getValue(1);
3479 InFlag = LoadFuncPtr.getValue(2);
3480
3481 // Load environment pointer into r11.
3482 // Offset of the environment pointer within the function descriptor.
3483 SDValue PtrOff = DAG.getIntPtrConstant(16);
3484
3485 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3486 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3487 InFlag);
3488 Chain = LoadEnvPtr.getValue(1);
3489 InFlag = LoadEnvPtr.getValue(2);
3490
3491 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3492 InFlag);
3493 Chain = EnvVal.getValue(0);
3494 InFlag = EnvVal.getValue(1);
3495
3496 // Load TOC of the callee into r2. We are using a target-specific load
3497 // with r2 hard coded, because the result of a target-independent load
3498 // would never go directly into r2, since r2 is a reserved register (which
3499 // prevents the register allocator from allocating it), resulting in an
3500 // additional register being allocated and an unnecessary move instruction
3501 // being generated.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003502 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003503 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3504 Callee, InFlag);
3505 Chain = LoadTOCPtr.getValue(0);
3506 InFlag = LoadTOCPtr.getValue(1);
3507
3508 MTCTROps[0] = Chain;
3509 MTCTROps[1] = LoadFuncPtr;
3510 MTCTROps[2] = InFlag;
3511 }
3512
Craig Topper48d114b2014-04-26 18:35:24 +00003513 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys,
3514 ArrayRef<SDValue>(MTCTROps, InFlag.getNode() ? 3 : 2));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003515 InFlag = Chain.getValue(1);
3516
3517 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00003518 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003519 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003520 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003521 CallOpc = PPCISD::BCTRL;
Craig Topper062a2ba2014-04-25 05:30:21 +00003522 Callee.setNode(nullptr);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003523 // Add use of X11 (holding environment pointer)
3524 if (isSVR4ABI && isPPC64)
3525 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003526 // Add CTR register as callee so a bctr can be emitted later.
3527 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00003528 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003529 }
3530
3531 // If this is a direct call, pass the chain and the callee.
3532 if (Callee.getNode()) {
3533 Ops.push_back(Chain);
3534 Ops.push_back(Callee);
3535 }
3536 // If this is a tail call add stack pointer delta.
3537 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00003538 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003539
3540 // Add argument registers to the end of the list so that they are known live
3541 // into the call.
3542 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3543 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3544 RegsToPass[i].second.getValueType()));
3545
3546 return CallOpc;
3547}
3548
Roman Divacky76293062012-09-18 16:47:58 +00003549static
3550bool isLocalCall(const SDValue &Callee)
3551{
3552 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00003553 return !G->getGlobal()->isDeclaration() &&
3554 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00003555 return false;
3556}
3557
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003558SDValue
3559PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003560 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003561 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003562 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003563 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003564
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003565 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003566 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003567 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003568 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003569
3570 // Copy all of the result registers out of their specified physreg.
3571 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3572 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003573 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00003574
3575 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3576 VA.getLocReg(), VA.getLocVT(), InFlag);
3577 Chain = Val.getValue(1);
3578 InFlag = Val.getValue(2);
3579
3580 switch (VA.getLocInfo()) {
3581 default: llvm_unreachable("Unknown loc info!");
3582 case CCValAssign::Full: break;
3583 case CCValAssign::AExt:
3584 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3585 break;
3586 case CCValAssign::ZExt:
3587 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3588 DAG.getValueType(VA.getValVT()));
3589 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3590 break;
3591 case CCValAssign::SExt:
3592 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3593 DAG.getValueType(VA.getValVT()));
3594 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3595 break;
3596 }
3597
3598 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003599 }
3600
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003601 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003602}
3603
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003604SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00003605PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003606 bool isTailCall, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003607 SelectionDAG &DAG,
3608 SmallVector<std::pair<unsigned, SDValue>, 8>
3609 &RegsToPass,
3610 SDValue InFlag, SDValue Chain,
3611 SDValue &Callee,
3612 int SPDiff, unsigned NumBytes,
3613 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003614 SmallVectorImpl<SDValue> &InVals) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003615 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003616 SmallVector<SDValue, 8> Ops;
3617 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3618 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003619 PPCSubTarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003620
Hal Finkel5ab37802012-08-28 02:10:27 +00003621 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3622 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3623 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3624
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003625 // When performing tail call optimization the callee pops its arguments off
3626 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00003627 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003628 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003629 (CallConv == CallingConv::Fast &&
3630 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003631
Roman Divackyef21be22012-03-06 16:41:49 +00003632 // Add a register mask operand representing the call-preserved registers.
3633 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3634 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3635 assert(Mask && "Missing call preserved mask for calling convention");
3636 Ops.push_back(DAG.getRegisterMask(Mask));
3637
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003638 if (InFlag.getNode())
3639 Ops.push_back(InFlag);
3640
3641 // Emit tail call.
3642 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003643 assert(((Callee.getOpcode() == ISD::Register &&
3644 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3645 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3646 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3647 isa<ConstantSDNode>(Callee)) &&
3648 "Expecting an global address, external symbol, absolute value or register");
3649
Craig Topper48d114b2014-04-26 18:35:24 +00003650 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003651 }
3652
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003653 // Add a NOP immediately after the branch instruction when using the 64-bit
3654 // SVR4 ABI. At link time, if caller and callee are in a different module and
3655 // thus have a different TOC, the call will be replaced with a call to a stub
3656 // function which saves the current TOC, loads the TOC of the callee and
3657 // branches to the callee. The NOP will be replaced with a load instruction
3658 // which restores the TOC of the caller from the TOC save slot of the current
3659 // stack frame. If caller and callee belong to the same module (and have the
3660 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00003661
3662 bool needsTOCRestore = false;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003663 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003664 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003665 // This is a call through a function pointer.
3666 // Restore the caller TOC from the save area into R2.
3667 // See PrepareCall() for more information about calls through function
3668 // pointers in the 64-bit SVR4 ABI.
3669 // We are using a target-specific load with r2 hard coded, because the
3670 // result of a target-independent load would never go directly into r2,
3671 // since r2 is a reserved register (which prevents the register allocator
3672 // from allocating it), resulting in an additional register being
3673 // allocated and an unnecessary move instruction being generated.
Hal Finkel51861b42012-03-31 14:45:15 +00003674 needsTOCRestore = true;
Bill Schmidtcea15962013-09-26 17:09:28 +00003675 } else if ((CallOpc == PPCISD::CALL) &&
3676 (!isLocalCall(Callee) ||
3677 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Roman Divacky76293062012-09-18 16:47:58 +00003678 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003679 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller79fef932009-12-18 13:00:15 +00003680 }
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003681 }
3682
Craig Topper48d114b2014-04-26 18:35:24 +00003683 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Hal Finkel51861b42012-03-31 14:45:15 +00003684 InFlag = Chain.getValue(1);
3685
3686 if (needsTOCRestore) {
3687 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3688 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3689 InFlag = Chain.getValue(1);
3690 }
3691
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003692 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3693 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003694 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003695 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003696 InFlag = Chain.getValue(1);
3697
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003698 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3699 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003700}
3701
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003702SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00003703PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003704 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00003705 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00003706 SDLoc &dl = CLI.DL;
3707 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3708 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3709 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00003710 SDValue Chain = CLI.Chain;
3711 SDValue Callee = CLI.Callee;
3712 bool &isTailCall = CLI.IsTailCall;
3713 CallingConv::ID CallConv = CLI.CallConv;
3714 bool isVarArg = CLI.IsVarArg;
3715
Evan Cheng67a69dd2010-01-27 00:07:07 +00003716 if (isTailCall)
3717 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3718 Ins, DAG);
3719
Reid Kleckner5772b772014-04-24 20:14:34 +00003720 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
3721 report_fatal_error("failed to perform tail call elimination on a call "
3722 "site marked musttail");
3723
Bill Schmidt57d6de52012-10-23 15:51:16 +00003724 if (PPCSubTarget.isSVR4ABI()) {
3725 if (PPCSubTarget.isPPC64())
3726 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3727 isTailCall, Outs, OutVals, Ins,
3728 dl, DAG, InVals);
3729 else
3730 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3731 isTailCall, Outs, OutVals, Ins,
3732 dl, DAG, InVals);
3733 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003734
Bill Schmidt57d6de52012-10-23 15:51:16 +00003735 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3736 isTailCall, Outs, OutVals, Ins,
3737 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003738}
3739
3740SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003741PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3742 CallingConv::ID CallConv, bool isVarArg,
3743 bool isTailCall,
3744 const SmallVectorImpl<ISD::OutputArg> &Outs,
3745 const SmallVectorImpl<SDValue> &OutVals,
3746 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003747 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003748 SmallVectorImpl<SDValue> &InVals) const {
3749 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003750 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003751
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003752 assert((CallConv == CallingConv::C ||
3753 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003754
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003755 unsigned PtrByteSize = 4;
3756
3757 MachineFunction &MF = DAG.getMachineFunction();
3758
3759 // Mark this function as potentially containing a function that contains a
3760 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3761 // and restoring the callers stack pointer in this functions epilog. This is
3762 // done because by tail calling the called function might overwrite the value
3763 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003764 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3765 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003766 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00003767
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003768 // Count how many bytes are to be pushed on the stack, including the linkage
3769 // area, parameter list area and the part of the local variable space which
3770 // contains copies of aggregates which are passed by value.
3771
3772 // Assign locations to all of the outgoing arguments.
3773 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003774 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003775 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003776
3777 // Reserve space for the linkage area on the stack.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003778 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003779
3780 if (isVarArg) {
3781 // Handle fixed and variable vector arguments differently.
3782 // Fixed vector arguments go into registers as long as registers are
3783 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003784 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00003785
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003786 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003787 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003788 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003789 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00003790
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003791 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00003792 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3793 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003794 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00003795 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3796 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003797 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003798
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003799 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003800#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00003801 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00003802 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003803#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00003804 llvm_unreachable(0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003805 }
3806 }
3807 } else {
3808 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00003809 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003810 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003811
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003812 // Assign locations to all of the outgoing aggregate by value arguments.
3813 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003814 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003815 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003816
3817 // Reserve stack space for the allocations in CCInfo.
3818 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3819
Bill Schmidtef17c142013-02-06 17:33:58 +00003820 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003821
3822 // Size of the linkage area, parameter list area and the part of the local
3823 // space variable where copies of aggregates which are passed by value are
3824 // stored.
3825 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003826
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003827 // Calculate by how many bytes the stack has to be adjusted in case of tail
3828 // call optimization.
3829 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3830
3831 // Adjust the stack pointer for the new arguments...
3832 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00003833 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
3834 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003835 SDValue CallSeqStart = Chain;
3836
3837 // Load the return address and frame pointer so it can be moved somewhere else
3838 // later.
3839 SDValue LROp, FPOp;
3840 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3841 dl);
3842
3843 // Set up a copy of the stack pointer for use loading and storing any
3844 // arguments that may not fit in the registers available for argument
3845 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00003846 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00003847
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003848 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3849 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3850 SmallVector<SDValue, 8> MemOpChains;
3851
Roman Divacky71038e72011-08-30 17:04:16 +00003852 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003853 // Walk the register/memloc assignments, inserting copies/loads.
3854 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3855 i != e;
3856 ++i) {
3857 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003858 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003859 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00003860
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003861 if (Flags.isByVal()) {
3862 // Argument is an aggregate which is passed by value, thus we need to
3863 // create a copy of it in the local variable space of the current stack
3864 // frame (which is the stack frame of the caller) and pass the address of
3865 // this copy to the callee.
3866 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3867 CCValAssign &ByValVA = ByValArgLocs[j++];
3868 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00003869
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003870 // Memory reserved in the local variable space of the callers stack frame.
3871 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00003872
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003873 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3874 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00003875
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003876 // Create a copy of the argument in the local area of the current
3877 // stack frame.
3878 SDValue MemcpyCall =
3879 CreateCopyOfByValArgument(Arg, PtrOff,
3880 CallSeqStart.getNode()->getOperand(0),
3881 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00003882
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003883 // This must go outside the CALLSEQ_START..END.
3884 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003885 CallSeqStart.getNode()->getOperand(1),
3886 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003887 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3888 NewCallSeqStart.getNode());
3889 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00003890
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003891 // Pass the address of the aggregate copy on the stack either in a
3892 // physical register or in the parameter list area of the current stack
3893 // frame to the callee.
3894 Arg = PtrOff;
3895 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003896
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003897 if (VA.isRegLoc()) {
Hal Finkel2a9d3182014-03-06 00:23:33 +00003898 if (Arg.getValueType() == MVT::i1)
3899 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg);
3900
Roman Divacky71038e72011-08-30 17:04:16 +00003901 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003902 // Put argument in a physical register.
3903 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3904 } else {
3905 // Put argument in the parameter list area of the current stack frame.
3906 assert(VA.isMemLoc());
3907 unsigned LocMemOffset = VA.getLocMemOffset();
3908
3909 if (!isTailCall) {
3910 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3911 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3912
3913 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00003914 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00003915 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003916 } else {
3917 // Calculate and remember argument location.
3918 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3919 TailCallArguments);
3920 }
3921 }
3922 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003923
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003924 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003925 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Wesley Peck527da1b2010-11-23 03:31:01 +00003926
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003927 // Build a sequence of copy-to-reg nodes chained together with token chain
3928 // and flag operands which copy the outgoing args into the appropriate regs.
3929 SDValue InFlag;
3930 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3931 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3932 RegsToPass[i].second, InFlag);
3933 InFlag = Chain.getValue(1);
3934 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003935
Hal Finkel5ab37802012-08-28 02:10:27 +00003936 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3937 // registers.
3938 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003939 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3940 SDValue Ops[] = { Chain, InFlag };
3941
Hal Finkel5ab37802012-08-28 02:10:27 +00003942 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
Craig Topper48d114b2014-04-26 18:35:24 +00003943 dl, VTs,
3944 ArrayRef<SDValue>(Ops, InFlag.getNode() ? 2 : 1));
NAKAMURA Takumiac490292012-08-30 15:52:29 +00003945
Hal Finkel5ab37802012-08-28 02:10:27 +00003946 InFlag = Chain.getValue(1);
3947 }
3948
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003949 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003950 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3951 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003952
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003953 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3954 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3955 Ins, InVals);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003956}
3957
Bill Schmidt57d6de52012-10-23 15:51:16 +00003958// Copy an argument into memory, being careful to do this outside the
3959// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003960SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00003961PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3962 SDValue CallSeqStart,
3963 ISD::ArgFlagsTy Flags,
3964 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003965 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00003966 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3967 CallSeqStart.getNode()->getOperand(0),
3968 Flags, DAG, dl);
3969 // The MEMCPY must go outside the CALLSEQ_START..END.
3970 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00003971 CallSeqStart.getNode()->getOperand(1),
3972 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00003973 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3974 NewCallSeqStart.getNode());
3975 return NewCallSeqStart;
3976}
3977
3978SDValue
3979PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003980 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003981 bool isTailCall,
3982 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003983 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003984 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003985 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003986 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003987
Bill Schmidt57d6de52012-10-23 15:51:16 +00003988 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003989
Bill Schmidt57d6de52012-10-23 15:51:16 +00003990 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3991 unsigned PtrByteSize = 8;
3992
3993 MachineFunction &MF = DAG.getMachineFunction();
3994
3995 // Mark this function as potentially containing a function that contains a
3996 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3997 // and restoring the callers stack pointer in this functions epilog. This is
3998 // done because by tail calling the called function might overwrite the value
3999 // in this function's (MF) stack pointer stack slot 0(SP).
4000 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4001 CallConv == CallingConv::Fast)
4002 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4003
4004 unsigned nAltivecParamsAtEnd = 0;
4005
4006 // Count how many bytes are to be pushed on the stack, including the linkage
4007 // area, and parameter passing area. We start with at least 48 bytes, which
4008 // is reserved space for [SP][CR][LR][3 x unused].
4009 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
4010 // of this call.
4011 unsigned NumBytes =
4012 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
4013 Outs, OutVals, nAltivecParamsAtEnd);
4014
4015 // Calculate by how many bytes the stack has to be adjusted in case of tail
4016 // call optimization.
4017 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4018
4019 // To protect arguments on the stack from being clobbered in a tail call,
4020 // force all the loads to happen before doing any other lowering.
4021 if (isTailCall)
4022 Chain = DAG.getStackArgumentTokenFactor(Chain);
4023
4024 // Adjust the stack pointer for the new arguments...
4025 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004026 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4027 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004028 SDValue CallSeqStart = Chain;
4029
4030 // Load the return address and frame pointer so it can be move somewhere else
4031 // later.
4032 SDValue LROp, FPOp;
4033 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4034 dl);
4035
4036 // Set up a copy of the stack pointer for use loading and storing any
4037 // arguments that may not fit in the registers available for argument
4038 // passing.
4039 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
4040
4041 // Figure out which arguments are going to go in registers, and which in
4042 // memory. Also, if this is a vararg function, floating point operations
4043 // must be stored to our stack, and loaded into integer regs as well, if
4044 // any integer regs are available for argument passing.
4045 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
4046 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
4047
Craig Topper840beec2014-04-04 05:16:06 +00004048 static const MCPhysReg GPR[] = {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004049 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4050 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4051 };
Craig Topper840beec2014-04-04 05:16:06 +00004052 static const MCPhysReg *FPR = GetFPR();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004053
Craig Topper840beec2014-04-04 05:16:06 +00004054 static const MCPhysReg VR[] = {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004055 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4056 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4057 };
Craig Topper840beec2014-04-04 05:16:06 +00004058 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00004059 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
4060 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
4061 };
4062
Bill Schmidt57d6de52012-10-23 15:51:16 +00004063 const unsigned NumGPRs = array_lengthof(GPR);
4064 const unsigned NumFPRs = 13;
4065 const unsigned NumVRs = array_lengthof(VR);
4066
4067 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4068 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4069
4070 SmallVector<SDValue, 8> MemOpChains;
4071 for (unsigned i = 0; i != NumOps; ++i) {
4072 SDValue Arg = OutVals[i];
4073 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4074
4075 // PtrOff will be used to store the current argument to the stack if a
4076 // register cannot be found for it.
4077 SDValue PtrOff;
4078
4079 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
4080
4081 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4082
4083 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00004084 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004085 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4086 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4087 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
4088 }
4089
4090 // FIXME memcpy is used way more than necessary. Correctness first.
4091 // Note: "by value" is code for passing a structure by value, not
4092 // basic types.
4093 if (Flags.isByVal()) {
4094 // Note: Size includes alignment padding, so
4095 // struct x { short a; char b; }
4096 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
4097 // These are the proper values we need for right-justifying the
4098 // aggregate in a parameter register.
4099 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00004100
4101 // An empty aggregate parameter takes up no storage and no
4102 // registers.
4103 if (Size == 0)
4104 continue;
4105
Hal Finkel262a2242013-09-12 23:20:06 +00004106 unsigned BVAlign = Flags.getByValAlign();
4107 if (BVAlign > 8) {
4108 if (BVAlign % PtrByteSize != 0)
4109 llvm_unreachable(
4110 "ByVal alignment is not a multiple of the pointer size");
4111
4112 ArgOffset = ((ArgOffset+BVAlign-1)/BVAlign)*BVAlign;
4113 }
4114
Bill Schmidt57d6de52012-10-23 15:51:16 +00004115 // All aggregates smaller than 8 bytes must be passed right-justified.
4116 if (Size==1 || Size==2 || Size==4) {
4117 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
4118 if (GPR_idx != NumGPRs) {
4119 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
4120 MachinePointerInfo(), VT,
4121 false, false, 0);
4122 MemOpChains.push_back(Load.getValue(1));
4123 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4124
4125 ArgOffset += PtrByteSize;
4126 continue;
4127 }
4128 }
4129
4130 if (GPR_idx == NumGPRs && Size < 8) {
4131 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4132 PtrOff.getValueType());
4133 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4134 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4135 CallSeqStart,
4136 Flags, DAG, dl);
4137 ArgOffset += PtrByteSize;
4138 continue;
4139 }
4140 // Copy entire object into memory. There are cases where gcc-generated
4141 // code assumes it is there, even if it could be put entirely into
4142 // registers. (This is not what the doc says.)
4143
4144 // FIXME: The above statement is likely due to a misunderstanding of the
4145 // documents. All arguments must be copied into the parameter area BY
4146 // THE CALLEE in the event that the callee takes the address of any
4147 // formal argument. That has not yet been implemented. However, it is
4148 // reasonable to use the stack area as a staging area for the register
4149 // load.
4150
4151 // Skip this for small aggregates, as we will use the same slot for a
4152 // right-justified copy, below.
4153 if (Size >= 8)
4154 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4155 CallSeqStart,
4156 Flags, DAG, dl);
4157
4158 // When a register is available, pass a small aggregate right-justified.
4159 if (Size < 8 && GPR_idx != NumGPRs) {
4160 // The easiest way to get this right-justified in a register
4161 // is to copy the structure into the rightmost portion of a
4162 // local variable slot, then load the whole slot into the
4163 // register.
4164 // FIXME: The memcpy seems to produce pretty awful code for
4165 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00004166 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00004167 // parameter save area instead of a new local variable.
4168 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
4169 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4170 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4171 CallSeqStart,
4172 Flags, DAG, dl);
4173
4174 // Load the slot into the register.
4175 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
4176 MachinePointerInfo(),
4177 false, false, false, 0);
4178 MemOpChains.push_back(Load.getValue(1));
4179 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4180
4181 // Done with this argument.
4182 ArgOffset += PtrByteSize;
4183 continue;
4184 }
4185
4186 // For aggregates larger than PtrByteSize, copy the pieces of the
4187 // object that fit into registers from the parameter save area.
4188 for (unsigned j=0; j<Size; j+=PtrByteSize) {
4189 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
4190 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
4191 if (GPR_idx != NumGPRs) {
4192 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4193 MachinePointerInfo(),
4194 false, false, false, 0);
4195 MemOpChains.push_back(Load.getValue(1));
4196 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4197 ArgOffset += PtrByteSize;
4198 } else {
4199 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4200 break;
4201 }
4202 }
4203 continue;
4204 }
4205
Craig Topper56710102013-08-15 02:33:50 +00004206 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004207 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00004208 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004209 case MVT::i32:
4210 case MVT::i64:
4211 if (GPR_idx != NumGPRs) {
4212 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
4213 } else {
4214 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4215 true, isTailCall, false, MemOpChains,
4216 TailCallArguments, dl);
4217 }
4218 ArgOffset += PtrByteSize;
4219 break;
4220 case MVT::f32:
4221 case MVT::f64:
4222 if (FPR_idx != NumFPRs) {
4223 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4224
4225 if (isVarArg) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004226 // A single float or an aggregate containing only a single float
4227 // must be passed right-justified in the stack doubleword, and
4228 // in the GPR, if one is available.
4229 SDValue StoreOff;
Craig Topper56710102013-08-15 02:33:50 +00004230 if (Arg.getSimpleValueType().SimpleTy == MVT::f32) {
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004231 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4232 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4233 } else
4234 StoreOff = PtrOff;
4235
4236 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004237 MachinePointerInfo(), false, false, 0);
4238 MemOpChains.push_back(Store);
4239
4240 // Float varargs are always shadowed in available integer registers
4241 if (GPR_idx != NumGPRs) {
4242 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4243 MachinePointerInfo(), false, false,
4244 false, 0);
4245 MemOpChains.push_back(Load.getValue(1));
4246 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4247 }
4248 } else if (GPR_idx != NumGPRs)
4249 // If we have any FPRs remaining, we may also have GPRs remaining.
4250 ++GPR_idx;
4251 } else {
4252 // Single-precision floating-point values are mapped to the
4253 // second (rightmost) word of the stack doubleword.
4254 if (Arg.getValueType() == MVT::f32) {
4255 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4256 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4257 }
4258
4259 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4260 true, isTailCall, false, MemOpChains,
4261 TailCallArguments, dl);
4262 }
4263 ArgOffset += 8;
4264 break;
4265 case MVT::v4f32:
4266 case MVT::v4i32:
4267 case MVT::v8i16:
4268 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00004269 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00004270 case MVT::v2i64:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004271 if (isVarArg) {
4272 // These go aligned on the stack, or in the corresponding R registers
4273 // when within range. The Darwin PPC ABI doc claims they also go in
4274 // V registers; in fact gcc does this only for arguments that are
4275 // prototyped, not for those that match the ... We do it for all
4276 // arguments, seems to work.
4277 while (ArgOffset % 16 !=0) {
4278 ArgOffset += PtrByteSize;
4279 if (GPR_idx != NumGPRs)
4280 GPR_idx++;
4281 }
4282 // We could elide this store in the case where the object fits
4283 // entirely in R registers. Maybe later.
4284 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
4285 DAG.getConstant(ArgOffset, PtrVT));
4286 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4287 MachinePointerInfo(), false, false, 0);
4288 MemOpChains.push_back(Store);
4289 if (VR_idx != NumVRs) {
4290 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4291 MachinePointerInfo(),
4292 false, false, false, 0);
4293 MemOpChains.push_back(Load.getValue(1));
Hal Finkel7811c612014-03-28 19:58:11 +00004294
4295 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4296 Arg.getSimpleValueType() == MVT::v2i64) ?
4297 VSRH[VR_idx] : VR[VR_idx];
4298 ++VR_idx;
4299
4300 RegsToPass.push_back(std::make_pair(VReg, Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004301 }
4302 ArgOffset += 16;
4303 for (unsigned i=0; i<16; i+=PtrByteSize) {
4304 if (GPR_idx == NumGPRs)
4305 break;
4306 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4307 DAG.getConstant(i, PtrVT));
4308 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4309 false, false, false, 0);
4310 MemOpChains.push_back(Load.getValue(1));
4311 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4312 }
4313 break;
4314 }
4315
4316 // Non-varargs Altivec params generally go in registers, but have
4317 // stack space allocated at the end.
4318 if (VR_idx != NumVRs) {
4319 // Doesn't have GPR space allocated.
Hal Finkel7811c612014-03-28 19:58:11 +00004320 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4321 Arg.getSimpleValueType() == MVT::v2i64) ?
4322 VSRH[VR_idx] : VR[VR_idx];
4323 ++VR_idx;
4324
4325 RegsToPass.push_back(std::make_pair(VReg, Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004326 } else {
4327 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4328 true, isTailCall, true, MemOpChains,
4329 TailCallArguments, dl);
4330 ArgOffset += 16;
4331 }
4332 break;
4333 }
4334 }
4335
4336 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004337 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004338
4339 // Check if this is an indirect call (MTCTR/BCTRL).
4340 // See PrepareCall() for more information about calls through function
4341 // pointers in the 64-bit SVR4 ABI.
4342 if (!isTailCall &&
4343 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4344 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4345 !isBLACompatibleAddress(Callee, DAG)) {
4346 // Load r2 into a virtual register and store it to the TOC save area.
4347 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4348 // TOC save area offset.
4349 SDValue PtrOff = DAG.getIntPtrConstant(40);
4350 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4351 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4352 false, false, 0);
4353 // R12 must contain the address of an indirect callee. This does not
4354 // mean the MTCTR instruction must use R12; it's easier to model this
4355 // as an extra parameter, so do that.
4356 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4357 }
4358
4359 // Build a sequence of copy-to-reg nodes chained together with token chain
4360 // and flag operands which copy the outgoing args into the appropriate regs.
4361 SDValue InFlag;
4362 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4363 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4364 RegsToPass[i].second, InFlag);
4365 InFlag = Chain.getValue(1);
4366 }
4367
4368 if (isTailCall)
4369 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4370 FPOp, true, TailCallArguments);
4371
4372 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4373 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4374 Ins, InVals);
4375}
4376
4377SDValue
4378PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4379 CallingConv::ID CallConv, bool isVarArg,
4380 bool isTailCall,
4381 const SmallVectorImpl<ISD::OutputArg> &Outs,
4382 const SmallVectorImpl<SDValue> &OutVals,
4383 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004384 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004385 SmallVectorImpl<SDValue> &InVals) const {
4386
4387 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004388
Owen Anderson53aa7a92009-08-10 22:56:29 +00004389 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00004390 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004391 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004392
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004393 MachineFunction &MF = DAG.getMachineFunction();
4394
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004395 // Mark this function as potentially containing a function that contains a
4396 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4397 // and restoring the callers stack pointer in this functions epilog. This is
4398 // done because by tail calling the called function might overwrite the value
4399 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004400 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4401 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004402 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4403
4404 unsigned nAltivecParamsAtEnd = 0;
4405
Chris Lattneraa40ec12006-05-16 22:56:08 +00004406 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00004407 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00004408 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004409 unsigned NumBytes =
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004410 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004411 Outs, OutVals,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004412 nAltivecParamsAtEnd);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004413
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004414 // Calculate by how many bytes the stack has to be adjusted in case of tail
4415 // call optimization.
4416 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004417
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004418 // To protect arguments on the stack from being clobbered in a tail call,
4419 // force all the loads to happen before doing any other lowering.
4420 if (isTailCall)
4421 Chain = DAG.getStackArgumentTokenFactor(Chain);
4422
Chris Lattnerb7552a82006-05-17 00:15:40 +00004423 // Adjust the stack pointer for the new arguments...
4424 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004425 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4426 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004427 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004428
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004429 // Load the return address and frame pointer so it can be move somewhere else
4430 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004431 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004432 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4433 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004434
Chris Lattnerb7552a82006-05-17 00:15:40 +00004435 // Set up a copy of the stack pointer for use loading and storing any
4436 // arguments that may not fit in the registers available for argument
4437 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004438 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004439 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004440 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004441 else
Owen Anderson9f944592009-08-11 20:47:22 +00004442 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004443
Chris Lattnerb7552a82006-05-17 00:15:40 +00004444 // Figure out which arguments are going to go in registers, and which in
4445 // memory. Also, if this is a vararg function, floating point operations
4446 // must be stored to our stack, and loaded into integer regs as well, if
4447 // any integer regs are available for argument passing.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004448 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004449 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004450
Craig Topper840beec2014-04-04 05:16:06 +00004451 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004452 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4453 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4454 };
Craig Topper840beec2014-04-04 05:16:06 +00004455 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00004456 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4457 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4458 };
Craig Topper840beec2014-04-04 05:16:06 +00004459 static const MCPhysReg *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004460
Craig Topper840beec2014-04-04 05:16:06 +00004461 static const MCPhysReg VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004462 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4463 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4464 };
Owen Andersone2f23a32007-09-07 04:06:50 +00004465 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004466 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00004467 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004468
Craig Topper840beec2014-04-04 05:16:06 +00004469 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004470
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004471 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004472 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4473
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004474 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00004475 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004476 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004477 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004478
Chris Lattnerb7552a82006-05-17 00:15:40 +00004479 // PtrOff will be used to store the current argument to the stack if a
4480 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004481 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004482
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004483 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004484
Dale Johannesen679073b2009-02-04 02:34:38 +00004485 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004486
4487 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00004488 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00004489 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4490 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00004491 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004492 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00004493
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004494 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004495 // Note: "by value" is code for passing a structure by value, not
4496 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00004497 if (Flags.isByVal()) {
4498 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004499 // Very small objects are passed right-justified. Everything else is
4500 // passed left-justified.
4501 if (Size==1 || Size==2) {
4502 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004503 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00004504 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00004505 MachinePointerInfo(), VT,
4506 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004507 MemOpChains.push_back(Load.getValue(1));
4508 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004509
4510 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004511 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00004512 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4513 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004514 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004515 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4516 CallSeqStart,
4517 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004518 ArgOffset += PtrByteSize;
4519 }
4520 continue;
4521 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004522 // Copy entire object into memory. There are cases where gcc-generated
4523 // code assumes it is there, even if it could be put entirely into
4524 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004525 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4526 CallSeqStart,
4527 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004528
4529 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4530 // copy the pieces of the object that fit into registers from the
4531 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00004532 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004533 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004534 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00004535 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004536 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4537 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004538 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00004539 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00004540 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004541 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004542 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004543 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004544 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004545 }
4546 }
4547 continue;
4548 }
4549
Craig Topper56710102013-08-15 02:33:50 +00004550 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004551 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00004552 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00004553 case MVT::i32:
4554 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004555 if (GPR_idx != NumGPRs) {
Hal Finkel7f908e82014-03-06 00:45:19 +00004556 if (Arg.getValueType() == MVT::i1)
4557 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg);
4558
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004559 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004560 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004561 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4562 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004563 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00004564 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004565 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004566 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004567 case MVT::f32:
4568 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004569 if (FPR_idx != NumFPRs) {
4570 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4571
Chris Lattnerb7552a82006-05-17 00:15:40 +00004572 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00004573 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4574 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004575 MemOpChains.push_back(Store);
4576
Chris Lattnerb7552a82006-05-17 00:15:40 +00004577 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004578 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004579 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004580 MachinePointerInfo(), false, false,
4581 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004582 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004583 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004584 }
Owen Anderson9f944592009-08-11 20:47:22 +00004585 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004586 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004587 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00004588 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4589 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004590 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004591 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004592 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00004593 }
4594 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00004595 // If we have any FPRs remaining, we may also have GPRs remaining.
4596 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4597 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004598 if (GPR_idx != NumGPRs)
4599 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00004600 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004601 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4602 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004603 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004604 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004605 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4606 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004607 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004608 if (isPPC64)
4609 ArgOffset += 8;
4610 else
Owen Anderson9f944592009-08-11 20:47:22 +00004611 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004612 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004613 case MVT::v4f32:
4614 case MVT::v4i32:
4615 case MVT::v8i16:
4616 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00004617 if (isVarArg) {
4618 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00004619 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00004620 // V registers; in fact gcc does this only for arguments that are
4621 // prototyped, not for those that match the ... We do it for all
4622 // arguments, seems to work.
4623 while (ArgOffset % 16 !=0) {
4624 ArgOffset += PtrByteSize;
4625 if (GPR_idx != NumGPRs)
4626 GPR_idx++;
4627 }
4628 // We could elide this store in the case where the object fits
4629 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004630 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004631 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00004632 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4633 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004634 MemOpChains.push_back(Store);
4635 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004636 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00004637 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004638 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004639 MemOpChains.push_back(Load.getValue(1));
4640 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4641 }
4642 ArgOffset += 16;
4643 for (unsigned i=0; i<16; i+=PtrByteSize) {
4644 if (GPR_idx == NumGPRs)
4645 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00004646 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004647 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00004648 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004649 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004650 MemOpChains.push_back(Load.getValue(1));
4651 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4652 }
4653 break;
4654 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004655
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004656 // Non-varargs Altivec params generally go in registers, but have
4657 // stack space allocated at the end.
4658 if (VR_idx != NumVRs) {
4659 // Doesn't have GPR space allocated.
4660 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4661 } else if (nAltivecParamsAtEnd==0) {
4662 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004663 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4664 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004665 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004666 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00004667 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00004668 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004669 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00004670 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004671 // If all Altivec parameters fit in registers, as they usually do,
4672 // they get stack space following the non-Altivec parameters. We
4673 // don't track this here because nobody below needs it.
4674 // If there are more Altivec parameters than fit in registers emit
4675 // the stores here.
4676 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4677 unsigned j = 0;
4678 // Offset is aligned; skip 1st 12 params which go in V registers.
4679 ArgOffset = ((ArgOffset+15)/16)*16;
4680 ArgOffset += 12*16;
4681 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004682 SDValue Arg = OutVals[i];
4683 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00004684 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4685 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004686 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004687 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004688 // We are emitting Altivec params in order.
4689 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4690 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004691 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004692 ArgOffset += 16;
4693 }
4694 }
4695 }
4696 }
4697
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004698 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004699 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004700
Dale Johannesen90eab672010-03-09 20:15:42 +00004701 // On Darwin, R12 must contain the address of an indirect callee. This does
4702 // not mean the MTCTR instruction must use R12; it's easier to model this as
4703 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004704 if (!isTailCall &&
Dale Johannesen90eab672010-03-09 20:15:42 +00004705 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4706 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4707 !isBLACompatibleAddress(Callee, DAG))
4708 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4709 PPC::R12), Callee));
4710
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004711 // Build a sequence of copy-to-reg nodes chained together with token chain
4712 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004713 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004714 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00004715 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00004716 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004717 InFlag = Chain.getValue(1);
4718 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004719
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004720 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004721 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4722 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004723
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004724 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4725 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4726 Ins, InVals);
Chris Lattneraa40ec12006-05-16 22:56:08 +00004727}
4728
Hal Finkel450128a2011-10-14 19:51:36 +00004729bool
4730PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4731 MachineFunction &MF, bool isVarArg,
4732 const SmallVectorImpl<ISD::OutputArg> &Outs,
4733 LLVMContext &Context) const {
4734 SmallVector<CCValAssign, 16> RVLocs;
4735 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4736 RVLocs, Context);
4737 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4738}
4739
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004740SDValue
4741PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004742 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004743 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004744 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004745 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004746
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004747 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00004748 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00004749 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004750 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004751
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004752 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004753 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004754
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004755 // Copy the result values into the output registers.
4756 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4757 CCValAssign &VA = RVLocs[i];
4758 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00004759
4760 SDValue Arg = OutVals[i];
4761
4762 switch (VA.getLocInfo()) {
4763 default: llvm_unreachable("Unknown loc info!");
4764 case CCValAssign::Full: break;
4765 case CCValAssign::AExt:
4766 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4767 break;
4768 case CCValAssign::ZExt:
4769 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4770 break;
4771 case CCValAssign::SExt:
4772 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4773 break;
4774 }
4775
4776 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004777 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004778 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00004779 }
4780
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004781 RetOps[0] = Chain; // Update chain.
4782
4783 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00004784 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00004785 RetOps.push_back(Flag);
4786
Craig Topper48d114b2014-04-26 18:35:24 +00004787 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps);
Chris Lattner4211ca92006-04-14 06:01:58 +00004788}
4789
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004790SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004791 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00004792 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004793 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004794
Jim Laskeye4f4d042006-12-04 22:04:42 +00004795 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004796 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00004797
4798 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00004799 bool isPPC64 = Subtarget.isPPC64();
4800 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004801 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004802
4803 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004804 SDValue Chain = Op.getOperand(0);
4805 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004806
Jim Laskeye4f4d042006-12-04 22:04:42 +00004807 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00004808 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4809 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004810 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004811
Jim Laskeye4f4d042006-12-04 22:04:42 +00004812 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00004813 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004814
Jim Laskeye4f4d042006-12-04 22:04:42 +00004815 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00004816 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004817 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00004818}
4819
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004820
4821
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004822SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004823PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004824 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004825 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004826 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004827 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004828
4829 // Get current frame pointer save index. The users of this index will be
4830 // primarily DYNALLOC instructions.
4831 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4832 int RASI = FI->getReturnAddrSaveIndex();
4833
4834 // If the frame pointer save index hasn't been defined yet.
4835 if (!RASI) {
4836 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004837 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004838 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004839 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004840 // Save the result.
4841 FI->setReturnAddrSaveIndex(RASI);
4842 }
4843 return DAG.getFrameIndex(RASI, PtrVT);
4844}
4845
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004846SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004847PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4848 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesen86dcae12009-11-24 01:09:07 +00004849 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004850 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004851 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004852
4853 // Get current frame pointer save index. The users of this index will be
4854 // primarily DYNALLOC instructions.
4855 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4856 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004857
Jim Laskey48850c12006-11-16 22:43:37 +00004858 // If the frame pointer save index hasn't been defined yet.
4859 if (!FPSI) {
4860 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00004861 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004862 isDarwinABI);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004863
Jim Laskey48850c12006-11-16 22:43:37 +00004864 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00004865 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00004866 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004867 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00004868 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004869 return DAG.getFrameIndex(FPSI, PtrVT);
4870}
Jim Laskey48850c12006-11-16 22:43:37 +00004871
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004872SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004873 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004874 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00004875 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004876 SDValue Chain = Op.getOperand(0);
4877 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004878 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004879
Jim Laskey48850c12006-11-16 22:43:37 +00004880 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00004881 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00004882 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00004883 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00004884 DAG.getConstant(0, PtrVT), Size);
4885 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004886 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00004887 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004888 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00004889 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Craig Topper48d114b2014-04-26 18:35:24 +00004890 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops);
Jim Laskey48850c12006-11-16 22:43:37 +00004891}
4892
Hal Finkel756810f2013-03-21 21:37:52 +00004893SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
4894 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004895 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004896 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
4897 DAG.getVTList(MVT::i32, MVT::Other),
4898 Op.getOperand(0), Op.getOperand(1));
4899}
4900
4901SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
4902 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004903 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00004904 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
4905 Op.getOperand(0), Op.getOperand(1));
4906}
4907
Hal Finkel940ab932014-02-28 00:27:01 +00004908SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
4909 assert(Op.getValueType() == MVT::i1 &&
4910 "Custom lowering only for i1 loads");
4911
4912 // First, load 8 bits into 32 bits, then truncate to 1 bit.
4913
4914 SDLoc dl(Op);
4915 LoadSDNode *LD = cast<LoadSDNode>(Op);
4916
4917 SDValue Chain = LD->getChain();
4918 SDValue BasePtr = LD->getBasePtr();
4919 MachineMemOperand *MMO = LD->getMemOperand();
4920
4921 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(), Chain,
4922 BasePtr, MVT::i8, MMO);
4923 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
4924
4925 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
4926 return DAG.getMergeValues(Ops, 2, dl);
4927}
4928
4929SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
4930 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
4931 "Custom lowering only for i1 stores");
4932
4933 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
4934
4935 SDLoc dl(Op);
4936 StoreSDNode *ST = cast<StoreSDNode>(Op);
4937
4938 SDValue Chain = ST->getChain();
4939 SDValue BasePtr = ST->getBasePtr();
4940 SDValue Value = ST->getValue();
4941 MachineMemOperand *MMO = ST->getMemOperand();
4942
4943 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(), Value);
4944 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
4945}
4946
4947// FIXME: Remove this once the ANDI glue bug is fixed:
4948SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
4949 assert(Op.getValueType() == MVT::i1 &&
4950 "Custom lowering only for i1 results");
4951
4952 SDLoc DL(Op);
4953 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
4954 Op.getOperand(0));
4955}
4956
Chris Lattner4211ca92006-04-14 06:01:58 +00004957/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4958/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004959SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00004960 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00004961 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4962 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00004963 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004964
Hal Finkel81f87992013-04-07 22:11:09 +00004965 // We might be able to do better than this under some circumstances, but in
4966 // general, fsel-based lowering of select is a finite-math-only optimization.
4967 // For more information, see section F.3 of the 2.06 ISA specification.
4968 if (!DAG.getTarget().Options.NoInfsFPMath ||
4969 !DAG.getTarget().Options.NoNaNsFPMath)
4970 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004971
Hal Finkel81f87992013-04-07 22:11:09 +00004972 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004973
Owen Anderson53aa7a92009-08-10 22:56:29 +00004974 EVT ResVT = Op.getValueType();
4975 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004976 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4977 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004978 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004979
Chris Lattner4211ca92006-04-14 06:01:58 +00004980 // If the RHS of the comparison is a 0.0, we don't need to do the
4981 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00004982 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00004983 if (isFloatingPointZero(RHS))
4984 switch (CC) {
4985 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00004986 case ISD::SETNE:
4987 std::swap(TV, FV);
4988 case ISD::SETEQ:
4989 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4990 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
4991 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
4992 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4993 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4994 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4995 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00004996 case ISD::SETULT:
4997 case ISD::SETLT:
4998 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00004999 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005000 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00005001 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5002 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005003 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005004 case ISD::SETUGT:
5005 case ISD::SETGT:
5006 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005007 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005008 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00005009 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5010 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005011 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005012 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005013 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005014
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005015 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00005016 switch (CC) {
5017 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00005018 case ISD::SETNE:
5019 std::swap(TV, FV);
5020 case ISD::SETEQ:
5021 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
5022 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5023 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
5024 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
5025 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
5026 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
5027 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5028 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005029 case ISD::SETULT:
5030 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005031 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005032 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5033 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005034 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005035 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005036 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005037 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005038 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5039 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005040 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005041 case ISD::SETUGT:
5042 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005043 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005044 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5045 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005046 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005047 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005048 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005049 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005050 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5051 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005052 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005053 }
Eli Friedman5806e182009-05-28 04:31:08 +00005054 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00005055}
5056
Chris Lattner57ee7c62007-11-28 18:44:47 +00005057// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005058SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005059 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00005060 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005061 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00005062 if (Src.getValueType() == MVT::f32)
5063 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00005064
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005065 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00005066 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005067 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00005068 case MVT::i32:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005069 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Hal Finkelf6d45f22013-04-01 17:52:07 +00005070 (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ :
5071 PPCISD::FCTIDZ),
Owen Anderson9f944592009-08-11 20:47:22 +00005072 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005073 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005074 case MVT::i64:
Hal Finkel3f88d082013-04-01 18:42:58 +00005075 assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) &&
5076 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00005077 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
5078 PPCISD::FCTIDUZ,
5079 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005080 break;
5081 }
Duncan Sands2a287912008-07-19 16:26:02 +00005082
Chris Lattner4211ca92006-04-14 06:01:58 +00005083 // Convert the FP value to an int value through memory.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005084 bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() &&
5085 (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT());
5086 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
5087 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
5088 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00005089
Chris Lattner06a49542007-10-15 20:14:52 +00005090 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005091 SDValue Chain;
5092 if (i32Stack) {
5093 MachineFunction &MF = DAG.getMachineFunction();
5094 MachineMemOperand *MMO =
5095 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
5096 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
5097 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00005098 DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005099 } else
5100 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
5101 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00005102
5103 // Result is a load from the stack slot. If loading 4 bytes, make sure to
5104 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005105 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00005106 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00005107 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkelf6d45f22013-04-01 17:52:07 +00005108 MPI = MachinePointerInfo();
5109 }
5110
5111 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooper82cd9e82011-11-08 18:42:53 +00005112 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005113}
5114
Hal Finkelf6d45f22013-04-01 17:52:07 +00005115SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005116 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005117 SDLoc dl(Op);
Dan Gohmand6819da2008-03-11 01:59:03 +00005118 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00005119 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005120 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00005121
Hal Finkel6a56b212014-03-05 22:14:00 +00005122 if (Op.getOperand(0).getValueType() == MVT::i1)
5123 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0),
5124 DAG.getConstantFP(1.0, Op.getValueType()),
5125 DAG.getConstantFP(0.0, Op.getValueType()));
5126
Hal Finkelf6d45f22013-04-01 17:52:07 +00005127 assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) &&
5128 "UINT_TO_FP is supported only with FPCVT");
5129
5130 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00005131 // Otherwise, convert to double-precision and then round.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005132 unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
5133 (Op.getOpcode() == ISD::UINT_TO_FP ?
5134 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
5135 (Op.getOpcode() == ISD::UINT_TO_FP ?
5136 PPCISD::FCFIDU : PPCISD::FCFID);
5137 MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
5138 MVT::f32 : MVT::f64;
5139
Owen Anderson9f944592009-08-11 20:47:22 +00005140 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005141 SDValue SINT = Op.getOperand(0);
5142 // When converting to single-precision, we actually need to convert
5143 // to double-precision first and then round to single-precision.
5144 // To avoid double-rounding effects during that operation, we have
5145 // to prepare the input operand. Bits that might be truncated when
5146 // converting to double-precision are replaced by a bit that won't
5147 // be lost at this stage, but is below the single-precision rounding
5148 // position.
5149 //
5150 // However, if -enable-unsafe-fp-math is in effect, accept double
5151 // rounding to avoid the extra overhead.
5152 if (Op.getValueType() == MVT::f32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005153 !PPCSubTarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005154 !DAG.getTarget().Options.UnsafeFPMath) {
5155
5156 // Twiddle input to make sure the low 11 bits are zero. (If this
5157 // is the case, we are guaranteed the value will fit into the 53 bit
5158 // mantissa of an IEEE double-precision value without rounding.)
5159 // If any of those low 11 bits were not zero originally, make sure
5160 // bit 12 (value 2048) is set instead, so that the final rounding
5161 // to single-precision gets the correct result.
5162 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5163 SINT, DAG.getConstant(2047, MVT::i64));
5164 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
5165 Round, DAG.getConstant(2047, MVT::i64));
5166 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
5167 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5168 Round, DAG.getConstant(-2048, MVT::i64));
5169
5170 // However, we cannot use that value unconditionally: if the magnitude
5171 // of the input value is small, the bit-twiddling we did above might
5172 // end up visibly changing the output. Fortunately, in that case, we
5173 // don't need to twiddle bits since the original input will convert
5174 // exactly to double-precision floating-point already. Therefore,
5175 // construct a conditional to use the original value if the top 11
5176 // bits are all sign-bit copies, and use the rounded value computed
5177 // above otherwise.
5178 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
5179 SINT, DAG.getConstant(53, MVT::i32));
5180 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
5181 Cond, DAG.getConstant(1, MVT::i64));
5182 Cond = DAG.getSetCC(dl, MVT::i32,
5183 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
5184
5185 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
5186 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00005187
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005188 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005189 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
5190
5191 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00005192 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005193 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005194 return FP;
5195 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005196
Owen Anderson9f944592009-08-11 20:47:22 +00005197 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005198 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005199 // Since we only generate this in 64-bit mode, we can take advantage of
5200 // 64-bit registers. In particular, sign extend the input value into the
5201 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
5202 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00005203 MachineFunction &MF = DAG.getMachineFunction();
5204 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005205 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005206
Hal Finkelbeb296b2013-03-31 10:12:51 +00005207 SDValue Ld;
Hal Finkelf6d45f22013-04-01 17:52:07 +00005208 if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) {
Hal Finkelbeb296b2013-03-31 10:12:51 +00005209 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
5210 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005211
Hal Finkelbeb296b2013-03-31 10:12:51 +00005212 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
5213 MachinePointerInfo::getFixedStack(FrameIdx),
5214 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00005215
Hal Finkelbeb296b2013-03-31 10:12:51 +00005216 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
5217 "Expected an i32 store");
5218 MachineMemOperand *MMO =
5219 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
5220 MachineMemOperand::MOLoad, 4, 4);
5221 SDValue Ops[] = { Store, FIdx };
Hal Finkelf6d45f22013-04-01 17:52:07 +00005222 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
5223 PPCISD::LFIWZX : PPCISD::LFIWAX,
5224 dl, DAG.getVTList(MVT::f64, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00005225 Ops, MVT::i32, MMO);
Hal Finkelbeb296b2013-03-31 10:12:51 +00005226 } else {
Hal Finkelf6d45f22013-04-01 17:52:07 +00005227 assert(PPCSubTarget.isPPC64() &&
5228 "i32->FP without LFIWAX supported only on PPC64");
5229
Hal Finkelbeb296b2013-03-31 10:12:51 +00005230 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
5231 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
5232
5233 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
5234 Op.getOperand(0));
5235
5236 // STD the extended value into the stack slot.
5237 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
5238 MachinePointerInfo::getFixedStack(FrameIdx),
5239 false, false, 0);
5240
5241 // Load the value as a double.
5242 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
5243 MachinePointerInfo::getFixedStack(FrameIdx),
5244 false, false, false, 0);
5245 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005246
Chris Lattner4211ca92006-04-14 06:01:58 +00005247 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005248 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
5249 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00005250 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005251 return FP;
5252}
5253
Dan Gohman21cea8a2010-04-17 15:26:15 +00005254SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
5255 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005256 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005257 /*
5258 The rounding mode is in bits 30:31 of FPSR, and has the following
5259 settings:
5260 00 Round to nearest
5261 01 Round to 0
5262 10 Round to +inf
5263 11 Round to -inf
5264
5265 FLT_ROUNDS, on the other hand, expects the following:
5266 -1 Undefined
5267 0 Round to 0
5268 1 Round to nearest
5269 2 Round to +inf
5270 3 Round to -inf
5271
5272 To perform the conversion, we do:
5273 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
5274 */
5275
5276 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005277 EVT VT = Op.getValueType();
5278 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005279
5280 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005281 EVT NodeTys[] = {
5282 MVT::f64, // return register
5283 MVT::Glue // unused in this context
5284 };
Craig Topper48d114b2014-04-26 18:35:24 +00005285 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, ArrayRef<SDValue>());
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005286
5287 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00005288 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005289 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005290 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00005291 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005292
5293 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005294 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005295 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00005296 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005297 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005298
5299 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005300 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00005301 DAG.getNode(ISD::AND, dl, MVT::i32,
5302 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005303 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00005304 DAG.getNode(ISD::SRL, dl, MVT::i32,
5305 DAG.getNode(ISD::AND, dl, MVT::i32,
5306 DAG.getNode(ISD::XOR, dl, MVT::i32,
5307 CWD, DAG.getConstant(3, MVT::i32)),
5308 DAG.getConstant(3, MVT::i32)),
5309 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005310
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005311 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00005312 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005313
Duncan Sands13237ac2008-06-06 12:08:01 +00005314 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00005315 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005316}
5317
Dan Gohman21cea8a2010-04-17 15:26:15 +00005318SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005319 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005320 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005321 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00005322 assert(Op.getNumOperands() == 3 &&
5323 VT == Op.getOperand(1).getValueType() &&
5324 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005325
Chris Lattner601b8652006-09-20 03:47:40 +00005326 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005327 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005328 SDValue Lo = Op.getOperand(0);
5329 SDValue Hi = Op.getOperand(1);
5330 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005331 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005332
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005333 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005334 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005335 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5336 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5337 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5338 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005339 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005340 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5341 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5342 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005343 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005344 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005345}
5346
Dan Gohman21cea8a2010-04-17 15:26:15 +00005347SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005348 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005349 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00005350 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005351 assert(Op.getNumOperands() == 3 &&
5352 VT == Op.getOperand(1).getValueType() &&
5353 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005354
Dan Gohman8d2ead22008-03-07 20:36:53 +00005355 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005356 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005357 SDValue Lo = Op.getOperand(0);
5358 SDValue Hi = Op.getOperand(1);
5359 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005360 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005361
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005362 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005363 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005364 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5365 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5366 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5367 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005368 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005369 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5370 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5371 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005372 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005373 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005374}
5375
Dan Gohman21cea8a2010-04-17 15:26:15 +00005376SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005377 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005378 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005379 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005380 assert(Op.getNumOperands() == 3 &&
5381 VT == Op.getOperand(1).getValueType() &&
5382 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005383
Dan Gohman8d2ead22008-03-07 20:36:53 +00005384 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005385 SDValue Lo = Op.getOperand(0);
5386 SDValue Hi = Op.getOperand(1);
5387 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005388 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005389
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005390 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005391 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005392 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5393 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5394 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5395 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005396 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005397 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5398 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5399 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00005400 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005401 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005402 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005403}
5404
5405//===----------------------------------------------------------------------===//
5406// Vector related lowering.
5407//
5408
Chris Lattner2a099c02006-04-17 06:00:21 +00005409/// BuildSplatI - Build a canonical splati of Val with an element size of
5410/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005411static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005412 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00005413 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005414
Owen Anderson53aa7a92009-08-10 22:56:29 +00005415 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00005416 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00005417 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005418
Owen Anderson9f944592009-08-11 20:47:22 +00005419 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005420
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005421 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5422 if (Val == -1)
5423 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005424
Owen Anderson53aa7a92009-08-10 22:56:29 +00005425 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005426
Chris Lattner2a099c02006-04-17 06:00:21 +00005427 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00005428 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005429 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00005430 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Craig Topper48d114b2014-04-26 18:35:24 +00005431 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005432 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005433}
5434
Hal Finkelcf2e9082013-05-24 23:00:14 +00005435/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5436/// specified intrinsic ID.
5437static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005438 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00005439 EVT DestVT = MVT::Other) {
5440 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5441 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5442 DAG.getConstant(IID, MVT::i32), Op);
5443}
5444
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005445/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00005446/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005447static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005448 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005449 EVT DestVT = MVT::Other) {
5450 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005451 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005452 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005453}
5454
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005455/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5456/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005457static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005458 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005459 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00005460 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005461 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005462 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005463}
5464
5465
Chris Lattner264c9082006-04-17 17:55:10 +00005466/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5467/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005468static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005469 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00005470 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00005471 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5472 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005473
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005474 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00005475 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005476 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00005477 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005478 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00005479}
5480
Chris Lattner19e90552006-04-14 05:19:18 +00005481// If this is a case we can't handle, return null and let the default
5482// expansion code take care of it. If we CAN select this case, and if it
5483// selects to a single instruction, return Op. Otherwise, if we can codegen
5484// this case more efficiently than a constant pool load, lower it to the
5485// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005486SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5487 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005488 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005489 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5490 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00005491
Bob Wilson85cefe82009-03-02 23:24:16 +00005492 // Check if this is a splat of a constant value.
5493 APInt APSplatBits, APSplatUndef;
5494 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005495 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00005496 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00005497 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00005498 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00005499
Bob Wilson530e0382009-03-03 19:26:27 +00005500 unsigned SplatBits = APSplatBits.getZExtValue();
5501 unsigned SplatUndef = APSplatUndef.getZExtValue();
5502 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005503
Bob Wilson530e0382009-03-03 19:26:27 +00005504 // First, handle single instruction cases.
5505
5506 // All zeros?
5507 if (SplatBits == 0) {
5508 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00005509 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5510 SDValue Z = DAG.getConstant(0, MVT::i32);
5511 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00005512 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00005513 }
Bob Wilson530e0382009-03-03 19:26:27 +00005514 return Op;
5515 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00005516
Bob Wilson530e0382009-03-03 19:26:27 +00005517 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5518 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5519 (32-SplatBitSize));
5520 if (SextVal >= -16 && SextVal <= 15)
5521 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005522
5523
Bob Wilson530e0382009-03-03 19:26:27 +00005524 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005525
Bob Wilson530e0382009-03-03 19:26:27 +00005526 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00005527 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5528 // If this value is in the range [17,31] and is odd, use:
5529 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5530 // If this value is in the range [-31,-17] and is odd, use:
5531 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5532 // Note the last two are three-instruction sequences.
5533 if (SextVal >= -32 && SextVal <= 31) {
5534 // To avoid having these optimizations undone by constant folding,
5535 // we convert to a pseudo that will be expanded later into one of
5536 // the above forms.
5537 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt51e79512013-02-20 15:50:31 +00005538 EVT VT = Op.getValueType();
5539 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5540 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5541 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilson530e0382009-03-03 19:26:27 +00005542 }
5543
5544 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5545 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5546 // for fneg/fabs.
5547 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5548 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00005549 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005550
5551 // Make the VSLW intrinsic, computing 0x8000_0000.
5552 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5553 OnesV, DAG, dl);
5554
5555 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00005556 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00005557 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005558 }
5559
5560 // Check to see if this is a wide variety of vsplti*, binop self cases.
5561 static const signed char SplatCsts[] = {
5562 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5563 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5564 };
5565
5566 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5567 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5568 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5569 int i = SplatCsts[idx];
5570
5571 // Figure out what shift amount will be used by altivec if shifted by i in
5572 // this splat size.
5573 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5574
5575 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00005576 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005577 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005578 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5579 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5580 Intrinsic::ppc_altivec_vslw
5581 };
5582 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005583 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005584 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005585
Bob Wilson530e0382009-03-03 19:26:27 +00005586 // vsplti + srl self.
5587 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005588 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005589 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5590 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5591 Intrinsic::ppc_altivec_vsrw
5592 };
5593 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005594 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005595 }
5596
Bob Wilson530e0382009-03-03 19:26:27 +00005597 // vsplti + sra self.
5598 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005599 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005600 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5601 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5602 Intrinsic::ppc_altivec_vsraw
5603 };
5604 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005605 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005606 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005607
Bob Wilson530e0382009-03-03 19:26:27 +00005608 // vsplti + rol self.
5609 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5610 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005611 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005612 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5613 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5614 Intrinsic::ppc_altivec_vrlw
5615 };
5616 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005617 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005618 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005619
Bob Wilson530e0382009-03-03 19:26:27 +00005620 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00005621 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005622 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005623 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00005624 }
Bob Wilson530e0382009-03-03 19:26:27 +00005625 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00005626 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005627 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005628 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00005629 }
Bob Wilson530e0382009-03-03 19:26:27 +00005630 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00005631 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005632 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005633 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5634 }
5635 }
5636
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005637 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00005638}
5639
Chris Lattner071ad012006-04-17 05:28:54 +00005640/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5641/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005642static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005643 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005644 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00005645 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00005646 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00005647 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005648
Chris Lattner071ad012006-04-17 05:28:54 +00005649 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00005650 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 +00005651 OP_VMRGHW,
5652 OP_VMRGLW,
5653 OP_VSPLTISW0,
5654 OP_VSPLTISW1,
5655 OP_VSPLTISW2,
5656 OP_VSPLTISW3,
5657 OP_VSLDOI4,
5658 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00005659 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00005660 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00005661
Chris Lattner071ad012006-04-17 05:28:54 +00005662 if (OpNum == OP_COPY) {
5663 if (LHSID == (1*9+2)*9+3) return LHS;
5664 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5665 return RHS;
5666 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005667
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005668 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005669 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5670 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005671
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005672 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00005673 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005674 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00005675 case OP_VMRGHW:
5676 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5677 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5678 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5679 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5680 break;
5681 case OP_VMRGLW:
5682 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5683 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5684 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5685 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5686 break;
5687 case OP_VSPLTISW0:
5688 for (unsigned i = 0; i != 16; ++i)
5689 ShufIdxs[i] = (i&3)+0;
5690 break;
5691 case OP_VSPLTISW1:
5692 for (unsigned i = 0; i != 16; ++i)
5693 ShufIdxs[i] = (i&3)+4;
5694 break;
5695 case OP_VSPLTISW2:
5696 for (unsigned i = 0; i != 16; ++i)
5697 ShufIdxs[i] = (i&3)+8;
5698 break;
5699 case OP_VSPLTISW3:
5700 for (unsigned i = 0; i != 16; ++i)
5701 ShufIdxs[i] = (i&3)+12;
5702 break;
5703 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005704 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005705 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005706 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005707 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005708 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005709 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00005710 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00005711 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5712 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005713 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00005714 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00005715}
5716
Chris Lattner19e90552006-04-14 05:19:18 +00005717/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5718/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5719/// return the code it can be lowered into. Worst case, it can always be
5720/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005721SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005722 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005723 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005724 SDValue V1 = Op.getOperand(0);
5725 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005726 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005727 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005728
Chris Lattner19e90552006-04-14 05:19:18 +00005729 // Cases that are handled by instructions that take permute immediates
5730 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5731 // selected by the instruction selector.
5732 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005733 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5734 PPC::isSplatShuffleMask(SVOp, 2) ||
5735 PPC::isSplatShuffleMask(SVOp, 4) ||
5736 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5737 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5738 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5739 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5740 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5741 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5742 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5743 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5744 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattner19e90552006-04-14 05:19:18 +00005745 return Op;
5746 }
5747 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005748
Chris Lattner19e90552006-04-14 05:19:18 +00005749 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5750 // and produce a fixed permutation. If any of these match, do not lower to
5751 // VPERM.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005752 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5753 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5754 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5755 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5756 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5757 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5758 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5759 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5760 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattner19e90552006-04-14 05:19:18 +00005761 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005762
Chris Lattner071ad012006-04-17 05:28:54 +00005763 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5764 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005765 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00005766
Chris Lattner071ad012006-04-17 05:28:54 +00005767 unsigned PFIndexes[4];
5768 bool isFourElementShuffle = true;
5769 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5770 unsigned EltNo = 8; // Start out undef.
5771 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005772 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00005773 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005774
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005775 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00005776 if ((ByteSource & 3) != j) {
5777 isFourElementShuffle = false;
5778 break;
5779 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005780
Chris Lattner071ad012006-04-17 05:28:54 +00005781 if (EltNo == 8) {
5782 EltNo = ByteSource/4;
5783 } else if (EltNo != ByteSource/4) {
5784 isFourElementShuffle = false;
5785 break;
5786 }
5787 }
5788 PFIndexes[i] = EltNo;
5789 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005790
5791 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00005792 // perfect shuffle vector to determine if it is cost effective to do this as
5793 // discrete instructions, or whether we should use a vperm.
5794 if (isFourElementShuffle) {
5795 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005796 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00005797 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005798
Chris Lattner071ad012006-04-17 05:28:54 +00005799 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5800 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005801
Chris Lattner071ad012006-04-17 05:28:54 +00005802 // Determining when to avoid vperm is tricky. Many things affect the cost
5803 // of vperm, particularly how many times the perm mask needs to be computed.
5804 // For example, if the perm mask can be hoisted out of a loop or is already
5805 // used (perhaps because there are multiple permutes with the same shuffle
5806 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5807 // the loop requires an extra register.
5808 //
5809 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00005810 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00005811 // available, if this block is within a loop, we should avoid using vperm
5812 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005813 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005814 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00005815 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005816
Chris Lattner19e90552006-04-14 05:19:18 +00005817 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5818 // vector that will get spilled to the constant pool.
5819 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005820
Chris Lattner19e90552006-04-14 05:19:18 +00005821 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5822 // that it is in input element units, not in bytes. Convert now.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005823 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005824 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005825
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005826 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005827 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5828 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005829
Chris Lattner19e90552006-04-14 05:19:18 +00005830 for (unsigned j = 0; j != BytesPerElement; ++j)
5831 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson9f944592009-08-11 20:47:22 +00005832 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00005833 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005834
Owen Anderson9f944592009-08-11 20:47:22 +00005835 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Craig Topper48d114b2014-04-26 18:35:24 +00005836 ResultMask);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005837 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00005838}
5839
Chris Lattner9754d142006-04-18 17:59:36 +00005840/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5841/// altivec comparison. If it is, return true and fill in Opc/isDot with
5842/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005843static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner9754d142006-04-18 17:59:36 +00005844 bool &isDot) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005845 unsigned IntrinsicID =
5846 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00005847 CompareOpc = -1;
5848 isDot = false;
5849 switch (IntrinsicID) {
5850 default: return false;
5851 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00005852 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5853 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5854 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5855 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5856 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5857 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5858 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5859 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5860 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5861 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5862 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5863 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5864 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005865
Chris Lattner4211ca92006-04-14 06:01:58 +00005866 // Normal Comparisons.
5867 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5868 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5869 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5870 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5871 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5872 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5873 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5874 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5875 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5876 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5877 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5878 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5879 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5880 }
Chris Lattner9754d142006-04-18 17:59:36 +00005881 return true;
5882}
5883
5884/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5885/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005886SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005887 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00005888 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5889 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005890 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00005891 int CompareOpc;
5892 bool isDot;
5893 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005894 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005895
Chris Lattner9754d142006-04-18 17:59:36 +00005896 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00005897 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00005898 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00005899 Op.getOperand(1), Op.getOperand(2),
5900 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00005901 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00005902 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005903
Chris Lattner4211ca92006-04-14 06:01:58 +00005904 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005905 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005906 Op.getOperand(2), // LHS
5907 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00005908 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00005909 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005910 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00005911 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005912
Chris Lattner4211ca92006-04-14 06:01:58 +00005913 // Now that we have the comparison, emit a copy from the CR to a GPR.
5914 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00005915 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00005916 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00005917 CompNode.getValue(1));
5918
Chris Lattner4211ca92006-04-14 06:01:58 +00005919 // Unpack the result based on how the target uses it.
5920 unsigned BitNo; // Bit # of CR6.
5921 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00005922 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00005923 default: // Can't happen, don't crash on invalid number though.
5924 case 0: // Return the value of the EQ bit of CR6.
5925 BitNo = 0; InvertBit = false;
5926 break;
5927 case 1: // Return the inverted value of the EQ bit of CR6.
5928 BitNo = 0; InvertBit = true;
5929 break;
5930 case 2: // Return the value of the LT bit of CR6.
5931 BitNo = 2; InvertBit = false;
5932 break;
5933 case 3: // Return the inverted value of the LT bit of CR6.
5934 BitNo = 2; InvertBit = true;
5935 break;
5936 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005937
Chris Lattner4211ca92006-04-14 06:01:58 +00005938 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00005939 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5940 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005941 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00005942 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5943 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00005944
Chris Lattner4211ca92006-04-14 06:01:58 +00005945 // If we are supposed to, toggle the bit.
5946 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00005947 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5948 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00005949 return Flags;
5950}
5951
Hal Finkel5c0d1452014-03-30 13:22:59 +00005952SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
5953 SelectionDAG &DAG) const {
5954 SDLoc dl(Op);
5955 // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int
5956 // instructions), but for smaller types, we need to first extend up to v2i32
5957 // before doing going farther.
5958 if (Op.getValueType() == MVT::v2i64) {
5959 EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
5960 if (ExtVT != MVT::v2i32) {
5961 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0));
5962 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op,
5963 DAG.getValueType(EVT::getVectorVT(*DAG.getContext(),
5964 ExtVT.getVectorElementType(), 4)));
5965 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op);
5966 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op,
5967 DAG.getValueType(MVT::v2i32));
5968 }
5969
5970 return Op;
5971 }
5972
5973 return SDValue();
5974}
5975
Scott Michelcf0da6c2009-02-17 22:15:04 +00005976SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005977 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005978 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00005979 // Create a stack slot that is 16-byte aligned.
5980 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00005981 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00005982 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005983 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005984
Chris Lattner4211ca92006-04-14 06:01:58 +00005985 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00005986 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00005987 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005988 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005989 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00005990 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005991 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005992}
5993
Dan Gohman21cea8a2010-04-17 15:26:15 +00005994SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005995 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005996 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005997 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005998
Owen Anderson9f944592009-08-11 20:47:22 +00005999 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
6000 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006001
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006002 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006003 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006004
Chris Lattner7e4398742006-04-18 03:43:48 +00006005 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00006006 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
6007 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
6008 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006009
Chris Lattner7e4398742006-04-18 03:43:48 +00006010 // Low parts multiplied together, generating 32-bit results (we ignore the
6011 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006012 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00006013 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006014
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006015 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00006016 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00006017 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006018 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006019 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00006020 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
6021 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006022 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006023
Owen Anderson9f944592009-08-11 20:47:22 +00006024 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00006025
Chris Lattner96d50482006-04-18 04:28:57 +00006026 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006027 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00006028 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006029 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006030
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006031 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006032 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00006033 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00006034 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006035
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006036 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006037 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00006038 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00006039 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006040
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006041 // Merge the results together.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006042 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006043 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006044 Ops[i*2 ] = 2*i+1;
6045 Ops[i*2+1] = 2*i+1+16;
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006046 }
Owen Anderson9f944592009-08-11 20:47:22 +00006047 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00006048 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006049 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00006050 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006051}
6052
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006053/// LowerOperation - Provide custom lowering hooks for some operations.
6054///
Dan Gohman21cea8a2010-04-17 15:26:15 +00006055SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006056 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006057 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00006058 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00006059 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006060 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00006061 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00006062 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006063 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00006064 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
6065 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006066 case ISD::VASTART:
Dan Gohman31ae5862010-04-17 14:41:14 +00006067 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006068
6069 case ISD::VAARG:
Dan Gohman31ae5862010-04-17 14:41:14 +00006070 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00006071
Roman Divackyc3825df2013-07-25 21:36:47 +00006072 case ISD::VACOPY:
6073 return LowerVACOPY(Op, DAG, PPCSubTarget);
6074
Jim Laskeye4f4d042006-12-04 22:04:42 +00006075 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00006076 case ISD::DYNAMIC_STACKALLOC:
6077 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng51096af2008-04-19 01:30:48 +00006078
Hal Finkel756810f2013-03-21 21:37:52 +00006079 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
6080 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
6081
Hal Finkel940ab932014-02-28 00:27:01 +00006082 case ISD::LOAD: return LowerLOAD(Op, DAG);
6083 case ISD::STORE: return LowerSTORE(Op, DAG);
6084 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006085 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006086 case ISD::FP_TO_UINT:
6087 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006088 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00006089 case ISD::UINT_TO_FP:
6090 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00006091 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006092
Chris Lattner4211ca92006-04-14 06:01:58 +00006093 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00006094 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
6095 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
6096 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006097
Chris Lattner4211ca92006-04-14 06:01:58 +00006098 // Vector-related lowering.
6099 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6100 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6101 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6102 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Hal Finkel5c0d1452014-03-30 13:22:59 +00006103 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006104 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006105
Hal Finkel25c19922013-05-15 21:37:41 +00006106 // For counter-based loop handling.
6107 case ISD::INTRINSIC_W_CHAIN: return SDValue();
6108
Chris Lattnerf6a81562007-12-08 06:59:59 +00006109 // Frame & Return address.
6110 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00006111 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00006112 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006113}
6114
Duncan Sands6ed40142008-12-01 11:39:25 +00006115void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
6116 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006117 SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00006118 const TargetMachine &TM = getTargetMachine();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006119 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00006120 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00006121 default:
Craig Toppere55c5562012-02-07 02:50:20 +00006122 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkel25c19922013-05-15 21:37:41 +00006123 case ISD::INTRINSIC_W_CHAIN: {
6124 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
6125 Intrinsic::ppc_is_decremented_ctr_nonzero)
6126 break;
6127
6128 assert(N->getValueType(0) == MVT::i1 &&
6129 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00006130 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00006131 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
6132 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
6133 N->getOperand(1));
6134
6135 Results.push_back(NewInt);
6136 Results.push_back(NewInt.getValue(1));
6137 break;
6138 }
Roman Divacky4394e682011-06-28 15:30:42 +00006139 case ISD::VAARG: {
6140 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
6141 || TM.getSubtarget<PPCSubtarget>().isPPC64())
6142 return;
6143
6144 EVT VT = N->getValueType(0);
6145
6146 if (VT == MVT::i64) {
6147 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
6148
6149 Results.push_back(NewNode);
6150 Results.push_back(NewNode.getValue(1));
6151 }
6152 return;
6153 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006154 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00006155 assert(N->getValueType(0) == MVT::ppcf128);
6156 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006157 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006158 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006159 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00006160 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006161 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006162 DAG.getIntPtrConstant(1));
6163
Ulrich Weigand874fc622013-03-26 10:56:22 +00006164 // Add the two halves of the long double in round-to-zero mode.
6165 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00006166
6167 // We know the low half is about to be thrown away, so just use something
6168 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00006169 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00006170 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00006171 return;
Duncan Sands2a287912008-07-19 16:26:02 +00006172 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006173 case ISD::FP_TO_SINT:
Bill Schmidt41221692013-07-09 18:50:20 +00006174 // LowerFP_TO_INT() can only handle f32 and f64.
6175 if (N->getOperand(0).getValueType() == MVT::ppcf128)
6176 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006177 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00006178 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00006179 }
6180}
6181
6182
Chris Lattner4211ca92006-04-14 06:01:58 +00006183//===----------------------------------------------------------------------===//
6184// Other Lowering Code
6185//===----------------------------------------------------------------------===//
6186
Chris Lattner9b577f12005-08-26 21:23:58 +00006187MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00006188PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman747e55b2009-02-07 16:15:20 +00006189 bool is64bit, unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006190 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesend4eb0522008-08-25 22:34:37 +00006191 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6192
6193 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6194 MachineFunction *F = BB->getParent();
6195 MachineFunction::iterator It = BB;
6196 ++It;
6197
6198 unsigned dest = MI->getOperand(0).getReg();
6199 unsigned ptrA = MI->getOperand(1).getReg();
6200 unsigned ptrB = MI->getOperand(2).getReg();
6201 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006202 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00006203
6204 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6205 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6206 F->insert(It, loopMBB);
6207 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006208 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006209 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006210 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006211
6212 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006213 unsigned TmpReg = (!BinOpcode) ? incr :
6214 RegInfo.createVirtualRegister(
Dale Johannesenbc698292008-09-02 20:30:23 +00006215 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6216 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006217
6218 // thisMBB:
6219 // ...
6220 // fallthrough --> loopMBB
6221 BB->addSuccessor(loopMBB);
6222
6223 // loopMBB:
6224 // l[wd]arx dest, ptr
6225 // add r0, dest, incr
6226 // st[wd]cx. r0, ptr
6227 // bne- loopMBB
6228 // fallthrough --> exitMBB
6229 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006230 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00006231 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006232 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006233 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
6234 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesend4eb0522008-08-25 22:34:37 +00006235 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006236 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006237 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006238 BB->addSuccessor(loopMBB);
6239 BB->addSuccessor(exitMBB);
6240
6241 // exitMBB:
6242 // ...
6243 BB = exitMBB;
6244 return BB;
6245}
6246
6247MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00006248PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00006249 MachineBasicBlock *BB,
6250 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00006251 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006252 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesena32affb2008-08-28 17:53:09 +00006253 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6254 // In 64 bit mode we have to use 64 bits for addresses, even though the
6255 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
6256 // registers without caring whether they're 32 or 64, but here we're
6257 // doing actual arithmetic on the addresses.
6258 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00006259 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00006260
6261 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6262 MachineFunction *F = BB->getParent();
6263 MachineFunction::iterator It = BB;
6264 ++It;
6265
6266 unsigned dest = MI->getOperand(0).getReg();
6267 unsigned ptrA = MI->getOperand(1).getReg();
6268 unsigned ptrB = MI->getOperand(2).getReg();
6269 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006270 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00006271
6272 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6273 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6274 F->insert(It, loopMBB);
6275 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006276 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006277 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006278 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006279
6280 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006281 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006282 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6283 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00006284 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6285 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6286 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6287 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
6288 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6289 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6290 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6291 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6292 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
6293 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006294 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006295 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006296 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006297
6298 // thisMBB:
6299 // ...
6300 // fallthrough --> loopMBB
6301 BB->addSuccessor(loopMBB);
6302
6303 // The 4-byte load must be aligned, while a char or short may be
6304 // anywhere in the word. Hence all this nasty bookkeeping code.
6305 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6306 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006307 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00006308 // rlwinm ptr, ptr1, 0, 0, 29
6309 // slw incr2, incr, shift
6310 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6311 // slw mask, mask2, shift
6312 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00006313 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006314 // add tmp, tmpDest, incr2
6315 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00006316 // and tmp3, tmp, mask
6317 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00006318 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00006319 // bne- loopMBB
6320 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006321 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006322 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00006323 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006324 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006325 .addReg(ptrA).addReg(ptrB);
6326 } else {
6327 Ptr1Reg = ptrB;
6328 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006329 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006330 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006331 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006332 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6333 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006334 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006335 .addReg(Ptr1Reg).addImm(0).addImm(61);
6336 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006337 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006338 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006339 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006340 .addReg(incr).addReg(ShiftReg);
6341 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006342 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00006343 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006344 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6345 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00006346 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006347 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006348 .addReg(Mask2Reg).addReg(ShiftReg);
6349
6350 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006351 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006352 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006353 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006354 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006355 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006356 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006357 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006358 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006359 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006360 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006361 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00006362 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006363 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006364 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006365 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006366 BB->addSuccessor(loopMBB);
6367 BB->addSuccessor(exitMBB);
6368
6369 // exitMBB:
6370 // ...
6371 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006372 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6373 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00006374 return BB;
6375}
6376
Hal Finkel756810f2013-03-21 21:37:52 +00006377llvm::MachineBasicBlock*
6378PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6379 MachineBasicBlock *MBB) const {
6380 DebugLoc DL = MI->getDebugLoc();
6381 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6382
6383 MachineFunction *MF = MBB->getParent();
6384 MachineRegisterInfo &MRI = MF->getRegInfo();
6385
6386 const BasicBlock *BB = MBB->getBasicBlock();
6387 MachineFunction::iterator I = MBB;
6388 ++I;
6389
6390 // Memory Reference
6391 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6392 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6393
6394 unsigned DstReg = MI->getOperand(0).getReg();
6395 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6396 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6397 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6398 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6399
6400 MVT PVT = getPointerTy();
6401 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6402 "Invalid Pointer Size!");
6403 // For v = setjmp(buf), we generate
6404 //
6405 // thisMBB:
6406 // SjLjSetup mainMBB
6407 // bl mainMBB
6408 // v_restore = 1
6409 // b sinkMBB
6410 //
6411 // mainMBB:
6412 // buf[LabelOffset] = LR
6413 // v_main = 0
6414 //
6415 // sinkMBB:
6416 // v = phi(main, restore)
6417 //
6418
6419 MachineBasicBlock *thisMBB = MBB;
6420 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6421 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6422 MF->insert(I, mainMBB);
6423 MF->insert(I, sinkMBB);
6424
6425 MachineInstrBuilder MIB;
6426
6427 // Transfer the remainder of BB and its successor edges to sinkMBB.
6428 sinkMBB->splice(sinkMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006429 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Hal Finkel756810f2013-03-21 21:37:52 +00006430 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6431
6432 // Note that the structure of the jmp_buf used here is not compatible
6433 // with that used by libc, and is not designed to be. Specifically, it
6434 // stores only those 'reserved' registers that LLVM does not otherwise
6435 // understand how to spill. Also, by convention, by the time this
6436 // intrinsic is called, Clang has already stored the frame address in the
6437 // first slot of the buffer and stack address in the third. Following the
6438 // X86 target code, we'll store the jump address in the second slot. We also
6439 // need to save the TOC pointer (R2) to handle jumps between shared
6440 // libraries, and that will be stored in the fourth slot. The thread
6441 // identifier (R13) is not affected.
6442
6443 // thisMBB:
6444 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6445 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006446 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006447
6448 // Prepare IP either in reg.
6449 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6450 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6451 unsigned BufReg = MI->getOperand(1).getReg();
6452
6453 if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) {
6454 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6455 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006456 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006457 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006458 MIB.setMemRefs(MMOBegin, MMOEnd);
6459 }
6460
Hal Finkelf05d6c72013-07-17 23:50:51 +00006461 // Naked functions never have a base pointer, and so we use r1. For all
6462 // other functions, this decision must be delayed until during PEI.
6463 unsigned BaseReg;
6464 if (MF->getFunction()->getAttributes().hasAttribute(
6465 AttributeSet::FunctionIndex, Attribute::Naked))
6466 BaseReg = PPCSubTarget.isPPC64() ? PPC::X1 : PPC::R1;
6467 else
6468 BaseReg = PPCSubTarget.isPPC64() ? PPC::BP8 : PPC::BP;
6469
6470 MIB = BuildMI(*thisMBB, MI, DL,
6471 TII->get(PPCSubTarget.isPPC64() ? PPC::STD : PPC::STW))
6472 .addReg(BaseReg)
6473 .addImm(BPOffset)
6474 .addReg(BufReg);
6475 MIB.setMemRefs(MMOBegin, MMOEnd);
6476
Hal Finkel756810f2013-03-21 21:37:52 +00006477 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00006478 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling5e7656b2013-06-07 07:55:53 +00006479 const PPCRegisterInfo *TRI =
6480 static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo());
6481 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00006482
6483 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6484
6485 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6486 .addMBB(mainMBB);
6487 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6488
6489 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6490 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6491
6492 // mainMBB:
6493 // mainDstReg = 0
6494 MIB = BuildMI(mainMBB, DL,
6495 TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
6496
6497 // Store IP
6498 if (PPCSubTarget.isPPC64()) {
6499 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6500 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006501 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006502 .addReg(BufReg);
6503 } else {
6504 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6505 .addReg(LabelReg)
6506 .addImm(LabelOffset)
6507 .addReg(BufReg);
6508 }
6509
6510 MIB.setMemRefs(MMOBegin, MMOEnd);
6511
6512 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6513 mainMBB->addSuccessor(sinkMBB);
6514
6515 // sinkMBB:
6516 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6517 TII->get(PPC::PHI), DstReg)
6518 .addReg(mainDstReg).addMBB(mainMBB)
6519 .addReg(restoreDstReg).addMBB(thisMBB);
6520
6521 MI->eraseFromParent();
6522 return sinkMBB;
6523}
6524
6525MachineBasicBlock *
6526PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6527 MachineBasicBlock *MBB) const {
6528 DebugLoc DL = MI->getDebugLoc();
6529 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6530
6531 MachineFunction *MF = MBB->getParent();
6532 MachineRegisterInfo &MRI = MF->getRegInfo();
6533
6534 // Memory Reference
6535 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6536 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6537
6538 MVT PVT = getPointerTy();
6539 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6540 "Invalid Pointer Size!");
6541
6542 const TargetRegisterClass *RC =
6543 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6544 unsigned Tmp = MRI.createVirtualRegister(RC);
6545 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6546 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6547 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006548 unsigned BP = (PVT == MVT::i64) ? PPC::X30 : PPC::R30;
Hal Finkel756810f2013-03-21 21:37:52 +00006549
6550 MachineInstrBuilder MIB;
6551
6552 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6553 const int64_t SPOffset = 2 * PVT.getStoreSize();
6554 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006555 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006556
6557 unsigned BufReg = MI->getOperand(0).getReg();
6558
6559 // Reload FP (the jumped-to function may not have had a
6560 // frame pointer, and if so, then its r31 will be restored
6561 // as necessary).
6562 if (PVT == MVT::i64) {
6563 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6564 .addImm(0)
6565 .addReg(BufReg);
6566 } else {
6567 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6568 .addImm(0)
6569 .addReg(BufReg);
6570 }
6571 MIB.setMemRefs(MMOBegin, MMOEnd);
6572
6573 // Reload IP
6574 if (PVT == MVT::i64) {
6575 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006576 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006577 .addReg(BufReg);
6578 } else {
6579 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6580 .addImm(LabelOffset)
6581 .addReg(BufReg);
6582 }
6583 MIB.setMemRefs(MMOBegin, MMOEnd);
6584
6585 // Reload SP
6586 if (PVT == MVT::i64) {
6587 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006588 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006589 .addReg(BufReg);
6590 } else {
6591 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6592 .addImm(SPOffset)
6593 .addReg(BufReg);
6594 }
6595 MIB.setMemRefs(MMOBegin, MMOEnd);
6596
Hal Finkelf05d6c72013-07-17 23:50:51 +00006597 // Reload BP
6598 if (PVT == MVT::i64) {
6599 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
6600 .addImm(BPOffset)
6601 .addReg(BufReg);
6602 } else {
6603 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
6604 .addImm(BPOffset)
6605 .addReg(BufReg);
6606 }
6607 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00006608
6609 // Reload TOC
6610 if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) {
6611 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006612 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006613 .addReg(BufReg);
6614
6615 MIB.setMemRefs(MMOBegin, MMOEnd);
6616 }
6617
6618 // Jump
6619 BuildMI(*MBB, MI, DL,
6620 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6621 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6622
6623 MI->eraseFromParent();
6624 return MBB;
6625}
6626
Dale Johannesena32affb2008-08-28 17:53:09 +00006627MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00006628PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00006629 MachineBasicBlock *BB) const {
Hal Finkel756810f2013-03-21 21:37:52 +00006630 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6631 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6632 return emitEHSjLjSetJmp(MI, BB);
6633 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6634 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6635 return emitEHSjLjLongJmp(MI, BB);
6636 }
6637
Evan Cheng20350c42006-11-27 23:37:22 +00006638 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00006639
6640 // To "insert" these instructions we actually have to insert their
6641 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00006642 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00006643 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00006644 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00006645
Dan Gohman3b460302008-07-07 23:14:23 +00006646 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00006647
Hal Finkel460e94d2012-06-22 23:10:08 +00006648 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006649 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6650 MI->getOpcode() == PPC::SELECT_I4 ||
6651 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00006652 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00006653 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6654 MI->getOpcode() == PPC::SELECT_CC_I8)
6655 Cond.push_back(MI->getOperand(4));
6656 else
6657 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00006658 Cond.push_back(MI->getOperand(1));
6659
Hal Finkel460e94d2012-06-22 23:10:08 +00006660 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00006661 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6662 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
6663 Cond, MI->getOperand(2).getReg(),
6664 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00006665 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6666 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6667 MI->getOpcode() == PPC::SELECT_CC_F4 ||
6668 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006669 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
6670 MI->getOpcode() == PPC::SELECT_I4 ||
6671 MI->getOpcode() == PPC::SELECT_I8 ||
6672 MI->getOpcode() == PPC::SELECT_F4 ||
6673 MI->getOpcode() == PPC::SELECT_F8 ||
6674 MI->getOpcode() == PPC::SELECT_VRRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00006675 // The incoming instruction knows the destination vreg to set, the
6676 // condition code register to branch on, the true/false values to
6677 // select between, and a branch opcode to use.
6678
6679 // thisMBB:
6680 // ...
6681 // TrueVal = ...
6682 // cmpTY ccX, r1, r2
6683 // bCC copy1MBB
6684 // fallthrough --> copy0MBB
6685 MachineBasicBlock *thisMBB = BB;
6686 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6687 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006688 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006689 F->insert(It, copy0MBB);
6690 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006691
6692 // Transfer the remainder of BB and its successor edges to sinkMBB.
6693 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006694 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006695 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6696
Evan Cheng32e376f2008-07-12 02:23:19 +00006697 // Next, add the true and fallthrough blocks as its successors.
6698 BB->addSuccessor(copy0MBB);
6699 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006700
Hal Finkel940ab932014-02-28 00:27:01 +00006701 if (MI->getOpcode() == PPC::SELECT_I4 ||
6702 MI->getOpcode() == PPC::SELECT_I8 ||
6703 MI->getOpcode() == PPC::SELECT_F4 ||
6704 MI->getOpcode() == PPC::SELECT_F8 ||
6705 MI->getOpcode() == PPC::SELECT_VRRC) {
6706 BuildMI(BB, dl, TII->get(PPC::BC))
6707 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6708 } else {
6709 unsigned SelectPred = MI->getOperand(4).getImm();
6710 BuildMI(BB, dl, TII->get(PPC::BCC))
6711 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6712 }
Dan Gohman34396292010-07-06 20:24:04 +00006713
Evan Cheng32e376f2008-07-12 02:23:19 +00006714 // copy0MBB:
6715 // %FalseValue = ...
6716 // # fallthrough to sinkMBB
6717 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006718
Evan Cheng32e376f2008-07-12 02:23:19 +00006719 // Update machine-CFG edges
6720 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006721
Evan Cheng32e376f2008-07-12 02:23:19 +00006722 // sinkMBB:
6723 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6724 // ...
6725 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00006726 BuildMI(*BB, BB->begin(), dl,
6727 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00006728 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
6729 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6730 }
Dale Johannesena32affb2008-08-28 17:53:09 +00006731 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
6732 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
6733 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
6734 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006735 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
6736 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
6737 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
6738 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006739
6740 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
6741 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
6742 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
6743 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006744 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
6745 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
6746 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
6747 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006748
6749 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
6750 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
6751 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
6752 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006753 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
6754 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
6755 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
6756 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006757
6758 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
6759 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
6760 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
6761 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006762 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
6763 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
6764 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
6765 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006766
6767 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006768 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006769 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006770 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006771 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006772 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006773 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesene5ca04e2008-09-11 02:15:03 +00006774 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006775
6776 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6777 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6778 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6779 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006780 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6781 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6782 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6783 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00006784
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006785 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6786 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6787 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6788 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6789 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6790 BB = EmitAtomicBinary(MI, BB, false, 0);
6791 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6792 BB = EmitAtomicBinary(MI, BB, true, 0);
6793
Evan Cheng32e376f2008-07-12 02:23:19 +00006794 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6795 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6796 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6797
6798 unsigned dest = MI->getOperand(0).getReg();
6799 unsigned ptrA = MI->getOperand(1).getReg();
6800 unsigned ptrB = MI->getOperand(2).getReg();
6801 unsigned oldval = MI->getOperand(3).getReg();
6802 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006803 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00006804
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006805 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6806 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6807 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006808 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006809 F->insert(It, loop1MBB);
6810 F->insert(It, loop2MBB);
6811 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006812 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006813 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006814 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006815 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006816
6817 // thisMBB:
6818 // ...
6819 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006820 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006821
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006822 // loop1MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006823 // l[wd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006824 // cmp[wd] dest, oldval
6825 // bne- midMBB
6826 // loop2MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00006827 // st[wd]cx. newval, ptr
6828 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006829 // b exitBB
6830 // midMBB:
6831 // st[wd]cx. dest, ptr
6832 // exitBB:
6833 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006834 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00006835 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006836 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00006837 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006838 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006839 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6840 BB->addSuccessor(loop2MBB);
6841 BB->addSuccessor(midMBB);
6842
6843 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006844 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng32e376f2008-07-12 02:23:19 +00006845 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006846 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006847 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006848 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006849 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00006850 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006851
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006852 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006853 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00006854 .addReg(dest).addReg(ptrA).addReg(ptrB);
6855 BB->addSuccessor(exitMBB);
6856
Evan Cheng32e376f2008-07-12 02:23:19 +00006857 // exitMBB:
6858 // ...
6859 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00006860 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6861 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6862 // We must use 64-bit registers for addresses when targeting 64-bit,
6863 // since we're actually doing arithmetic on them. Other registers
6864 // can be 32-bit.
6865 bool is64bit = PPCSubTarget.isPPC64();
6866 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6867
6868 unsigned dest = MI->getOperand(0).getReg();
6869 unsigned ptrA = MI->getOperand(1).getReg();
6870 unsigned ptrB = MI->getOperand(2).getReg();
6871 unsigned oldval = MI->getOperand(3).getReg();
6872 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006873 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00006874
6875 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6876 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6877 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6878 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6879 F->insert(It, loop1MBB);
6880 F->insert(It, loop2MBB);
6881 F->insert(It, midMBB);
6882 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006883 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006884 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006885 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006886
6887 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006888 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006889 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6890 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00006891 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6892 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6893 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6894 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6895 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6896 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6897 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6898 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6899 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6900 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6901 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6902 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6903 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6904 unsigned Ptr1Reg;
6905 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00006906 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00006907 // thisMBB:
6908 // ...
6909 // fallthrough --> loopMBB
6910 BB->addSuccessor(loop1MBB);
6911
6912 // The 4-byte load must be aligned, while a char or short may be
6913 // anywhere in the word. Hence all this nasty bookkeeping code.
6914 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6915 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006916 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00006917 // rlwinm ptr, ptr1, 0, 0, 29
6918 // slw newval2, newval, shift
6919 // slw oldval2, oldval,shift
6920 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6921 // slw mask, mask2, shift
6922 // and newval3, newval2, mask
6923 // and oldval3, oldval2, mask
6924 // loop1MBB:
6925 // lwarx tmpDest, ptr
6926 // and tmp, tmpDest, mask
6927 // cmpw tmp, oldval3
6928 // bne- midMBB
6929 // loop2MBB:
6930 // andc tmp2, tmpDest, mask
6931 // or tmp4, tmp2, newval3
6932 // stwcx. tmp4, ptr
6933 // bne- loop1MBB
6934 // b exitBB
6935 // midMBB:
6936 // stwcx. tmpDest, ptr
6937 // exitBB:
6938 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006939 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00006940 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006941 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006942 .addReg(ptrA).addReg(ptrB);
6943 } else {
6944 Ptr1Reg = ptrB;
6945 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006946 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006947 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006948 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006949 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6950 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006951 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006952 .addReg(Ptr1Reg).addImm(0).addImm(61);
6953 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006954 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006955 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006956 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006957 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006958 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006959 .addReg(oldval).addReg(ShiftReg);
6960 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006961 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00006962 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006963 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6964 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6965 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00006966 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006967 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006968 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006969 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006970 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006971 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00006972 .addReg(OldVal2Reg).addReg(MaskReg);
6973
6974 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006975 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006976 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006977 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6978 .addReg(TmpDestReg).addReg(MaskReg);
6979 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00006980 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006981 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006982 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6983 BB->addSuccessor(loop2MBB);
6984 BB->addSuccessor(midMBB);
6985
6986 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006987 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6988 .addReg(TmpDestReg).addReg(MaskReg);
6989 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6990 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6991 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006992 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006993 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00006994 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006995 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00006996 BB->addSuccessor(loop1MBB);
6997 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006998
Dale Johannesen340d2642008-08-30 00:08:53 +00006999 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007000 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007001 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00007002 BB->addSuccessor(exitMBB);
7003
7004 // exitMBB:
7005 // ...
7006 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00007007 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
7008 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00007009 } else if (MI->getOpcode() == PPC::FADDrtz) {
7010 // This pseudo performs an FADD with rounding mode temporarily forced
7011 // to round-to-zero. We emit this via custom inserter since the FPSCR
7012 // is not modeled at the SelectionDAG level.
7013 unsigned Dest = MI->getOperand(0).getReg();
7014 unsigned Src1 = MI->getOperand(1).getReg();
7015 unsigned Src2 = MI->getOperand(2).getReg();
7016 DebugLoc dl = MI->getDebugLoc();
7017
7018 MachineRegisterInfo &RegInfo = F->getRegInfo();
7019 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
7020
7021 // Save FPSCR value.
7022 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
7023
7024 // Set rounding mode to round-to-zero.
7025 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
7026 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
7027
7028 // Perform addition.
7029 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
7030
7031 // Restore FPSCR value.
7032 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00007033 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
7034 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
7035 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
7036 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
7037 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
7038 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
7039 PPC::ANDIo8 : PPC::ANDIo;
7040 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
7041 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
7042
7043 MachineRegisterInfo &RegInfo = F->getRegInfo();
7044 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
7045 &PPC::GPRCRegClass :
7046 &PPC::G8RCRegClass);
7047
7048 DebugLoc dl = MI->getDebugLoc();
7049 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
7050 .addReg(MI->getOperand(1).getReg()).addImm(1);
7051 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
7052 MI->getOperand(0).getReg())
7053 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Dale Johannesen340d2642008-08-30 00:08:53 +00007054 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007055 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00007056 }
Chris Lattner9b577f12005-08-26 21:23:58 +00007057
Dan Gohman34396292010-07-06 20:24:04 +00007058 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00007059 return BB;
7060}
7061
Chris Lattner4211ca92006-04-14 06:01:58 +00007062//===----------------------------------------------------------------------===//
7063// Target Optimization Hooks
7064//===----------------------------------------------------------------------===//
7065
Hal Finkelb0c810f2013-04-03 17:44:56 +00007066SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
7067 DAGCombinerInfo &DCI) const {
Hal Finkel2e103312013-04-03 04:01:11 +00007068 if (DCI.isAfterLegalizeVectorOps())
7069 return SDValue();
7070
Hal Finkelb0c810f2013-04-03 17:44:56 +00007071 EVT VT = Op.getValueType();
7072
7073 if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) ||
7074 (VT == MVT::f64 && PPCSubTarget.hasFRE()) ||
Hal Finkel27774d92014-03-13 07:58:58 +00007075 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec()) ||
7076 (VT == MVT::v2f64 && PPCSubTarget.hasVSX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00007077
7078 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
7079 // For the reciprocal, we need to find the zero of the function:
7080 // F(X) = A X - 1 [which has a zero at X = 1/A]
7081 // =>
7082 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
7083 // does not require additional intermediate precision]
7084
7085 // Convergence is quadratic, so we essentially double the number of digits
7086 // correct after every iteration. The minimum architected relative
7087 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
7088 // 23 digits and double has 52 digits.
7089 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00007090 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00007091 ++Iterations;
7092
7093 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007094 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007095
7096 SDValue FPOne =
Hal Finkelb0c810f2013-04-03 17:44:56 +00007097 DAG.getConstantFP(1.0, VT.getScalarType());
7098 if (VT.isVector()) {
7099 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00007100 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00007101 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel2e103312013-04-03 04:01:11 +00007102 FPOne, FPOne, FPOne, FPOne);
7103 }
7104
Hal Finkelb0c810f2013-04-03 17:44:56 +00007105 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007106 DCI.AddToWorklist(Est.getNode());
7107
7108 // Newton iterations: Est = Est + Est (1 - Arg * Est)
7109 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007110 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00007111 DCI.AddToWorklist(NewEst.getNode());
7112
Hal Finkelb0c810f2013-04-03 17:44:56 +00007113 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007114 DCI.AddToWorklist(NewEst.getNode());
7115
Hal Finkelb0c810f2013-04-03 17:44:56 +00007116 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007117 DCI.AddToWorklist(NewEst.getNode());
7118
Hal Finkelb0c810f2013-04-03 17:44:56 +00007119 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007120 DCI.AddToWorklist(Est.getNode());
7121 }
7122
7123 return Est;
7124 }
7125
7126 return SDValue();
7127}
7128
Hal Finkelb0c810f2013-04-03 17:44:56 +00007129SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel2e103312013-04-03 04:01:11 +00007130 DAGCombinerInfo &DCI) const {
7131 if (DCI.isAfterLegalizeVectorOps())
7132 return SDValue();
7133
Hal Finkelb0c810f2013-04-03 17:44:56 +00007134 EVT VT = Op.getValueType();
7135
7136 if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) ||
7137 (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) ||
Hal Finkel27774d92014-03-13 07:58:58 +00007138 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec()) ||
7139 (VT == MVT::v2f64 && PPCSubTarget.hasVSX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00007140
7141 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
7142 // For the reciprocal sqrt, we need to find the zero of the function:
7143 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
7144 // =>
7145 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
7146 // As a result, we precompute A/2 prior to the iteration loop.
7147
7148 // Convergence is quadratic, so we essentially double the number of digits
7149 // correct after every iteration. The minimum architected relative
7150 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
7151 // 23 digits and double has 52 digits.
7152 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00007153 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00007154 ++Iterations;
7155
7156 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007157 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007158
Hal Finkelb0c810f2013-04-03 17:44:56 +00007159 SDValue FPThreeHalves =
7160 DAG.getConstantFP(1.5, VT.getScalarType());
7161 if (VT.isVector()) {
7162 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00007163 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00007164 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
7165 FPThreeHalves, FPThreeHalves,
7166 FPThreeHalves, FPThreeHalves);
Hal Finkel2e103312013-04-03 04:01:11 +00007167 }
7168
Hal Finkelb0c810f2013-04-03 17:44:56 +00007169 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007170 DCI.AddToWorklist(Est.getNode());
7171
7172 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
7173 // this entire sequence requires only one FP constant.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007174 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007175 DCI.AddToWorklist(HalfArg.getNode());
7176
Hal Finkelb0c810f2013-04-03 17:44:56 +00007177 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007178 DCI.AddToWorklist(HalfArg.getNode());
7179
7180 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
7181 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007182 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00007183 DCI.AddToWorklist(NewEst.getNode());
7184
Hal Finkelb0c810f2013-04-03 17:44:56 +00007185 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007186 DCI.AddToWorklist(NewEst.getNode());
7187
Hal Finkelb0c810f2013-04-03 17:44:56 +00007188 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007189 DCI.AddToWorklist(NewEst.getNode());
7190
Hal Finkelb0c810f2013-04-03 17:44:56 +00007191 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007192 DCI.AddToWorklist(Est.getNode());
7193 }
7194
7195 return Est;
7196 }
7197
7198 return SDValue();
7199}
7200
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007201// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
7202// not enforce equality of the chain operands.
7203static bool isConsecutiveLS(LSBaseSDNode *LS, LSBaseSDNode *Base,
7204 unsigned Bytes, int Dist,
7205 SelectionDAG &DAG) {
7206 EVT VT = LS->getMemoryVT();
7207 if (VT.getSizeInBits() / 8 != Bytes)
7208 return false;
7209
7210 SDValue Loc = LS->getBasePtr();
7211 SDValue BaseLoc = Base->getBasePtr();
7212 if (Loc.getOpcode() == ISD::FrameIndex) {
7213 if (BaseLoc.getOpcode() != ISD::FrameIndex)
7214 return false;
7215 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7216 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
7217 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
7218 int FS = MFI->getObjectSize(FI);
7219 int BFS = MFI->getObjectSize(BFI);
7220 if (FS != BFS || FS != (int)Bytes) return false;
7221 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
7222 }
7223
7224 // Handle X+C
7225 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
7226 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
7227 return true;
7228
7229 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Craig Topper062a2ba2014-04-25 05:30:21 +00007230 const GlobalValue *GV1 = nullptr;
7231 const GlobalValue *GV2 = nullptr;
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007232 int64_t Offset1 = 0;
7233 int64_t Offset2 = 0;
7234 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
7235 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
7236 if (isGA1 && isGA2 && GV1 == GV2)
7237 return Offset1 == (Offset2 + Dist*Bytes);
7238 return false;
7239}
7240
Hal Finkel7d8a6912013-05-26 18:08:30 +00007241// Return true is there is a nearyby consecutive load to the one provided
7242// (regardless of alignment). We search up and down the chain, looking though
7243// token factors and other loads (but nothing else). As a result, a true
7244// results indicates that it is safe to create a new consecutive load adjacent
7245// to the load provided.
7246static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
7247 SDValue Chain = LD->getChain();
7248 EVT VT = LD->getMemoryVT();
7249
7250 SmallSet<SDNode *, 16> LoadRoots;
7251 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
7252 SmallSet<SDNode *, 16> Visited;
7253
7254 // First, search up the chain, branching to follow all token-factor operands.
7255 // If we find a consecutive load, then we're done, otherwise, record all
7256 // nodes just above the top-level loads and token factors.
7257 while (!Queue.empty()) {
7258 SDNode *ChainNext = Queue.pop_back_val();
7259 if (!Visited.insert(ChainNext))
7260 continue;
7261
7262 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007263 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007264 return true;
7265
7266 if (!Visited.count(ChainLD->getChain().getNode()))
7267 Queue.push_back(ChainLD->getChain().getNode());
7268 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
7269 for (SDNode::op_iterator O = ChainNext->op_begin(),
7270 OE = ChainNext->op_end(); O != OE; ++O)
7271 if (!Visited.count(O->getNode()))
7272 Queue.push_back(O->getNode());
7273 } else
7274 LoadRoots.insert(ChainNext);
7275 }
7276
7277 // Second, search down the chain, starting from the top-level nodes recorded
7278 // in the first phase. These top-level nodes are the nodes just above all
7279 // loads and token factors. Starting with their uses, recursively look though
7280 // all loads (just the chain uses) and token factors to find a consecutive
7281 // load.
7282 Visited.clear();
7283 Queue.clear();
7284
7285 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
7286 IE = LoadRoots.end(); I != IE; ++I) {
7287 Queue.push_back(*I);
7288
7289 while (!Queue.empty()) {
7290 SDNode *LoadRoot = Queue.pop_back_val();
7291 if (!Visited.insert(LoadRoot))
7292 continue;
7293
7294 if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007295 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007296 return true;
7297
7298 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
7299 UE = LoadRoot->use_end(); UI != UE; ++UI)
7300 if (((isa<LoadSDNode>(*UI) &&
7301 cast<LoadSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
7302 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
7303 Queue.push_back(*UI);
7304 }
7305 }
7306
7307 return false;
7308}
7309
Hal Finkel940ab932014-02-28 00:27:01 +00007310SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
7311 DAGCombinerInfo &DCI) const {
7312 SelectionDAG &DAG = DCI.DAG;
7313 SDLoc dl(N);
7314
7315 assert(PPCSubTarget.useCRBits() &&
7316 "Expecting to be tracking CR bits");
7317 // If we're tracking CR bits, we need to be careful that we don't have:
7318 // trunc(binary-ops(zext(x), zext(y)))
7319 // or
7320 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
7321 // such that we're unnecessarily moving things into GPRs when it would be
7322 // better to keep them in CR bits.
7323
7324 // Note that trunc here can be an actual i1 trunc, or can be the effective
7325 // truncation that comes from a setcc or select_cc.
7326 if (N->getOpcode() == ISD::TRUNCATE &&
7327 N->getValueType(0) != MVT::i1)
7328 return SDValue();
7329
7330 if (N->getOperand(0).getValueType() != MVT::i32 &&
7331 N->getOperand(0).getValueType() != MVT::i64)
7332 return SDValue();
7333
7334 if (N->getOpcode() == ISD::SETCC ||
7335 N->getOpcode() == ISD::SELECT_CC) {
7336 // If we're looking at a comparison, then we need to make sure that the
7337 // high bits (all except for the first) don't matter the result.
7338 ISD::CondCode CC =
7339 cast<CondCodeSDNode>(N->getOperand(
7340 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
7341 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
7342
7343 if (ISD::isSignedIntSetCC(CC)) {
7344 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
7345 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
7346 return SDValue();
7347 } else if (ISD::isUnsignedIntSetCC(CC)) {
7348 if (!DAG.MaskedValueIsZero(N->getOperand(0),
7349 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
7350 !DAG.MaskedValueIsZero(N->getOperand(1),
7351 APInt::getHighBitsSet(OpBits, OpBits-1)))
7352 return SDValue();
7353 } else {
7354 // This is neither a signed nor an unsigned comparison, just make sure
7355 // that the high bits are equal.
7356 APInt Op1Zero, Op1One;
7357 APInt Op2Zero, Op2One;
7358 DAG.ComputeMaskedBits(N->getOperand(0), Op1Zero, Op1One);
7359 DAG.ComputeMaskedBits(N->getOperand(1), Op2Zero, Op2One);
7360
7361 // We don't really care about what is known about the first bit (if
7362 // anything), so clear it in all masks prior to comparing them.
7363 Op1Zero.clearBit(0); Op1One.clearBit(0);
7364 Op2Zero.clearBit(0); Op2One.clearBit(0);
7365
7366 if (Op1Zero != Op2Zero || Op1One != Op2One)
7367 return SDValue();
7368 }
7369 }
7370
7371 // We now know that the higher-order bits are irrelevant, we just need to
7372 // make sure that all of the intermediate operations are bit operations, and
7373 // all inputs are extensions.
7374 if (N->getOperand(0).getOpcode() != ISD::AND &&
7375 N->getOperand(0).getOpcode() != ISD::OR &&
7376 N->getOperand(0).getOpcode() != ISD::XOR &&
7377 N->getOperand(0).getOpcode() != ISD::SELECT &&
7378 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
7379 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
7380 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
7381 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
7382 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
7383 return SDValue();
7384
7385 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
7386 N->getOperand(1).getOpcode() != ISD::AND &&
7387 N->getOperand(1).getOpcode() != ISD::OR &&
7388 N->getOperand(1).getOpcode() != ISD::XOR &&
7389 N->getOperand(1).getOpcode() != ISD::SELECT &&
7390 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
7391 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
7392 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
7393 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
7394 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
7395 return SDValue();
7396
7397 SmallVector<SDValue, 4> Inputs;
7398 SmallVector<SDValue, 8> BinOps, PromOps;
7399 SmallPtrSet<SDNode *, 16> Visited;
7400
7401 for (unsigned i = 0; i < 2; ++i) {
7402 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7403 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7404 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7405 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7406 isa<ConstantSDNode>(N->getOperand(i)))
7407 Inputs.push_back(N->getOperand(i));
7408 else
7409 BinOps.push_back(N->getOperand(i));
7410
7411 if (N->getOpcode() == ISD::TRUNCATE)
7412 break;
7413 }
7414
7415 // Visit all inputs, collect all binary operations (and, or, xor and
7416 // select) that are all fed by extensions.
7417 while (!BinOps.empty()) {
7418 SDValue BinOp = BinOps.back();
7419 BinOps.pop_back();
7420
7421 if (!Visited.insert(BinOp.getNode()))
7422 continue;
7423
7424 PromOps.push_back(BinOp);
7425
7426 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7427 // The condition of the select is not promoted.
7428 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7429 continue;
7430 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7431 continue;
7432
7433 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7434 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7435 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7436 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7437 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7438 Inputs.push_back(BinOp.getOperand(i));
7439 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7440 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7441 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7442 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7443 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
7444 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7445 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7446 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7447 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
7448 BinOps.push_back(BinOp.getOperand(i));
7449 } else {
7450 // We have an input that is not an extension or another binary
7451 // operation; we'll abort this transformation.
7452 return SDValue();
7453 }
7454 }
7455 }
7456
7457 // Make sure that this is a self-contained cluster of operations (which
7458 // is not quite the same thing as saying that everything has only one
7459 // use).
7460 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7461 if (isa<ConstantSDNode>(Inputs[i]))
7462 continue;
7463
7464 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7465 UE = Inputs[i].getNode()->use_end();
7466 UI != UE; ++UI) {
7467 SDNode *User = *UI;
7468 if (User != N && !Visited.count(User))
7469 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007470
7471 // Make sure that we're not going to promote the non-output-value
7472 // operand(s) or SELECT or SELECT_CC.
7473 // FIXME: Although we could sometimes handle this, and it does occur in
7474 // practice that one of the condition inputs to the select is also one of
7475 // the outputs, we currently can't deal with this.
7476 if (User->getOpcode() == ISD::SELECT) {
7477 if (User->getOperand(0) == Inputs[i])
7478 return SDValue();
7479 } else if (User->getOpcode() == ISD::SELECT_CC) {
7480 if (User->getOperand(0) == Inputs[i] ||
7481 User->getOperand(1) == Inputs[i])
7482 return SDValue();
7483 }
Hal Finkel940ab932014-02-28 00:27:01 +00007484 }
7485 }
7486
7487 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7488 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7489 UE = PromOps[i].getNode()->use_end();
7490 UI != UE; ++UI) {
7491 SDNode *User = *UI;
7492 if (User != N && !Visited.count(User))
7493 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007494
7495 // Make sure that we're not going to promote the non-output-value
7496 // operand(s) or SELECT or SELECT_CC.
7497 // FIXME: Although we could sometimes handle this, and it does occur in
7498 // practice that one of the condition inputs to the select is also one of
7499 // the outputs, we currently can't deal with this.
7500 if (User->getOpcode() == ISD::SELECT) {
7501 if (User->getOperand(0) == PromOps[i])
7502 return SDValue();
7503 } else if (User->getOpcode() == ISD::SELECT_CC) {
7504 if (User->getOperand(0) == PromOps[i] ||
7505 User->getOperand(1) == PromOps[i])
7506 return SDValue();
7507 }
Hal Finkel940ab932014-02-28 00:27:01 +00007508 }
7509 }
7510
7511 // Replace all inputs with the extension operand.
7512 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7513 // Constants may have users outside the cluster of to-be-promoted nodes,
7514 // and so we need to replace those as we do the promotions.
7515 if (isa<ConstantSDNode>(Inputs[i]))
7516 continue;
7517 else
7518 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
7519 }
7520
7521 // Replace all operations (these are all the same, but have a different
7522 // (i1) return type). DAG.getNode will validate that the types of
7523 // a binary operator match, so go through the list in reverse so that
7524 // we've likely promoted both operands first. Any intermediate truncations or
7525 // extensions disappear.
7526 while (!PromOps.empty()) {
7527 SDValue PromOp = PromOps.back();
7528 PromOps.pop_back();
7529
7530 if (PromOp.getOpcode() == ISD::TRUNCATE ||
7531 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
7532 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
7533 PromOp.getOpcode() == ISD::ANY_EXTEND) {
7534 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
7535 PromOp.getOperand(0).getValueType() != MVT::i1) {
7536 // The operand is not yet ready (see comment below).
7537 PromOps.insert(PromOps.begin(), PromOp);
7538 continue;
7539 }
7540
7541 SDValue RepValue = PromOp.getOperand(0);
7542 if (isa<ConstantSDNode>(RepValue))
7543 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
7544
7545 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
7546 continue;
7547 }
7548
7549 unsigned C;
7550 switch (PromOp.getOpcode()) {
7551 default: C = 0; break;
7552 case ISD::SELECT: C = 1; break;
7553 case ISD::SELECT_CC: C = 2; break;
7554 }
7555
7556 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7557 PromOp.getOperand(C).getValueType() != MVT::i1) ||
7558 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7559 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
7560 // The to-be-promoted operands of this node have not yet been
7561 // promoted (this should be rare because we're going through the
7562 // list backward, but if one of the operands has several users in
7563 // this cluster of to-be-promoted nodes, it is possible).
7564 PromOps.insert(PromOps.begin(), PromOp);
7565 continue;
7566 }
7567
7568 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7569 PromOp.getNode()->op_end());
7570
7571 // If there are any constant inputs, make sure they're replaced now.
7572 for (unsigned i = 0; i < 2; ++i)
7573 if (isa<ConstantSDNode>(Ops[C+i]))
7574 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
7575
7576 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00007577 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00007578 }
7579
7580 // Now we're left with the initial truncation itself.
7581 if (N->getOpcode() == ISD::TRUNCATE)
7582 return N->getOperand(0);
7583
7584 // Otherwise, this is a comparison. The operands to be compared have just
7585 // changed type (to i1), but everything else is the same.
7586 return SDValue(N, 0);
7587}
7588
7589SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
7590 DAGCombinerInfo &DCI) const {
7591 SelectionDAG &DAG = DCI.DAG;
7592 SDLoc dl(N);
7593
Hal Finkel940ab932014-02-28 00:27:01 +00007594 // If we're tracking CR bits, we need to be careful that we don't have:
7595 // zext(binary-ops(trunc(x), trunc(y)))
7596 // or
7597 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
7598 // such that we're unnecessarily moving things into CR bits that can more
7599 // efficiently stay in GPRs. Note that if we're not certain that the high
7600 // bits are set as required by the final extension, we still may need to do
7601 // some masking to get the proper behavior.
7602
Hal Finkel46043ed2014-03-01 21:36:57 +00007603 // This same functionality is important on PPC64 when dealing with
7604 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
7605 // the return values of functions. Because it is so similar, it is handled
7606 // here as well.
7607
Hal Finkel940ab932014-02-28 00:27:01 +00007608 if (N->getValueType(0) != MVT::i32 &&
7609 N->getValueType(0) != MVT::i64)
7610 return SDValue();
7611
Hal Finkel46043ed2014-03-01 21:36:57 +00007612 if (!((N->getOperand(0).getValueType() == MVT::i1 &&
7613 PPCSubTarget.useCRBits()) ||
7614 (N->getOperand(0).getValueType() == MVT::i32 &&
7615 PPCSubTarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +00007616 return SDValue();
7617
7618 if (N->getOperand(0).getOpcode() != ISD::AND &&
7619 N->getOperand(0).getOpcode() != ISD::OR &&
7620 N->getOperand(0).getOpcode() != ISD::XOR &&
7621 N->getOperand(0).getOpcode() != ISD::SELECT &&
7622 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
7623 return SDValue();
7624
7625 SmallVector<SDValue, 4> Inputs;
7626 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
7627 SmallPtrSet<SDNode *, 16> Visited;
7628
7629 // Visit all inputs, collect all binary operations (and, or, xor and
7630 // select) that are all fed by truncations.
7631 while (!BinOps.empty()) {
7632 SDValue BinOp = BinOps.back();
7633 BinOps.pop_back();
7634
7635 if (!Visited.insert(BinOp.getNode()))
7636 continue;
7637
7638 PromOps.push_back(BinOp);
7639
7640 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7641 // The condition of the select is not promoted.
7642 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7643 continue;
7644 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7645 continue;
7646
7647 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7648 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7649 Inputs.push_back(BinOp.getOperand(i));
7650 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7651 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7652 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7653 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7654 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
7655 BinOps.push_back(BinOp.getOperand(i));
7656 } else {
7657 // We have an input that is not a truncation or another binary
7658 // operation; we'll abort this transformation.
7659 return SDValue();
7660 }
7661 }
7662 }
7663
7664 // Make sure that this is a self-contained cluster of operations (which
7665 // is not quite the same thing as saying that everything has only one
7666 // use).
7667 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7668 if (isa<ConstantSDNode>(Inputs[i]))
7669 continue;
7670
7671 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7672 UE = Inputs[i].getNode()->use_end();
7673 UI != UE; ++UI) {
7674 SDNode *User = *UI;
7675 if (User != N && !Visited.count(User))
7676 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007677
7678 // Make sure that we're not going to promote the non-output-value
7679 // operand(s) or SELECT or SELECT_CC.
7680 // FIXME: Although we could sometimes handle this, and it does occur in
7681 // practice that one of the condition inputs to the select is also one of
7682 // the outputs, we currently can't deal with this.
7683 if (User->getOpcode() == ISD::SELECT) {
7684 if (User->getOperand(0) == Inputs[i])
7685 return SDValue();
7686 } else if (User->getOpcode() == ISD::SELECT_CC) {
7687 if (User->getOperand(0) == Inputs[i] ||
7688 User->getOperand(1) == Inputs[i])
7689 return SDValue();
7690 }
Hal Finkel940ab932014-02-28 00:27:01 +00007691 }
7692 }
7693
7694 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7695 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7696 UE = PromOps[i].getNode()->use_end();
7697 UI != UE; ++UI) {
7698 SDNode *User = *UI;
7699 if (User != N && !Visited.count(User))
7700 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007701
7702 // Make sure that we're not going to promote the non-output-value
7703 // operand(s) or SELECT or SELECT_CC.
7704 // FIXME: Although we could sometimes handle this, and it does occur in
7705 // practice that one of the condition inputs to the select is also one of
7706 // the outputs, we currently can't deal with this.
7707 if (User->getOpcode() == ISD::SELECT) {
7708 if (User->getOperand(0) == PromOps[i])
7709 return SDValue();
7710 } else if (User->getOpcode() == ISD::SELECT_CC) {
7711 if (User->getOperand(0) == PromOps[i] ||
7712 User->getOperand(1) == PromOps[i])
7713 return SDValue();
7714 }
Hal Finkel940ab932014-02-28 00:27:01 +00007715 }
7716 }
7717
Hal Finkel46043ed2014-03-01 21:36:57 +00007718 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +00007719 bool ReallyNeedsExt = false;
7720 if (N->getOpcode() != ISD::ANY_EXTEND) {
7721 // If all of the inputs are not already sign/zero extended, then
7722 // we'll still need to do that at the end.
7723 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7724 if (isa<ConstantSDNode>(Inputs[i]))
7725 continue;
7726
7727 unsigned OpBits =
7728 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +00007729 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
7730
Hal Finkel940ab932014-02-28 00:27:01 +00007731 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
7732 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00007733 APInt::getHighBitsSet(OpBits,
7734 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +00007735 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +00007736 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
7737 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +00007738 ReallyNeedsExt = true;
7739 break;
7740 }
7741 }
7742 }
7743
7744 // Replace all inputs, either with the truncation operand, or a
7745 // truncation or extension to the final output type.
7746 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7747 // Constant inputs need to be replaced with the to-be-promoted nodes that
7748 // use them because they might have users outside of the cluster of
7749 // promoted nodes.
7750 if (isa<ConstantSDNode>(Inputs[i]))
7751 continue;
7752
7753 SDValue InSrc = Inputs[i].getOperand(0);
7754 if (Inputs[i].getValueType() == N->getValueType(0))
7755 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
7756 else if (N->getOpcode() == ISD::SIGN_EXTEND)
7757 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7758 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
7759 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7760 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7761 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
7762 else
7763 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
7764 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
7765 }
7766
7767 // Replace all operations (these are all the same, but have a different
7768 // (promoted) return type). DAG.getNode will validate that the types of
7769 // a binary operator match, so go through the list in reverse so that
7770 // we've likely promoted both operands first.
7771 while (!PromOps.empty()) {
7772 SDValue PromOp = PromOps.back();
7773 PromOps.pop_back();
7774
7775 unsigned C;
7776 switch (PromOp.getOpcode()) {
7777 default: C = 0; break;
7778 case ISD::SELECT: C = 1; break;
7779 case ISD::SELECT_CC: C = 2; break;
7780 }
7781
7782 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7783 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
7784 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7785 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
7786 // The to-be-promoted operands of this node have not yet been
7787 // promoted (this should be rare because we're going through the
7788 // list backward, but if one of the operands has several users in
7789 // this cluster of to-be-promoted nodes, it is possible).
7790 PromOps.insert(PromOps.begin(), PromOp);
7791 continue;
7792 }
7793
7794 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7795 PromOp.getNode()->op_end());
7796
7797 // If this node has constant inputs, then they'll need to be promoted here.
7798 for (unsigned i = 0; i < 2; ++i) {
7799 if (!isa<ConstantSDNode>(Ops[C+i]))
7800 continue;
7801 if (Ops[C+i].getValueType() == N->getValueType(0))
7802 continue;
7803
7804 if (N->getOpcode() == ISD::SIGN_EXTEND)
7805 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7806 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7807 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7808 else
7809 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
7810 }
7811
7812 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00007813 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00007814 }
7815
7816 // Now we're left with the initial extension itself.
7817 if (!ReallyNeedsExt)
7818 return N->getOperand(0);
7819
Hal Finkel46043ed2014-03-01 21:36:57 +00007820 // To zero extend, just mask off everything except for the first bit (in the
7821 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +00007822 if (N->getOpcode() == ISD::ZERO_EXTEND)
7823 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00007824 DAG.getConstant(APInt::getLowBitsSet(
7825 N->getValueSizeInBits(0), PromBits),
7826 N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +00007827
7828 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
7829 "Invalid extension type");
7830 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0));
7831 SDValue ShiftCst =
Hal Finkel46043ed2014-03-01 21:36:57 +00007832 DAG.getConstant(N->getValueSizeInBits(0)-PromBits, ShiftAmountTy);
Hal Finkel940ab932014-02-28 00:27:01 +00007833 return DAG.getNode(ISD::SRA, dl, N->getValueType(0),
7834 DAG.getNode(ISD::SHL, dl, N->getValueType(0),
7835 N->getOperand(0), ShiftCst), ShiftCst);
7836}
7837
Duncan Sandsdc2dac12008-11-24 14:53:14 +00007838SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
7839 DAGCombinerInfo &DCI) const {
Dan Gohman57c732b2010-04-21 01:34:56 +00007840 const TargetMachine &TM = getTargetMachine();
Chris Lattnerf4184352006-03-01 04:57:39 +00007841 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007842 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00007843 switch (N->getOpcode()) {
7844 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00007845 case PPCISD::SHL:
7846 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007847 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007848 return N->getOperand(0);
7849 }
7850 break;
7851 case PPCISD::SRL:
7852 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007853 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007854 return N->getOperand(0);
7855 }
7856 break;
7857 case PPCISD::SRA:
7858 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00007859 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00007860 C->isAllOnesValue()) // -1 >>s V -> -1.
7861 return N->getOperand(0);
7862 }
7863 break;
Hal Finkel940ab932014-02-28 00:27:01 +00007864 case ISD::SIGN_EXTEND:
7865 case ISD::ZERO_EXTEND:
7866 case ISD::ANY_EXTEND:
7867 return DAGCombineExtBoolTrunc(N, DCI);
7868 case ISD::TRUNCATE:
7869 case ISD::SETCC:
7870 case ISD::SELECT_CC:
7871 return DAGCombineTruncBoolExt(N, DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00007872 case ISD::FDIV: {
7873 assert(TM.Options.UnsafeFPMath &&
7874 "Reciprocal estimates require UnsafeFPMath");
Scott Michelcf0da6c2009-02-17 22:15:04 +00007875
Hal Finkel2e103312013-04-03 04:01:11 +00007876 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007877 SDValue RV =
7878 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007879 if (RV.getNode()) {
Hal Finkel2e103312013-04-03 04:01:11 +00007880 DCI.AddToWorklist(RV.getNode());
7881 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7882 N->getOperand(0), RV);
7883 }
Hal Finkelf96c18e2013-04-04 22:44:12 +00007884 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
7885 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7886 SDValue RV =
7887 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7888 DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007889 if (RV.getNode()) {
Hal Finkelf96c18e2013-04-04 22:44:12 +00007890 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007891 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007892 N->getValueType(0), RV);
7893 DCI.AddToWorklist(RV.getNode());
7894 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7895 N->getOperand(0), RV);
7896 }
7897 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
7898 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
7899 SDValue RV =
7900 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
7901 DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007902 if (RV.getNode()) {
Hal Finkelf96c18e2013-04-04 22:44:12 +00007903 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007904 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00007905 N->getValueType(0), RV,
7906 N->getOperand(1).getOperand(1));
7907 DCI.AddToWorklist(RV.getNode());
7908 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7909 N->getOperand(0), RV);
7910 }
Hal Finkel2e103312013-04-03 04:01:11 +00007911 }
7912
Hal Finkelb0c810f2013-04-03 17:44:56 +00007913 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007914 if (RV.getNode()) {
Hal Finkel2e103312013-04-03 04:01:11 +00007915 DCI.AddToWorklist(RV.getNode());
7916 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
7917 N->getOperand(0), RV);
7918 }
7919
7920 }
7921 break;
7922 case ISD::FSQRT: {
7923 assert(TM.Options.UnsafeFPMath &&
7924 "Reciprocal estimates require UnsafeFPMath");
7925
7926 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
7927 // reciprocal sqrt.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007928 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007929 if (RV.getNode()) {
Hal Finkel2e103312013-04-03 04:01:11 +00007930 DCI.AddToWorklist(RV.getNode());
Hal Finkelb0c810f2013-04-03 17:44:56 +00007931 RV = DAGCombineFastRecip(RV, DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00007932 if (RV.getNode()) {
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007933 // Unfortunately, RV is now NaN if the input was exactly 0. Select out
7934 // this case and force the answer to 0.
7935
7936 EVT VT = RV.getValueType();
7937
7938 SDValue Zero = DAG.getConstantFP(0.0, VT.getScalarType());
7939 if (VT.isVector()) {
7940 assert(VT.getVectorNumElements() == 4 && "Unknown vector type");
7941 Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Zero, Zero, Zero, Zero);
7942 }
7943
7944 SDValue ZeroCmp =
7945 DAG.getSetCC(dl, getSetCCResultType(*DAG.getContext(), VT),
7946 N->getOperand(0), Zero, ISD::SETEQ);
7947 DCI.AddToWorklist(ZeroCmp.getNode());
7948 DCI.AddToWorklist(RV.getNode());
7949
7950 RV = DAG.getNode(VT.isVector() ? ISD::VSELECT : ISD::SELECT, dl, VT,
7951 ZeroCmp, Zero, RV);
Hal Finkel2e103312013-04-03 04:01:11 +00007952 return RV;
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00007953 }
Hal Finkel2e103312013-04-03 04:01:11 +00007954 }
7955
7956 }
7957 break;
Chris Lattnerf4184352006-03-01 04:57:39 +00007958 case ISD::SINT_TO_FP:
Chris Lattnera35f3062006-06-16 17:34:12 +00007959 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007960 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
7961 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
7962 // We allow the src/dst to be either f32/f64, but the intermediate
7963 // type must be i64.
Owen Anderson9f944592009-08-11 20:47:22 +00007964 if (N->getOperand(0).getValueType() == MVT::i64 &&
7965 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007966 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007967 if (Val.getValueType() == MVT::f32) {
7968 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007969 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007970 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007971
Owen Anderson9f944592009-08-11 20:47:22 +00007972 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007973 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007974 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007975 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00007976 if (N->getValueType(0) == MVT::f32) {
7977 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner72733e52008-01-17 07:00:52 +00007978 DAG.getIntPtrConstant(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007979 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00007980 }
7981 return Val;
Owen Anderson9f944592009-08-11 20:47:22 +00007982 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattner4a66d692006-03-22 05:30:33 +00007983 // If the intermediate type is i32, we can avoid the load/store here
7984 // too.
Chris Lattnerf4184352006-03-01 04:57:39 +00007985 }
Chris Lattnerf4184352006-03-01 04:57:39 +00007986 }
7987 }
7988 break;
Chris Lattner27f53452006-03-01 05:50:56 +00007989 case ISD::STORE:
7990 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
7991 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnerf5b46f72008-01-18 16:54:56 +00007992 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00007993 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00007994 N->getOperand(1).getValueType() == MVT::i32 &&
7995 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007996 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00007997 if (Val.getValueType() == MVT::f32) {
7998 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007999 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008000 }
Owen Anderson9f944592009-08-11 20:47:22 +00008001 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008002 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008003
Hal Finkel60c75102013-04-01 15:37:53 +00008004 SDValue Ops[] = {
8005 N->getOperand(0), Val, N->getOperand(2),
8006 DAG.getValueType(N->getOperand(1).getValueType())
8007 };
8008
8009 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00008010 DAG.getVTList(MVT::Other), Ops,
Hal Finkel60c75102013-04-01 15:37:53 +00008011 cast<StoreSDNode>(N)->getMemoryVT(),
8012 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00008013 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008014 return Val;
8015 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008016
Chris Lattnera7976d32006-07-10 20:56:58 +00008017 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00008018 if (cast<StoreSDNode>(N)->isUnindexed() &&
8019 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00008020 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00008021 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00008022 N->getOperand(1).getValueType() == MVT::i16 ||
8023 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008024 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008025 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008026 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008027 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00008028 if (BSwapOp.getValueType() == MVT::i16)
8029 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00008030
Dan Gohman48b185d2009-09-25 20:36:54 +00008031 SDValue Ops[] = {
8032 N->getOperand(0), BSwapOp, N->getOperand(2),
8033 DAG.getValueType(N->getOperand(1).getValueType())
8034 };
8035 return
8036 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00008037 Ops, cast<StoreSDNode>(N)->getMemoryVT(),
Dan Gohman48b185d2009-09-25 20:36:54 +00008038 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008039 }
8040 break;
Hal Finkelcf2e9082013-05-24 23:00:14 +00008041 case ISD::LOAD: {
8042 LoadSDNode *LD = cast<LoadSDNode>(N);
8043 EVT VT = LD->getValueType(0);
8044 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
8045 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
8046 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
8047 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
Hal Finkel40c34782013-09-15 22:09:58 +00008048 (VT == MVT::v16i8 || VT == MVT::v8i16 ||
8049 VT == MVT::v4i32 || VT == MVT::v4f32) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +00008050 LD->getAlignment() < ABIAlignment) {
8051 // This is a type-legal unaligned Altivec load.
8052 SDValue Chain = LD->getChain();
8053 SDValue Ptr = LD->getBasePtr();
8054
8055 // This implements the loading of unaligned vectors as described in
8056 // the venerable Apple Velocity Engine overview. Specifically:
8057 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
8058 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
8059 //
8060 // The general idea is to expand a sequence of one or more unaligned
8061 // loads into a alignment-based permutation-control instruction (lvsl),
8062 // a series of regular vector loads (which always truncate their
8063 // input address to an aligned address), and a series of permutations.
8064 // The results of these permutations are the requested loaded values.
8065 // The trick is that the last "extra" load is not taken from the address
8066 // you might suspect (sizeof(vector) bytes after the last requested
8067 // load), but rather sizeof(vector) - 1 bytes after the last
8068 // requested vector. The point of this is to avoid a page fault if the
Alp Tokercb402912014-01-24 17:20:08 +00008069 // base address happened to be aligned. This works because if the base
Hal Finkelcf2e9082013-05-24 23:00:14 +00008070 // address is aligned, then adding less than a full vector length will
8071 // cause the last vector in the sequence to be (re)loaded. Otherwise,
8072 // the next vector will be fetched as you might suspect was necessary.
8073
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008074 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00008075 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008076 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
8077 // optimization later.
Hal Finkelcf2e9082013-05-24 23:00:14 +00008078 SDValue PermCntl = BuildIntrinsicOp(Intrinsic::ppc_altivec_lvsl, Ptr,
8079 DAG, dl, MVT::v16i8);
8080
8081 // Refine the alignment of the original load (a "new" load created here
8082 // which was identical to the first except for the alignment would be
8083 // merged with the existing node regardless).
8084 MachineFunction &MF = DAG.getMachineFunction();
8085 MachineMemOperand *MMO =
8086 MF.getMachineMemOperand(LD->getPointerInfo(),
8087 LD->getMemOperand()->getFlags(),
8088 LD->getMemoryVT().getStoreSize(),
8089 ABIAlignment);
8090 LD->refineAlignment(MMO);
8091 SDValue BaseLoad = SDValue(LD, 0);
8092
8093 // Note that the value of IncOffset (which is provided to the next
8094 // load's pointer info offset value, and thus used to calculate the
8095 // alignment), and the value of IncValue (which is actually used to
8096 // increment the pointer value) are different! This is because we
8097 // require the next load to appear to be aligned, even though it
8098 // is actually offset from the base pointer by a lesser amount.
8099 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +00008100 int IncValue = IncOffset;
8101
8102 // Walk (both up and down) the chain looking for another load at the real
8103 // (aligned) offset (the alignment of the other load does not matter in
8104 // this case). If found, then do not use the offset reduction trick, as
8105 // that will prevent the loads from being later combined (as they would
8106 // otherwise be duplicates).
8107 if (!findConsecutiveLoad(LD, DAG))
8108 --IncValue;
8109
Hal Finkelcf2e9082013-05-24 23:00:14 +00008110 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
8111 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
8112
Hal Finkelcf2e9082013-05-24 23:00:14 +00008113 SDValue ExtraLoad =
8114 DAG.getLoad(VT, dl, Chain, Ptr,
8115 LD->getPointerInfo().getWithOffset(IncOffset),
8116 LD->isVolatile(), LD->isNonTemporal(),
8117 LD->isInvariant(), ABIAlignment);
8118
8119 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8120 BaseLoad.getValue(1), ExtraLoad.getValue(1));
8121
8122 if (BaseLoad.getValueType() != MVT::v4i32)
8123 BaseLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, BaseLoad);
8124
8125 if (ExtraLoad.getValueType() != MVT::v4i32)
8126 ExtraLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ExtraLoad);
8127
8128 SDValue Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
8129 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
8130
8131 if (VT != MVT::v4i32)
8132 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
8133
8134 // Now we need to be really careful about how we update the users of the
8135 // original load. We cannot just call DCI.CombineTo (or
8136 // DAG.ReplaceAllUsesWith for that matter), because the load still has
8137 // uses created here (the permutation for example) that need to stay.
8138 SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
8139 while (UI != UE) {
8140 SDUse &Use = UI.getUse();
8141 SDNode *User = *UI;
8142 // Note: BaseLoad is checked here because it might not be N, but a
8143 // bitcast of N.
8144 if (User == Perm.getNode() || User == BaseLoad.getNode() ||
8145 User == TF.getNode() || Use.getResNo() > 1) {
8146 ++UI;
8147 continue;
8148 }
8149
8150 SDValue To = Use.getResNo() ? TF : Perm;
8151 ++UI;
8152
8153 SmallVector<SDValue, 8> Ops;
8154 for (SDNode::op_iterator O = User->op_begin(),
8155 OE = User->op_end(); O != OE; ++O) {
8156 if (*O == Use)
8157 Ops.push_back(To);
8158 else
8159 Ops.push_back(*O);
8160 }
8161
8162 DAG.UpdateNodeOperands(User, Ops.data(), Ops.size());
8163 }
8164
8165 return SDValue(N, 0);
8166 }
8167 }
8168 break;
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008169 case ISD::INTRINSIC_WO_CHAIN:
8170 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() ==
8171 Intrinsic::ppc_altivec_lvsl &&
8172 N->getOperand(1)->getOpcode() == ISD::ADD) {
8173 SDValue Add = N->getOperand(1);
8174
8175 if (DAG.MaskedValueIsZero(Add->getOperand(1),
8176 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
8177 Add.getValueType().getScalarType().getSizeInBits()))) {
8178 SDNode *BasePtr = Add->getOperand(0).getNode();
8179 for (SDNode::use_iterator UI = BasePtr->use_begin(),
8180 UE = BasePtr->use_end(); UI != UE; ++UI) {
8181 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8182 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
8183 Intrinsic::ppc_altivec_lvsl) {
8184 // We've found another LVSL, and this address if an aligned
8185 // multiple of that one. The results will be the same, so use the
8186 // one we've just found instead.
8187
8188 return SDValue(*UI, 0);
8189 }
8190 }
8191 }
8192 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +00008193
8194 break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008195 case ISD::BSWAP:
8196 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008197 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +00008198 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008199 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
8200 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008201 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008202 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008203 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00008204 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +00008205 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008206 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +00008207 LD->getChain(), // Chain
8208 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008209 DAG.getValueType(N->getValueType(0)) // VT
8210 };
Dan Gohman48b185d2009-09-25 20:36:54 +00008211 SDValue BSLoad =
8212 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +00008213 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
8214 MVT::i64 : MVT::i32, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00008215 Ops, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008216
Scott Michelcf0da6c2009-02-17 22:15:04 +00008217 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008218 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +00008219 if (N->getValueType(0) == MVT::i16)
8220 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008221
Chris Lattnera7976d32006-07-10 20:56:58 +00008222 // First, combine the bswap away. This makes the value produced by the
8223 // load dead.
8224 DCI.CombineTo(N, ResVal);
8225
8226 // Next, combine the load away, we give it a bogus result value but a real
8227 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008228 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00008229
Chris Lattnera7976d32006-07-10 20:56:58 +00008230 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008231 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008232 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008233
Chris Lattner27f53452006-03-01 05:50:56 +00008234 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008235 case PPCISD::VCMP: {
8236 // If a VCMPo node already exists with exactly the same operands as this
8237 // node, use its result instead of this node (VCMPo computes both a CR6 and
8238 // a normal output).
8239 //
8240 if (!N->getOperand(0).hasOneUse() &&
8241 !N->getOperand(1).hasOneUse() &&
8242 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00008243
Chris Lattnerd4058a52006-03-31 06:02:07 +00008244 // Scan all of the users of the LHS, looking for VCMPo's that match.
Craig Topper062a2ba2014-04-25 05:30:21 +00008245 SDNode *VCMPoNode = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008246
Gabor Greiff304a7a2008-08-28 21:40:38 +00008247 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +00008248 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
8249 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008250 if (UI->getOpcode() == PPCISD::VCMPo &&
8251 UI->getOperand(1) == N->getOperand(1) &&
8252 UI->getOperand(2) == N->getOperand(2) &&
8253 UI->getOperand(0) == N->getOperand(0)) {
8254 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008255 break;
8256 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008257
Chris Lattner518834c2006-04-18 18:28:22 +00008258 // If there is no VCMPo node, or if the flag value has a single use, don't
8259 // transform this.
8260 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
8261 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008262
8263 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +00008264 // chain, this transformation is more complex. Note that multiple things
8265 // could use the value result, which we should ignore.
Craig Topper062a2ba2014-04-25 05:30:21 +00008266 SDNode *FlagUser = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008267 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Craig Topper062a2ba2014-04-25 05:30:21 +00008268 FlagUser == nullptr; ++UI) {
Chris Lattner518834c2006-04-18 18:28:22 +00008269 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008270 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +00008271 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008272 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +00008273 FlagUser = User;
8274 break;
8275 }
8276 }
8277 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008278
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008279 // If the user is a MFOCRF instruction, we know this is safe.
8280 // Otherwise we give up for right now.
8281 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008282 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +00008283 }
8284 break;
8285 }
Hal Finkel940ab932014-02-28 00:27:01 +00008286 case ISD::BRCOND: {
8287 SDValue Cond = N->getOperand(1);
8288 SDValue Target = N->getOperand(2);
8289
8290 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8291 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
8292 Intrinsic::ppc_is_decremented_ctr_nonzero) {
8293
8294 // We now need to make the intrinsic dead (it cannot be instruction
8295 // selected).
8296 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
8297 assert(Cond.getNode()->hasOneUse() &&
8298 "Counter decrement has more than one use");
8299
8300 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
8301 N->getOperand(0), Target);
8302 }
8303 }
8304 break;
Chris Lattner9754d142006-04-18 17:59:36 +00008305 case ISD::BR_CC: {
8306 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008307 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +00008308 // lowering is done pre-legalize, because the legalizer lowers the predicate
8309 // compare down to code that is difficult to reassemble.
8310 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008311 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +00008312
8313 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
8314 // value. If so, pass-through the AND to get to the intrinsic.
8315 if (LHS.getOpcode() == ISD::AND &&
8316 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8317 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
8318 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8319 isa<ConstantSDNode>(LHS.getOperand(1)) &&
8320 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
8321 isZero())
8322 LHS = LHS.getOperand(0);
8323
8324 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8325 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
8326 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8327 isa<ConstantSDNode>(RHS)) {
8328 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
8329 "Counter decrement comparison is not EQ or NE");
8330
8331 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
8332 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
8333 (CC == ISD::SETNE && !Val);
8334
8335 // We now need to make the intrinsic dead (it cannot be instruction
8336 // selected).
8337 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
8338 assert(LHS.getNode()->hasOneUse() &&
8339 "Counter decrement has more than one use");
8340
8341 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
8342 N->getOperand(0), N->getOperand(4));
8343 }
8344
Chris Lattner9754d142006-04-18 17:59:36 +00008345 int CompareOpc;
8346 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008347
Chris Lattner9754d142006-04-18 17:59:36 +00008348 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8349 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
8350 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
8351 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00008352
Chris Lattner9754d142006-04-18 17:59:36 +00008353 // If this is a comparison against something other than 0/1, then we know
8354 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008355 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00008356 if (Val != 0 && Val != 1) {
8357 if (CC == ISD::SETEQ) // Cond never true, remove branch.
8358 return N->getOperand(0);
8359 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +00008360 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +00008361 N->getOperand(0), N->getOperand(4));
8362 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008363
Chris Lattner9754d142006-04-18 17:59:36 +00008364 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008365
Chris Lattner9754d142006-04-18 17:59:36 +00008366 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008367 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008368 LHS.getOperand(2), // LHS of compare
8369 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +00008370 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008371 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00008372 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00008373 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008374
Chris Lattner9754d142006-04-18 17:59:36 +00008375 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008376 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +00008377 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +00008378 default: // Can't happen, don't crash on invalid number though.
8379 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008380 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +00008381 break;
8382 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008383 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +00008384 break;
8385 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008386 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +00008387 break;
8388 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008389 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +00008390 break;
8391 }
8392
Owen Anderson9f944592009-08-11 20:47:22 +00008393 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
8394 DAG.getConstant(CompOpc, MVT::i32),
8395 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +00008396 N->getOperand(4), CompNode.getValue(1));
8397 }
8398 break;
8399 }
Chris Lattnerf4184352006-03-01 04:57:39 +00008400 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008401
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008402 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +00008403}
8404
Chris Lattner4211ca92006-04-14 06:01:58 +00008405//===----------------------------------------------------------------------===//
8406// Inline Assembly Support
8407//===----------------------------------------------------------------------===//
8408
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008409void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Scott Michelcf0da6c2009-02-17 22:15:04 +00008410 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +00008411 APInt &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +00008412 const SelectionDAG &DAG,
Chris Lattnerc5287c02006-04-02 06:26:07 +00008413 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00008414 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +00008415 switch (Op.getOpcode()) {
8416 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008417 case PPCISD::LBRX: {
8418 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +00008419 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +00008420 KnownZero = 0xFFFF0000;
8421 break;
8422 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008423 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00008424 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +00008425 default: break;
8426 case Intrinsic::ppc_altivec_vcmpbfp_p:
8427 case Intrinsic::ppc_altivec_vcmpeqfp_p:
8428 case Intrinsic::ppc_altivec_vcmpequb_p:
8429 case Intrinsic::ppc_altivec_vcmpequh_p:
8430 case Intrinsic::ppc_altivec_vcmpequw_p:
8431 case Intrinsic::ppc_altivec_vcmpgefp_p:
8432 case Intrinsic::ppc_altivec_vcmpgtfp_p:
8433 case Intrinsic::ppc_altivec_vcmpgtsb_p:
8434 case Intrinsic::ppc_altivec_vcmpgtsh_p:
8435 case Intrinsic::ppc_altivec_vcmpgtsw_p:
8436 case Intrinsic::ppc_altivec_vcmpgtub_p:
8437 case Intrinsic::ppc_altivec_vcmpgtuh_p:
8438 case Intrinsic::ppc_altivec_vcmpgtuw_p:
8439 KnownZero = ~1U; // All bits but the low one are known to be zero.
8440 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008441 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008442 }
8443 }
8444}
8445
8446
Chris Lattnerd6855142007-03-25 02:14:49 +00008447/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +00008448/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008449PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00008450PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
8451 if (Constraint.size() == 1) {
8452 switch (Constraint[0]) {
8453 default: break;
8454 case 'b':
8455 case 'r':
8456 case 'f':
8457 case 'v':
8458 case 'y':
8459 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +00008460 case 'Z':
8461 // FIXME: While Z does indicate a memory constraint, it specifically
8462 // indicates an r+r address (used in conjunction with the 'y' modifier
8463 // in the replacement string). Currently, we're forcing the base
8464 // register to be r0 in the asm printer (which is interpreted as zero)
8465 // and forming the complete address in the second register. This is
8466 // suboptimal.
8467 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +00008468 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008469 } else if (Constraint == "wc") { // individual CR bits.
8470 return C_RegisterClass;
Hal Finkel27774d92014-03-13 07:58:58 +00008471 } else if (Constraint == "wa" || Constraint == "wd" ||
8472 Constraint == "wf" || Constraint == "ws") {
8473 return C_RegisterClass; // VSX registers.
Chris Lattnerd6855142007-03-25 02:14:49 +00008474 }
8475 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +00008476}
8477
John Thompsone8360b72010-10-29 17:29:13 +00008478/// Examine constraint type and operand type and determine a weight value.
8479/// This object must already have been set up with the operand type
8480/// and the current alternative constraint selected.
8481TargetLowering::ConstraintWeight
8482PPCTargetLowering::getSingleConstraintMatchWeight(
8483 AsmOperandInfo &info, const char *constraint) const {
8484 ConstraintWeight weight = CW_Invalid;
8485 Value *CallOperandVal = info.CallOperandVal;
8486 // If we don't have a value, we can't do a match,
8487 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00008488 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00008489 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00008490 Type *type = CallOperandVal->getType();
Hal Finkel6aca2372014-03-02 18:23:39 +00008491
John Thompsone8360b72010-10-29 17:29:13 +00008492 // Look at the constraint type.
Hal Finkel6aca2372014-03-02 18:23:39 +00008493 if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
8494 return CW_Register; // an individual CR bit.
Hal Finkel27774d92014-03-13 07:58:58 +00008495 else if ((StringRef(constraint) == "wa" ||
8496 StringRef(constraint) == "wd" ||
8497 StringRef(constraint) == "wf") &&
8498 type->isVectorTy())
8499 return CW_Register;
8500 else if (StringRef(constraint) == "ws" && type->isDoubleTy())
8501 return CW_Register;
Hal Finkel6aca2372014-03-02 18:23:39 +00008502
John Thompsone8360b72010-10-29 17:29:13 +00008503 switch (*constraint) {
8504 default:
8505 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8506 break;
8507 case 'b':
8508 if (type->isIntegerTy())
8509 weight = CW_Register;
8510 break;
8511 case 'f':
8512 if (type->isFloatTy())
8513 weight = CW_Register;
8514 break;
8515 case 'd':
8516 if (type->isDoubleTy())
8517 weight = CW_Register;
8518 break;
8519 case 'v':
8520 if (type->isVectorTy())
8521 weight = CW_Register;
8522 break;
8523 case 'y':
8524 weight = CW_Register;
8525 break;
Hal Finkel4f24c622012-11-05 18:18:42 +00008526 case 'Z':
8527 weight = CW_Memory;
8528 break;
John Thompsone8360b72010-10-29 17:29:13 +00008529 }
8530 return weight;
8531}
8532
Scott Michelcf0da6c2009-02-17 22:15:04 +00008533std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner584a11a2006-11-02 01:44:04 +00008534PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00008535 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +00008536 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +00008537 // GCC RS6000 Constraint Letters
8538 switch (Constraint[0]) {
8539 case 'b': // R1-R31
Hal Finkel638a9fa2013-03-19 18:51:05 +00008540 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
8541 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
8542 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008543 case 'r': // R0-R31
Owen Anderson9f944592009-08-11 20:47:22 +00008544 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +00008545 return std::make_pair(0U, &PPC::G8RCRegClass);
8546 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008547 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008548 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +00008549 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008550 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +00008551 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008552 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008553 case 'v':
Craig Topperabadc662012-04-20 06:31:50 +00008554 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008555 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +00008556 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008557 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008558 } else if (Constraint == "wc") { // an individual CR bit.
8559 return std::make_pair(0U, &PPC::CRBITRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +00008560 } else if (Constraint == "wa" || Constraint == "wd" ||
Hal Finkel19be5062014-03-29 05:29:01 +00008561 Constraint == "wf") {
Hal Finkel27774d92014-03-13 07:58:58 +00008562 return std::make_pair(0U, &PPC::VSRCRegClass);
Hal Finkel19be5062014-03-29 05:29:01 +00008563 } else if (Constraint == "ws") {
8564 return std::make_pair(0U, &PPC::VSFRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008565 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008566
Hal Finkelb176acb2013-08-03 12:25:10 +00008567 std::pair<unsigned, const TargetRegisterClass*> R =
8568 TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8569
8570 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
8571 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
8572 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
8573 // register.
8574 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
8575 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
8576 if (R.first && VT == MVT::i64 && PPCSubTarget.isPPC64() &&
8577 PPC::GPRCRegClass.contains(R.first)) {
8578 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
8579 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +00008580 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +00008581 &PPC::G8RCRegClass);
8582 }
8583
8584 return R;
Chris Lattner01513612006-01-31 19:20:21 +00008585}
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008586
Chris Lattner584a11a2006-11-02 01:44:04 +00008587
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008588/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +00008589/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +00008590void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00008591 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008592 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00008593 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00008594 SDValue Result;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008595
Eric Christopherde9399b2011-06-02 23:16:42 +00008596 // Only support length 1 constraints.
8597 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008598
Eric Christopherde9399b2011-06-02 23:16:42 +00008599 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008600 switch (Letter) {
8601 default: break;
8602 case 'I':
8603 case 'J':
8604 case 'K':
8605 case 'L':
8606 case 'M':
8607 case 'N':
8608 case 'O':
8609 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +00008610 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008611 if (!CST) return; // Must be an immediate to match.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008612 unsigned Value = CST->getZExtValue();
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008613 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00008614 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008615 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008616 if ((short)Value == (int)Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008617 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008618 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008619 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
8620 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008621 if ((short)Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008622 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008623 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008624 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008625 if ((Value >> 16) == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008626 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008627 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008628 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008629 if (Value > 31)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008630 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008631 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008632 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008633 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008634 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008635 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008636 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008637 if (Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008638 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008639 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008640 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00008641 if ((short)-Value == (int)-Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008642 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00008643 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008644 }
8645 break;
8646 }
8647 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008648
Gabor Greiff304a7a2008-08-28 21:40:38 +00008649 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008650 Ops.push_back(Result);
8651 return;
8652 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008653
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008654 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +00008655 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008656}
Evan Cheng2dd2c652006-03-13 23:20:37 +00008657
Chris Lattner1eb94d92007-03-30 23:15:24 +00008658// isLegalAddressingMode - Return true if the addressing mode represented
8659// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008660bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00008661 Type *Ty) const {
Chris Lattner1eb94d92007-03-30 23:15:24 +00008662 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelcf0da6c2009-02-17 22:15:04 +00008663
Chris Lattner1eb94d92007-03-30 23:15:24 +00008664 // PPC allows a sign-extended 16-bit immediate field.
8665 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
8666 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008667
Chris Lattner1eb94d92007-03-30 23:15:24 +00008668 // No global is ever allowed as a base.
8669 if (AM.BaseGV)
8670 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008671
8672 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +00008673 switch (AM.Scale) {
8674 case 0: // "r+i" or just "i", depending on HasBaseReg.
8675 break;
8676 case 1:
8677 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
8678 return false;
8679 // Otherwise we have r+r or r+i.
8680 break;
8681 case 2:
8682 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
8683 return false;
8684 // Allow 2*r as r+r.
8685 break;
Chris Lattner19ccd622007-04-09 22:10:05 +00008686 default:
8687 // No other scales are supported.
8688 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +00008689 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008690
Chris Lattner1eb94d92007-03-30 23:15:24 +00008691 return true;
8692}
8693
Dan Gohman21cea8a2010-04-17 15:26:15 +00008694SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
8695 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00008696 MachineFunction &MF = DAG.getMachineFunction();
8697 MachineFrameInfo *MFI = MF.getFrameInfo();
8698 MFI->setReturnAddressIsTaken(true);
8699
Bill Wendling908bf812014-01-06 00:43:20 +00008700 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008701 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00008702
Andrew Trickef9de2a2013-05-25 02:42:55 +00008703 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008704 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +00008705
Dale Johannesen81bfca72010-05-03 22:59:34 +00008706 // Make sure the function does not optimize away the store of the RA to
8707 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +00008708 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008709 FuncInfo->setLRStoreRequired();
8710 bool isPPC64 = PPCSubTarget.isPPC64();
8711 bool isDarwinABI = PPCSubTarget.isDarwinABI();
8712
8713 if (Depth > 0) {
8714 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8715 SDValue Offset =
Wesley Peck527da1b2010-11-23 03:31:01 +00008716
Anton Korobeynikov2f931282011-01-10 12:39:04 +00008717 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen81bfca72010-05-03 22:59:34 +00008718 isPPC64? MVT::i64 : MVT::i32);
8719 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
8720 DAG.getNode(ISD::ADD, dl, getPointerTy(),
8721 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008722 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008723 }
Chris Lattnerf6a81562007-12-08 06:59:59 +00008724
Chris Lattnerf6a81562007-12-08 06:59:59 +00008725 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008726 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008727 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008728 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +00008729}
8730
Dan Gohman21cea8a2010-04-17 15:26:15 +00008731SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
8732 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008733 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008734 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00008735
Owen Anderson53aa7a92009-08-10 22:56:29 +00008736 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00008737 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008738
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008739 MachineFunction &MF = DAG.getMachineFunction();
8740 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +00008741 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +00008742
8743 // Naked functions never have a frame pointer, and so we use r1. For all
8744 // other functions, this decision must be delayed until during PEI.
8745 unsigned FrameReg;
8746 if (MF.getFunction()->getAttributes().hasAttribute(
8747 AttributeSet::FunctionIndex, Attribute::Naked))
8748 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
8749 else
8750 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
8751
Dale Johannesen81bfca72010-05-03 22:59:34 +00008752 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
8753 PtrVT);
8754 while (Depth--)
8755 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008756 FrameAddr, MachinePointerInfo(), false, false,
8757 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00008758 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00008759}
Dan Gohmanc14e5222008-10-21 03:41:46 +00008760
8761bool
8762PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8763 // The PowerPC target isn't yet aware of offsets.
8764 return false;
8765}
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008766
Evan Chengd9929f02010-04-01 20:10:42 +00008767/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +00008768/// and store operations as a result of memset, memcpy, and memmove
8769/// lowering. If DstAlign is zero that means it's safe to destination
8770/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
8771/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +00008772/// probably because the source does not need to be loaded. If 'IsMemset' is
8773/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
8774/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
8775/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +00008776/// It returns EVT::Other if the type should be determined using generic
8777/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +00008778EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
8779 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00008780 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00008781 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +00008782 MachineFunction &MF) const {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008783 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +00008784 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008785 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00008786 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00008787 }
8788}
Hal Finkel88ed4e32012-04-01 19:23:08 +00008789
Hal Finkel34974ed2014-04-12 21:52:38 +00008790/// \brief Returns true if it is beneficial to convert a load of a constant
8791/// to just the constant itself.
8792bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
8793 Type *Ty) const {
8794 assert(Ty->isIntegerTy());
8795
8796 unsigned BitSize = Ty->getPrimitiveSizeInBits();
8797 if (BitSize == 0 || BitSize > 64)
8798 return false;
8799 return true;
8800}
8801
8802bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
8803 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
8804 return false;
8805 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
8806 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
8807 return NumBits1 == 64 && NumBits2 == 32;
8808}
8809
8810bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
8811 if (!VT1.isInteger() || !VT2.isInteger())
8812 return false;
8813 unsigned NumBits1 = VT1.getSizeInBits();
8814 unsigned NumBits2 = VT2.getSizeInBits();
8815 return NumBits1 == 64 && NumBits2 == 32;
8816}
8817
8818bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
8819 return isInt<16>(Imm) || isUInt<16>(Imm);
8820}
8821
8822bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8823 return isInt<16>(Imm) || isUInt<16>(Imm);
8824}
8825
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008826bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +00008827 unsigned,
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008828 bool *Fast) const {
8829 if (DisablePPCUnaligned)
8830 return false;
8831
8832 // PowerPC supports unaligned memory access for simple non-vector types.
8833 // Although accessing unaligned addresses is not as efficient as accessing
8834 // aligned addresses, it is generally more efficient than manual expansion,
8835 // and generally only traps for software emulation when crossing page
8836 // boundaries.
8837
8838 if (!VT.isSimple())
8839 return false;
8840
Hal Finkel6e28e6a2014-03-26 19:39:09 +00008841 if (VT.getSimpleVT().isVector()) {
8842 if (PPCSubTarget.hasVSX()) {
8843 if (VT != MVT::v2f64 && VT != MVT::v2i64)
8844 return false;
8845 } else {
8846 return false;
8847 }
8848 }
Hal Finkel8d7fbc92013-03-15 15:27:13 +00008849
8850 if (VT == MVT::ppcf128)
8851 return false;
8852
8853 if (Fast)
8854 *Fast = true;
8855
8856 return true;
8857}
8858
Stephen Lin73de7bf2013-07-09 18:16:56 +00008859bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
8860 VT = VT.getScalarType();
8861
Hal Finkel0a479ae2012-06-22 00:49:52 +00008862 if (!VT.isSimple())
8863 return false;
8864
8865 switch (VT.getSimpleVT().SimpleTy) {
8866 case MVT::f32:
8867 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +00008868 return true;
8869 default:
8870 break;
8871 }
8872
8873 return false;
8874}
8875
Hal Finkelb4240ca2014-03-31 17:48:16 +00008876bool
8877PPCTargetLowering::shouldExpandBuildVectorWithShuffles(
8878 EVT VT , unsigned DefinedValues) const {
8879 if (VT == MVT::v2i64)
8880 return false;
8881
8882 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
8883}
8884
Hal Finkel88ed4e32012-04-01 19:23:08 +00008885Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel21442b22013-09-11 23:05:25 +00008886 if (DisableILPPref || PPCSubTarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008887 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +00008888
Hal Finkel4e9f1a82012-06-10 19:32:29 +00008889 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +00008890}
8891
Bill Schmidt0cf702f2013-07-30 00:50:39 +00008892// Create a fast isel object.
8893FastISel *
8894PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
8895 const TargetLibraryInfo *LibInfo) const {
8896 return PPC::createFastISel(FuncInfo, LibInfo);
8897}