blob: bd74a09a876339f7257cfe0450af7d4a21f10c7d [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"
Hal Finkel0d8db462014-05-11 19:29:11 +000021#include "llvm/ADT/StringSwitch.h"
Eric Christopher89958332014-05-31 00:07:32 +000022#include "llvm/ADT/Triple.h"
Chris Lattner4f2e4e02007-03-06 00:59:59 +000023#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner9b577f12005-08-26 21:23:58 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000028#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000029#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/Constants.h"
32#include "llvm/IR/DerivedTypes.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/Intrinsics.h"
Chris Lattnerce645542006-11-10 02:08:47 +000035#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000036#include "llvm/Support/ErrorHandling.h"
Craig Topperb25fda92012-03-17 18:46:09 +000037#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000038#include "llvm/Support/raw_ostream.h"
Craig Topperb25fda92012-03-17 18:46:09 +000039#include "llvm/Target/TargetOptions.h"
Chris Lattnerf22556d2005-08-16 17:14:42 +000040using namespace llvm;
41
Hal Finkel595817e2012-06-04 02:21:00 +000042static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
43cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattnerce645542006-11-10 02:08:47 +000044
Hal Finkel4e9f1a82012-06-10 19:32:29 +000045static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
46cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
47
Hal Finkel8d7fbc92013-03-15 15:27:13 +000048static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
49cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
50
Hal Finkel940ab932014-02-28 00:27:01 +000051// FIXME: Remove this once the bug has been fixed!
52extern cl::opt<bool> ANDIGlueBug;
53
Eric Christopher89958332014-05-31 00:07:32 +000054static TargetLoweringObjectFile *createTLOF(const Triple &TT) {
Eric Christophera84189a2014-06-02 17:29:07 +000055 // If it isn't a Mach-O file then it's going to be a linux ELF
56 // object file.
Eric Christopher89958332014-05-31 00:07:32 +000057 if (TT.isOSDarwin())
Bill Wendlingbbcaa402010-03-15 21:09:38 +000058 return new TargetLoweringObjectFileMachO();
Eric Christophera84189a2014-06-02 17:29:07 +000059
60 return new PPC64LinuxTargetObjectFile();
Chris Lattner5e693ed2009-07-28 03:13:23 +000061}
62
Chris Lattner584a11a2006-11-02 01:44:04 +000063PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Eric Christopher89958332014-05-31 00:07:32 +000064 : TargetLowering(TM, createTLOF(Triple(TM.getTargetTriple()))),
Eric Christopherb1aaebe2014-06-12 22:38:18 +000065 Subtarget(*TM.getSubtargetImpl()) {
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.
Eric Christopherb1aaebe2014-06-12 22:38:18 +000074 bool isPPC64 = Subtarget.isPPC64();
Evan Cheng39e90022012-07-02 22:39:56 +000075 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000100 if (Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000101 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
102
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000103 if (isPPC64 || Subtarget.hasFPCVT()) {
Hal Finkel6a56b212014-03-05 22:14:00 +0000104 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000178 if (!Subtarget.hasFSQRT() &&
Hal Finkel2e103312013-04-03 04:01:11 +0000179 !(TM.Options.UnsafeFPMath &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000180 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000183 if (!Subtarget.hasFSQRT() &&
Hal Finkel2e103312013-04-03 04:01:11 +0000184 !(TM.Options.UnsafeFPMath &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000185 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000188 if (Subtarget.hasFCPSGN()) {
Hal Finkeldbc78e12013-08-19 05:01:02 +0000189 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000196 if (Subtarget.hasFPRND()) {
Hal Finkelc20a08d2013-03-29 08:57:48 +0000197 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +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
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000230 if (!Subtarget.useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +0000231 // 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000243 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000244 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000247 if (!Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000248 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000299 if (Subtarget.isSVR4ABI()) {
Evan Cheng39e90022012-07-02 22:39:56 +0000300 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000319 if (Subtarget.isSVR4ABI() && !isPPC64)
Roman Divackyc3825df2013-07-25 21:36:47 +0000320 // 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +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
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000362 if (Subtarget.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.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000370 if (Subtarget.hasFPCVT()) {
371 if (Subtarget.has64BitSupport()) {
Hal Finkelf6d45f22013-04-01 17:52:07 +0000372 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +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
Eric Christopherb1aaebe2014-06-12 22:38:18 +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);
Ulrich Weigand51eccec2014-08-04 13:27:12 +0000456 setOperationAction(ISD::MULHU, VT, Expand);
457 setOperationAction(ISD::MULHS, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000458 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
459 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
460 setOperationAction(ISD::UDIVREM, VT, Expand);
461 setOperationAction(ISD::SDIVREM, VT, Expand);
462 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
463 setOperationAction(ISD::FPOW, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000464 setOperationAction(ISD::BSWAP, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000465 setOperationAction(ISD::CTPOP, VT, Expand);
466 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000467 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands13237ac2008-06-06 12:08:01 +0000468 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000469 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramerc5071462012-12-19 15:49:14 +0000470 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellac4182d12012-11-05 17:15:56 +0000471 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
472
473 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
474 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
475 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
476 setTruncStoreAction(VT, InnerVT, Expand);
477 }
478 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
479 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
480 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnerbaa73e02006-03-31 19:52:36 +0000481 }
482
Chris Lattner95c7adc2006-04-04 17:25:31 +0000483 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
484 // with merges, splats, etc.
Owen Anderson9f944592009-08-11 20:47:22 +0000485 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner95c7adc2006-04-04 17:25:31 +0000486
Owen Anderson9f944592009-08-11 20:47:22 +0000487 setOperationAction(ISD::AND , MVT::v4i32, Legal);
488 setOperationAction(ISD::OR , MVT::v4i32, Legal);
489 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
490 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Hal Finkel940ab932014-02-28 00:27:01 +0000491 setOperationAction(ISD::SELECT, MVT::v4i32,
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000492 Subtarget.useCRBits() ? Legal : Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000493 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella5c6e0842012-10-08 17:27:24 +0000494 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
495 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
496 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
497 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellabdface52012-11-15 20:56:03 +0000498 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
499 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
500 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
501 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000502
Craig Topperabadc662012-04-20 06:31:50 +0000503 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
504 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
505 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
506 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000507
Owen Anderson9f944592009-08-11 20:47:22 +0000508 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel0a479ae2012-06-22 00:49:52 +0000509 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel2e103312013-04-03 04:01:11 +0000510
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000511 if (TM.Options.UnsafeFPMath || Subtarget.hasVSX()) {
Hal Finkel2e103312013-04-03 04:01:11 +0000512 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
513 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
514 }
515
Owen Anderson9f944592009-08-11 20:47:22 +0000516 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
517 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
518 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnera8713b12006-03-20 01:53:53 +0000519
Owen Anderson9f944592009-08-11 20:47:22 +0000520 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
521 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000522
Owen Anderson9f944592009-08-11 20:47:22 +0000523 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
524 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
525 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
526 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000527
528 // Altivec does not contain unordered floating-point compare instructions
529 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
530 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
Hal Finkel21ada792013-07-08 20:00:03 +0000531 setCondCodeAction(ISD::SETO, MVT::v4f32, Expand);
532 setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000533
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000534 if (Subtarget.hasVSX()) {
Hal Finkel27774d92014-03-13 07:58:58 +0000535 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
Hal Finkel82569b62014-03-27 22:22:48 +0000536 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Legal);
Hal Finkel27774d92014-03-13 07:58:58 +0000537
538 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
539 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
540 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
541 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
542 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
543
544 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
545
546 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
547 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
548
549 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
550 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
551
Hal Finkel732f0f72014-03-26 12:49:28 +0000552 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
553 setOperationAction(ISD::VSELECT, MVT::v8i16, Legal);
554 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
555 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
556 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
557
Hal Finkel27774d92014-03-13 07:58:58 +0000558 // Share the Altivec comparison restrictions.
559 setCondCodeAction(ISD::SETUO, MVT::v2f64, Expand);
560 setCondCodeAction(ISD::SETUEQ, MVT::v2f64, Expand);
Hal Finkel27774d92014-03-13 07:58:58 +0000561 setCondCodeAction(ISD::SETO, MVT::v2f64, Expand);
562 setCondCodeAction(ISD::SETONE, MVT::v2f64, Expand);
563
Hal Finkel9281c9a2014-03-26 18:26:30 +0000564 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
565 setOperationAction(ISD::STORE, MVT::v2f64, Legal);
566
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000567 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Legal);
568
Hal Finkel19be5062014-03-29 05:29:01 +0000569 addRegisterClass(MVT::f64, &PPC::VSFRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000570
571 addRegisterClass(MVT::v4f32, &PPC::VSRCRegClass);
572 addRegisterClass(MVT::v2f64, &PPC::VSRCRegClass);
Hal Finkela6c8b512014-03-26 16:12:58 +0000573
574 // VSX v2i64 only supports non-arithmetic operations.
575 setOperationAction(ISD::ADD, MVT::v2i64, Expand);
576 setOperationAction(ISD::SUB, MVT::v2i64, Expand);
577
Hal Finkelad801b72014-03-27 21:26:33 +0000578 setOperationAction(ISD::SHL, MVT::v2i64, Expand);
579 setOperationAction(ISD::SRA, MVT::v2i64, Expand);
580 setOperationAction(ISD::SRL, MVT::v2i64, Expand);
581
Hal Finkel777c9dd2014-03-29 16:04:40 +0000582 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
583
Hal Finkel9281c9a2014-03-26 18:26:30 +0000584 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
585 AddPromotedToType (ISD::LOAD, MVT::v2i64, MVT::v2f64);
586 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
587 AddPromotedToType (ISD::STORE, MVT::v2i64, MVT::v2f64);
588
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000589 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Legal);
590
Hal Finkel7279f4b2014-03-26 19:13:54 +0000591 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
592 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
593 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
594 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
595
Hal Finkel5c0d1452014-03-30 13:22:59 +0000596 // Vector operation legalization checks the result type of
597 // SIGN_EXTEND_INREG, overall legalization checks the inner type.
598 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i64, Legal);
599 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Legal);
600 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
601 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
602
Hal Finkela6c8b512014-03-26 16:12:58 +0000603 addRegisterClass(MVT::v2i64, &PPC::VSRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +0000604 }
Nate Begeman3e7db9c2005-11-29 08:17:20 +0000605 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000606
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000607 if (Subtarget.has64BitSupport()) {
Hal Finkel322e41a2012-04-01 20:08:17 +0000608 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel70381a72012-08-04 14:10:46 +0000609 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
610 }
Hal Finkel322e41a2012-04-01 20:08:17 +0000611
Eli Friedman7dfa7912011-08-29 18:23:02 +0000612 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
613 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkel1b5ff082012-12-25 17:22:53 +0000614 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
615 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000616
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000617 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidta76bf5a2013-04-23 18:49:44 +0000618 // Altivec instructions set fields to all zeros or all ones.
619 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000620
Joerg Sonnenbergerb5459e62014-07-24 22:20:10 +0000621 if (!isPPC64) {
622 // These libcalls are not available in 32-bit.
623 setLibcallName(RTLIB::SHL_I128, nullptr);
624 setLibcallName(RTLIB::SRL_I128, nullptr);
625 setLibcallName(RTLIB::SRA_I128, nullptr);
626 }
627
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);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000643 if (Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000644 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +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.
Eric Christopherb1aaebe2014-06-12 22:38:18 +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.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000680 if (Subtarget.useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000681 setHasMultipleConditionRegisters();
682
Hal Finkel65298572011-10-17 18:53:03 +0000683 setMinFunctionAlignment(2);
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000684 if (Subtarget.isDarwin())
Hal Finkel65298572011-10-17 18:53:03 +0000685 setPrefFunctionAlignment(4);
Eli Friedman2518f832011-05-06 20:34:06 +0000686
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000687 if (isPPC64 && Subtarget.isJITCodeModel())
Evan Cheng39e90022012-07-02 22:39:56 +0000688 // 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
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000694 if (Subtarget.enableMachineScheduler())
Hal Finkel21442b22013-09-11 23:05:25 +0000695 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).
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000703 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.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000748 if (Subtarget.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.
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000753 unsigned Align = Subtarget.isPPC64() ? 8 : 4;
754 if (Subtarget.hasAltivec() || Subtarget.hasQPX())
755 getMaxByValAlign(Ty, Align, Subtarget.hasQPX() ? 32 : 16);
Hal Finkel262a2242013-09-12 23:20:06 +0000756 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::LOAD: return "PPCISD::LOAD";
776 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng32e376f2008-07-12 02:23:19 +0000777 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
778 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
779 case PPCISD::SRL: return "PPCISD::SRL";
780 case PPCISD::SRA: return "PPCISD::SRA";
781 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000782 case PPCISD::CALL: return "PPCISD::CALL";
783 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng32e376f2008-07-12 02:23:19 +0000784 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000785 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng32e376f2008-07-12 02:23:19 +0000786 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel756810f2013-03-21 21:37:52 +0000787 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
788 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000789 case PPCISD::MFOCRF: return "PPCISD::MFOCRF";
Evan Cheng32e376f2008-07-12 02:23:19 +0000790 case PPCISD::VCMP: return "PPCISD::VCMP";
791 case PPCISD::VCMPo: return "PPCISD::VCMPo";
792 case PPCISD::LBRX: return "PPCISD::LBRX";
793 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng32e376f2008-07-12 02:23:19 +0000794 case PPCISD::LARX: return "PPCISD::LARX";
795 case PPCISD::STCX: return "PPCISD::STCX";
796 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkel25c19922013-05-15 21:37:41 +0000797 case PPCISD::BDNZ: return "PPCISD::BDNZ";
798 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000799 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng32e376f2008-07-12 02:23:19 +0000800 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng32e376f2008-07-12 02:23:19 +0000801 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel5ab37802012-08-28 02:10:27 +0000802 case PPCISD::CR6SET: return "PPCISD::CR6SET";
803 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34627e32012-11-27 17:35:46 +0000804 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
805 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
806 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Roman Divacky32143e22013-12-20 18:08:54 +0000807 case PPCISD::PPC32_GOT: return "PPCISD::PPC32_GOT";
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000808 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
809 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000810 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000811 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
812 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
813 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000814 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
815 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
816 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
817 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
818 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidt51e79512013-02-20 15:50:31 +0000819 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidta87a7e22013-05-14 19:35:45 +0000820 case PPCISD::SC: return "PPCISD::SC";
Chris Lattner347ed8a2006-01-09 23:52:17 +0000821 }
822}
823
Matt Arsenault758659232013-05-18 00:21:46 +0000824EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000825 if (!VT.isVector())
Eric Christopherb1aaebe2014-06-12 22:38:18 +0000826 return Subtarget.useCRBits() ? MVT::i1 : MVT::i32;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000827 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000828}
829
Chris Lattner4211ca92006-04-14 06:01:58 +0000830//===----------------------------------------------------------------------===//
831// Node matching predicates, for use by the tblgen matching code.
832//===----------------------------------------------------------------------===//
833
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000834/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000835static bool isFloatingPointZero(SDValue Op) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000836 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000837 return CFP->getValueAPF().isZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000838 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000839 // Maybe this has already been legalized into the constant pool?
840 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000841 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000842 return CFP->getValueAPF().isZero();
Chris Lattner7f1fa8e2005-08-26 17:36:52 +0000843 }
844 return false;
845}
846
Chris Lattnere8b83b42006-04-06 17:23:16 +0000847/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
848/// true if Op is undef or if it matches the specified value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000849static bool isConstantOrUndef(int Op, int Val) {
850 return Op < 0 || Op == Val;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000851}
852
853/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
854/// VPKUHUM instruction.
Bill Schmidtf910a062014-06-10 14:35:01 +0000855bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary,
856 SelectionDAG &DAG) {
857 unsigned j = DAG.getTarget().getDataLayout()->isLittleEndian() ? 0 : 1;
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000858 if (!isUnary) {
859 for (unsigned i = 0; i != 16; ++i)
Bill Schmidtf910a062014-06-10 14:35:01 +0000860 if (!isConstantOrUndef(N->getMaskElt(i), i*2+j))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000861 return false;
862 } else {
863 for (unsigned i = 0; i != 8; ++i)
Bill Schmidtf910a062014-06-10 14:35:01 +0000864 if (!isConstantOrUndef(N->getMaskElt(i), i*2+j) ||
865 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000866 return false;
867 }
Chris Lattner1d338192006-04-06 18:26:28 +0000868 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000869}
870
871/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
872/// VPKUWUM instruction.
Bill Schmidtf910a062014-06-10 14:35:01 +0000873bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary,
874 SelectionDAG &DAG) {
875 unsigned j, k;
876 if (DAG.getTarget().getDataLayout()->isLittleEndian()) {
877 j = 0;
878 k = 1;
879 } else {
880 j = 2;
881 k = 3;
882 }
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000883 if (!isUnary) {
884 for (unsigned i = 0; i != 16; i += 2)
Bill Schmidtf910a062014-06-10 14:35:01 +0000885 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) ||
886 !isConstantOrUndef(N->getMaskElt(i+1), i*2+k))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000887 return false;
888 } else {
889 for (unsigned i = 0; i != 8; i += 2)
Bill Schmidtf910a062014-06-10 14:35:01 +0000890 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+j) ||
891 !isConstantOrUndef(N->getMaskElt(i+1), i*2+k) ||
892 !isConstantOrUndef(N->getMaskElt(i+8), i*2+j) ||
893 !isConstantOrUndef(N->getMaskElt(i+9), i*2+k))
Chris Lattnera4bbfae2006-04-06 22:28:36 +0000894 return false;
895 }
Chris Lattner1d338192006-04-06 18:26:28 +0000896 return true;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000897}
898
Chris Lattnerf38e0332006-04-06 22:02:42 +0000899/// isVMerge - Common function, used to match vmrg* shuffles.
900///
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000901static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnerf38e0332006-04-06 22:02:42 +0000902 unsigned LHSStart, unsigned RHSStart) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000903 if (N->getValueType(0) != MVT::v16i8)
904 return false;
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000905 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
906 "Unsupported merge size!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000907
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000908 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
909 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000910 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000911 LHSStart+j+i*UnitSize) ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000912 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnerf38e0332006-04-06 22:02:42 +0000913 RHSStart+j+i*UnitSize))
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000914 return false;
915 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000916 return true;
Chris Lattnerf38e0332006-04-06 22:02:42 +0000917}
918
919/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +0000920/// a VMRGL* instruction with the specified unit size (1,2 or 4 bytes).
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000921/// The ShuffleKind distinguishes between big-endian merges with two
922/// different inputs (0), either-endian merges with two identical inputs (1),
923/// and little-endian merges with two different inputs (2). For the latter,
924/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +0000925bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000926 unsigned ShuffleKind, SelectionDAG &DAG) {
Bill Schmidtf910a062014-06-10 14:35:01 +0000927 if (DAG.getTarget().getDataLayout()->isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000928 if (ShuffleKind == 1) // unary
929 return isVMerge(N, UnitSize, 0, 0);
930 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +0000931 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000932 else
933 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +0000934 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000935 if (ShuffleKind == 1) // unary
936 return isVMerge(N, UnitSize, 8, 8);
937 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +0000938 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000939 else
940 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +0000941 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000942}
943
944/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
Bill Schmidtf910a062014-06-10 14:35:01 +0000945/// a VMRGH* instruction with the specified unit size (1,2 or 4 bytes).
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000946/// The ShuffleKind distinguishes between big-endian merges with two
947/// different inputs (0), either-endian merges with two identical inputs (1),
948/// and little-endian merges with two different inputs (2). For the latter,
949/// the input operands are swapped (see PPCInstrAltivec.td).
Wesley Peck527da1b2010-11-23 03:31:01 +0000950bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000951 unsigned ShuffleKind, SelectionDAG &DAG) {
Bill Schmidtf910a062014-06-10 14:35:01 +0000952 if (DAG.getTarget().getDataLayout()->isLittleEndian()) {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000953 if (ShuffleKind == 1) // unary
954 return isVMerge(N, UnitSize, 8, 8);
955 else if (ShuffleKind == 2) // swapped
Bill Schmidtf910a062014-06-10 14:35:01 +0000956 return isVMerge(N, UnitSize, 8, 24);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000957 else
958 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +0000959 } else {
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000960 if (ShuffleKind == 1) // unary
961 return isVMerge(N, UnitSize, 0, 0);
962 else if (ShuffleKind == 0) // normal
Bill Schmidtf910a062014-06-10 14:35:01 +0000963 return isVMerge(N, UnitSize, 0, 16);
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +0000964 else
965 return false;
Bill Schmidtf910a062014-06-10 14:35:01 +0000966 }
Chris Lattnerd1dcb522006-04-06 21:11:54 +0000967}
968
969
Chris Lattner1d338192006-04-06 18:26:28 +0000970/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
971/// amount, otherwise return -1.
Bill Schmidtf910a062014-06-10 14:35:01 +0000972int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary, SelectionDAG &DAG) {
Hal Finkeldf3e34d2014-03-26 22:58:37 +0000973 if (N->getValueType(0) != MVT::v16i8)
Hal Finkela775e512014-04-08 19:00:27 +0000974 return -1;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000975
976 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peck527da1b2010-11-23 03:31:01 +0000977
Chris Lattner1d338192006-04-06 18:26:28 +0000978 // Find the first non-undef value in the shuffle mask.
979 unsigned i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000980 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattner1d338192006-04-06 18:26:28 +0000981 /*search*/;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000982
Chris Lattner1d338192006-04-06 18:26:28 +0000983 if (i == 16) return -1; // all undef.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000984
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000985 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattner1d338192006-04-06 18:26:28 +0000986 // numbered from this value.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000987 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattner1d338192006-04-06 18:26:28 +0000988 if (ShiftAmt < i) return -1;
Chris Lattnere8b83b42006-04-06 17:23:16 +0000989
Bill Schmidtf910a062014-06-10 14:35:01 +0000990 if (DAG.getTarget().getDataLayout()->isLittleEndian()) {
991
992 ShiftAmt += i;
993
994 if (!isUnary) {
995 // Check the rest of the elements to see if they are consecutive.
996 for (++i; i != 16; ++i)
997 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt - i))
998 return -1;
999 } else {
1000 // Check the rest of the elements to see if they are consecutive.
1001 for (++i; i != 16; ++i)
1002 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt - i) & 15))
1003 return -1;
1004 }
1005
1006 } else { // Big Endian
1007
1008 ShiftAmt -= i;
1009
1010 if (!isUnary) {
1011 // Check the rest of the elements to see if they are consecutive.
1012 for (++i; i != 16; ++i)
1013 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
1014 return -1;
1015 } else {
1016 // Check the rest of the elements to see if they are consecutive.
1017 for (++i; i != 16; ++i)
1018 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
1019 return -1;
1020 }
Chris Lattnera4bbfae2006-04-06 22:28:36 +00001021 }
Chris Lattner1d338192006-04-06 18:26:28 +00001022 return ShiftAmt;
1023}
Chris Lattnerffc47562006-03-20 06:33:01 +00001024
1025/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
1026/// specifies a splat of a single element that is suitable for input to
1027/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001028bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson9f944592009-08-11 20:47:22 +00001029 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner95c7adc2006-04-04 17:25:31 +00001030 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001031
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001032 // This is a splat operation if each element of the permute is the same, and
1033 // if the value doesn't reference the second vector.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001034 unsigned ElementBase = N->getMaskElt(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001035
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001036 // FIXME: Handle UNDEF elements too!
1037 if (ElementBase >= 16)
Chris Lattner95c7adc2006-04-04 17:25:31 +00001038 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001039
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001040 // Check that the indices are consecutive, in the case of a multi-byte element
1041 // splatted with a v16i8 mask.
1042 for (unsigned i = 1; i != EltSize; ++i)
1043 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001044 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001045
Chris Lattner95c7adc2006-04-04 17:25:31 +00001046 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001047 if (N->getMaskElt(i) < 0) continue;
Chris Lattner95c7adc2006-04-04 17:25:31 +00001048 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001049 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner95c7adc2006-04-04 17:25:31 +00001050 return false;
Chris Lattnera8fbb6d2006-03-20 06:37:44 +00001051 }
Chris Lattner95c7adc2006-04-04 17:25:31 +00001052 return true;
Chris Lattnerffc47562006-03-20 06:33:01 +00001053}
1054
Evan Cheng581d2792007-07-30 07:51:22 +00001055/// isAllNegativeZeroVector - Returns true if all elements of build_vector
1056/// are -0.0.
1057bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001058 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
1059
1060 APInt APVal, APUndef;
1061 unsigned BitSize;
1062 bool HasAnyUndefs;
Wesley Peck527da1b2010-11-23 03:31:01 +00001063
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00001064 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001065 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001066 return CFP->getValueAPF().isNegZero();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001067
Evan Cheng581d2792007-07-30 07:51:22 +00001068 return false;
1069}
1070
Chris Lattnerffc47562006-03-20 06:33:01 +00001071/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
1072/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Bill Schmidtf910a062014-06-10 14:35:01 +00001073unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize,
1074 SelectionDAG &DAG) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001075 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1076 assert(isSplatShuffleMask(SVOp, EltSize));
Bill Schmidtf910a062014-06-10 14:35:01 +00001077 if (DAG.getTarget().getDataLayout()->isLittleEndian())
1078 return (16 / EltSize) - 1 - (SVOp->getMaskElt(0) / EltSize);
1079 else
1080 return SVOp->getMaskElt(0) / EltSize;
Chris Lattnerffc47562006-03-20 06:33:01 +00001081}
1082
Chris Lattner74cf9ff2006-04-12 17:37:20 +00001083/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001084/// by using a vspltis[bhw] instruction of the specified element size, return
1085/// the constant being splatted. The ByteSize field indicates the number of
1086/// bytes of each element [124] -> [bhw].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001087SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001088 SDValue OpVal(nullptr, 0);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001089
1090 // If ByteSize of the splat is bigger than the element size of the
1091 // build_vector, then we have a case where we are checking for a splat where
1092 // multiple elements of the buildvector are folded together into a single
1093 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
1094 unsigned EltSize = 16/N->getNumOperands();
1095 if (EltSize < ByteSize) {
1096 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001097 SDValue UniquedVals[4];
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001098 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001099
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001100 // See if all of the elements in the buildvector agree across.
1101 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1102 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1103 // If the element isn't a constant, bail fully out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001104 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001105
Scott Michelcf0da6c2009-02-17 22:15:04 +00001106
Craig Topper062a2ba2014-04-25 05:30:21 +00001107 if (!UniquedVals[i&(Multiple-1)].getNode())
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001108 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
1109 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001110 return SDValue(); // no match.
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001111 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001112
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001113 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
1114 // either constant or undef values that are identical for each chunk. See
1115 // if these chunks can form into a larger vspltis*.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001116
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001117 // Check to see if all of the leading entries are either 0 or -1. If
1118 // neither, then this won't fit into the immediate field.
1119 bool LeadingZero = true;
1120 bool LeadingOnes = true;
1121 for (unsigned i = 0; i != Multiple-1; ++i) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001122 if (!UniquedVals[i].getNode()) continue; // Must have been undefs.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001123
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001124 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
1125 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
1126 }
1127 // Finally, check the least significant entry.
1128 if (LeadingZero) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001129 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001130 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00001131 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001132 if (Val < 16)
Owen Anderson9f944592009-08-11 20:47:22 +00001133 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001134 }
1135 if (LeadingOnes) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001136 if (!UniquedVals[Multiple-1].getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001137 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman6e054832008-09-26 21:54:37 +00001138 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001139 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson9f944592009-08-11 20:47:22 +00001140 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001141 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001142
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001143 return SDValue();
Chris Lattnerd9e80f42006-04-08 07:14:26 +00001144 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001145
Chris Lattner2771e2c2006-03-25 06:12:06 +00001146 // Check to see if this buildvec has a single non-undef value in its elements.
1147 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1148 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Craig Topper062a2ba2014-04-25 05:30:21 +00001149 if (!OpVal.getNode())
Chris Lattner2771e2c2006-03-25 06:12:06 +00001150 OpVal = N->getOperand(i);
1151 else if (OpVal != N->getOperand(i))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001152 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001153 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001154
Craig Topper062a2ba2014-04-25 05:30:21 +00001155 if (!OpVal.getNode()) return SDValue(); // All UNDEF: use implicit def.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001156
Eli Friedman9c6ab1a2009-05-24 02:03:36 +00001157 unsigned ValSizeInBytes = EltSize;
Nate Begeman1b392872006-03-28 04:15:58 +00001158 uint64_t Value = 0;
Chris Lattner2771e2c2006-03-25 06:12:06 +00001159 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001160 Value = CN->getZExtValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001161 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001162 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001163 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner2771e2c2006-03-25 06:12:06 +00001164 }
1165
1166 // If the splat value is larger than the element value, then we can never do
1167 // this splat. The only case that we could fit the replicated bits into our
1168 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001169 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001170
Chris Lattner2771e2c2006-03-25 06:12:06 +00001171 // If the element value is larger than the splat value, cut it in half and
1172 // check to see if the two halves are equal. Continue doing this until we
1173 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
1174 while (ValSizeInBytes > ByteSize) {
1175 ValSizeInBytes >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001176
Chris Lattner2771e2c2006-03-25 06:12:06 +00001177 // If the top half equals the bottom half, we're still ok.
Chris Lattner39cc7172006-04-05 17:39:25 +00001178 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
1179 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001180 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001181 }
1182
1183 // Properly sign extend the value.
Richard Smith228e6d42012-08-24 23:29:28 +00001184 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001185
Evan Chengb1ddc982006-03-26 09:52:32 +00001186 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001187 if (MaskVal == 0) return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001188
Chris Lattnerd71a1f92006-04-08 06:46:53 +00001189 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith228e6d42012-08-24 23:29:28 +00001190 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson9f944592009-08-11 20:47:22 +00001191 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001192 return SDValue();
Chris Lattner2771e2c2006-03-25 06:12:06 +00001193}
1194
Chris Lattner4211ca92006-04-14 06:01:58 +00001195//===----------------------------------------------------------------------===//
Chris Lattnera801fced2006-11-08 02:15:41 +00001196// Addressing Mode Selection
1197//===----------------------------------------------------------------------===//
1198
1199/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
1200/// or 64-bit immediate, and if the value can be accurately represented as a
1201/// sign extension from a 16-bit value. If so, this returns true and the
1202/// immediate.
1203static bool isIntS16Immediate(SDNode *N, short &Imm) {
Adam Nemet571eb5f2014-05-20 17:20:34 +00001204 if (!isa<ConstantSDNode>(N))
Chris Lattnera801fced2006-11-08 02:15:41 +00001205 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001206
Dan Gohmaneffb8942008-09-12 16:56:44 +00001207 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001208 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +00001209 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001210 else
Dan Gohmaneffb8942008-09-12 16:56:44 +00001211 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnera801fced2006-11-08 02:15:41 +00001212}
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001213static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001214 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnera801fced2006-11-08 02:15:41 +00001215}
1216
1217
1218/// SelectAddressRegReg - Given the specified addressed, check to see if it
1219/// can be represented as an indexed [r+r] operation. Returns false if it
1220/// can be more efficiently represented with [r+imm].
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001221bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1222 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001223 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001224 short imm = 0;
1225 if (N.getOpcode() == ISD::ADD) {
1226 if (isIntS16Immediate(N.getOperand(1), imm))
1227 return false; // r+i
1228 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1229 return false; // r+i
Scott Michelcf0da6c2009-02-17 22:15:04 +00001230
Chris Lattnera801fced2006-11-08 02:15:41 +00001231 Base = N.getOperand(0);
1232 Index = N.getOperand(1);
1233 return true;
1234 } else if (N.getOpcode() == ISD::OR) {
1235 if (isIntS16Immediate(N.getOperand(1), imm))
1236 return false; // r+i can fold it if we can.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001237
Chris Lattnera801fced2006-11-08 02:15:41 +00001238 // If this is an or of disjoint bitfields, we can codegen this as an add
1239 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1240 // disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001241 APInt LHSKnownZero, LHSKnownOne;
1242 APInt RHSKnownZero, RHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001243 DAG.computeKnownBits(N.getOperand(0),
1244 LHSKnownZero, LHSKnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001245
Dan Gohmanf19609a2008-02-27 01:23:58 +00001246 if (LHSKnownZero.getBoolValue()) {
Jay Foada0653a32014-05-14 21:14:37 +00001247 DAG.computeKnownBits(N.getOperand(1),
1248 RHSKnownZero, RHSKnownOne);
Chris Lattnera801fced2006-11-08 02:15:41 +00001249 // If all of the bits are known zero on the LHS or RHS, the add won't
1250 // carry.
Dan Gohman26854f22008-02-27 21:12:32 +00001251 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001252 Base = N.getOperand(0);
1253 Index = N.getOperand(1);
1254 return true;
1255 }
1256 }
1257 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001258
Chris Lattnera801fced2006-11-08 02:15:41 +00001259 return false;
1260}
1261
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001262// If we happen to be doing an i64 load or store into a stack slot that has
1263// less than a 4-byte alignment, then the frame-index elimination may need to
1264// use an indexed load or store instruction (because the offset may not be a
1265// multiple of 4). The extra register needed to hold the offset comes from the
1266// register scavenger, and it is possible that the scavenger will need to use
1267// an emergency spill slot. As a result, we need to make sure that a spill slot
1268// is allocated when doing an i64 load/store into a less-than-4-byte-aligned
1269// stack slot.
1270static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) {
1271 // FIXME: This does not handle the LWA case.
1272 if (VT != MVT::i64)
1273 return;
1274
Hal Finkel7ab3db52013-07-10 15:29:01 +00001275 // NOTE: We'll exclude negative FIs here, which come from argument
1276 // lowering, because there are no known test cases triggering this problem
1277 // using packed structures (or similar). We can remove this exclusion if
1278 // we find such a test case. The reason why this is so test-case driven is
1279 // because this entire 'fixup' is only to prevent crashes (from the
1280 // register scavenger) on not-really-valid inputs. For example, if we have:
1281 // %a = alloca i1
1282 // %b = bitcast i1* %a to i64*
1283 // store i64* a, i64 b
1284 // then the store should really be marked as 'align 1', but is not. If it
1285 // were marked as 'align 1' then the indexed form would have been
1286 // instruction-selected initially, and the problem this 'fixup' is preventing
1287 // won't happen regardless.
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001288 if (FrameIdx < 0)
1289 return;
1290
1291 MachineFunction &MF = DAG.getMachineFunction();
1292 MachineFrameInfo *MFI = MF.getFrameInfo();
1293
1294 unsigned Align = MFI->getObjectAlignment(FrameIdx);
1295 if (Align >= 4)
1296 return;
1297
1298 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1299 FuncInfo->setHasNonRISpills();
1300}
1301
Chris Lattnera801fced2006-11-08 02:15:41 +00001302/// Returns true if the address N can be represented by a base register plus
1303/// a signed 16-bit displacement [r+imm], and if it is not better
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001304/// represented as reg+reg. If Aligned is true, only accept displacements
1305/// suitable for STD and friends, i.e. multiples of 4.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001306bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman02b93132009-01-15 16:29:45 +00001307 SDValue &Base,
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001308 SelectionDAG &DAG,
1309 bool Aligned) const {
Dale Johannesenab8e4422009-02-06 19:16:40 +00001310 // FIXME dl should come from parent load or store, not from address
Andrew Trickef9de2a2013-05-25 02:42:55 +00001311 SDLoc dl(N);
Chris Lattnera801fced2006-11-08 02:15:41 +00001312 // If this can be more profitably realized as r+r, fail.
1313 if (SelectAddressRegReg(N, Disp, Base, DAG))
1314 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001315
Chris Lattnera801fced2006-11-08 02:15:41 +00001316 if (N.getOpcode() == ISD::ADD) {
1317 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001318 if (isIntS16Immediate(N.getOperand(1), imm) &&
1319 (!Aligned || (imm & 3) == 0)) {
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001320 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001321 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1322 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001323 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001324 } else {
1325 Base = N.getOperand(0);
1326 }
1327 return true; // [r+i]
1328 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1329 // Match LOAD (ADD (X, Lo(G))).
Gabor Greifc8a9abe2012-04-20 11:41:38 +00001330 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnera801fced2006-11-08 02:15:41 +00001331 && "Cannot handle constant offsets yet!");
1332 Disp = N.getOperand(1).getOperand(0); // The global address.
1333 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackye3f15c982012-06-04 17:36:38 +00001334 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnera801fced2006-11-08 02:15:41 +00001335 Disp.getOpcode() == ISD::TargetConstantPool ||
1336 Disp.getOpcode() == ISD::TargetJumpTable);
1337 Base = N.getOperand(0);
1338 return true; // [&g+r]
1339 }
1340 } else if (N.getOpcode() == ISD::OR) {
1341 short imm = 0;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001342 if (isIntS16Immediate(N.getOperand(1), imm) &&
1343 (!Aligned || (imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001344 // If this is an or of disjoint bitfields, we can codegen this as an add
1345 // (for better address arithmetic) if the LHS and RHS of the OR are
1346 // provably disjoint.
Dan Gohmanf19609a2008-02-27 01:23:58 +00001347 APInt LHSKnownZero, LHSKnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00001348 DAG.computeKnownBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling63061832008-03-24 23:16:37 +00001349
Dan Gohmanf19609a2008-02-27 01:23:58 +00001350 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001351 // If all of the bits are known zero on the LHS or RHS, the add won't
1352 // carry.
Ulrich Weigand55a96652014-07-20 22:26:40 +00001353 if (FrameIndexSDNode *FI =
1354 dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1355 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1356 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1357 } else {
1358 Base = N.getOperand(0);
1359 }
Ulrich Weigand7aa76b62013-05-16 14:53:05 +00001360 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001361 return true;
1362 }
1363 }
1364 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1365 // Loading from a constant address.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001366
Chris Lattnera801fced2006-11-08 02:15:41 +00001367 // If this address fits entirely in a 16-bit sext immediate field, codegen
1368 // this as "d, 0"
1369 short Imm;
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001370 if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001371 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001372 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001373 CN->getValueType(0));
Chris Lattnera801fced2006-11-08 02:15:41 +00001374 return true;
1375 }
Chris Lattner4a9c0bb2007-02-17 06:44:03 +00001376
1377 // Handle 32-bit sext immediates with LIS + addr mode.
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001378 if ((CN->getValueType(0) == MVT::i32 ||
1379 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) &&
1380 (!Aligned || (CN->getZExtValue() & 3) == 0)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001381 int Addr = (int)CN->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001382
Chris Lattnera801fced2006-11-08 02:15:41 +00001383 // Otherwise, break this down into an LIS + disp.
Owen Anderson9f944592009-08-11 20:47:22 +00001384 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001385
Owen Anderson9f944592009-08-11 20:47:22 +00001386 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1387 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman32f71d72009-09-25 18:54:59 +00001388 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnera801fced2006-11-08 02:15:41 +00001389 return true;
1390 }
1391 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001392
Chris Lattnera801fced2006-11-08 02:15:41 +00001393 Disp = DAG.getTargetConstant(0, getPointerTy());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001394 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) {
Chris Lattnera801fced2006-11-08 02:15:41 +00001395 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
Hal Finkeldbbf09b2013-07-09 06:34:51 +00001396 fixupFuncForFI(DAG, FI->getIndex(), N.getValueType());
1397 } else
Chris Lattnera801fced2006-11-08 02:15:41 +00001398 Base = N;
1399 return true; // [r+0]
1400}
1401
1402/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1403/// represented as an indexed [r+r] operation.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001404bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1405 SDValue &Index,
Dan Gohman02b93132009-01-15 16:29:45 +00001406 SelectionDAG &DAG) const {
Chris Lattnera801fced2006-11-08 02:15:41 +00001407 // Check to see if we can easily represent this as an [r+r] address. This
1408 // will fail if it thinks that the address is more profitably represented as
1409 // reg+imm, e.g. where imm = 0.
1410 if (SelectAddressRegReg(N, Base, Index, DAG))
1411 return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001412
Chris Lattnera801fced2006-11-08 02:15:41 +00001413 // If the operand is an addition, always emit this as [r+r], since this is
1414 // better (for code size, and execution, as the memop does the add for free)
1415 // than emitting an explicit add.
1416 if (N.getOpcode() == ISD::ADD) {
1417 Base = N.getOperand(0);
1418 Index = N.getOperand(1);
1419 return true;
1420 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001421
Chris Lattnera801fced2006-11-08 02:15:41 +00001422 // Otherwise, do it the hard way, using R0 as the base register.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001423 Base = DAG.getRegister(Subtarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
Hal Finkelf70c41e2013-03-21 23:45:03 +00001424 N.getValueType());
Chris Lattnera801fced2006-11-08 02:15:41 +00001425 Index = N;
1426 return true;
1427}
1428
Chris Lattnera801fced2006-11-08 02:15:41 +00001429/// getPreIndexedAddressParts - returns true by value, base pointer and
1430/// offset pointer and addressing mode by reference if the node's address
1431/// can be legally represented as pre-indexed load / store address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001432bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1433 SDValue &Offset,
Evan Chengb1500072006-11-09 17:55:04 +00001434 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00001435 SelectionDAG &DAG) const {
Hal Finkel595817e2012-06-04 02:21:00 +00001436 if (DisablePPCPreinc) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001437
Ulrich Weigande90b0222013-03-22 14:58:48 +00001438 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001439 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001440 EVT VT;
Hal Finkelb09680b2013-03-18 23:00:58 +00001441 unsigned Alignment;
Chris Lattnera801fced2006-11-08 02:15:41 +00001442 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1443 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001444 VT = LD->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001445 Alignment = LD->getAlignment();
Chris Lattnera801fced2006-11-08 02:15:41 +00001446 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner68371252006-11-14 01:38:31 +00001447 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001448 VT = ST->getMemoryVT();
Hal Finkelb09680b2013-03-18 23:00:58 +00001449 Alignment = ST->getAlignment();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001450 isLoad = false;
Chris Lattnera801fced2006-11-08 02:15:41 +00001451 } else
1452 return false;
1453
Chris Lattner68371252006-11-14 01:38:31 +00001454 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands13237ac2008-06-06 12:08:01 +00001455 if (VT.isVector())
Chris Lattner68371252006-11-14 01:38:31 +00001456 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001457
Ulrich Weigande90b0222013-03-22 14:58:48 +00001458 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1459
1460 // Common code will reject creating a pre-inc form if the base pointer
1461 // is a frame index, or if N is a store and the base pointer is either
1462 // the same as or a predecessor of the value being stored. Check for
1463 // those situations here, and try with swapped Base/Offset instead.
1464 bool Swap = false;
1465
1466 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1467 Swap = true;
1468 else if (!isLoad) {
1469 SDValue Val = cast<StoreSDNode>(N)->getValue();
1470 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1471 Swap = true;
1472 }
1473
1474 if (Swap)
1475 std::swap(Base, Offset);
1476
Hal Finkelca542be2012-06-20 15:43:03 +00001477 AM = ISD::PRE_INC;
1478 return true;
Hal Finkel1cc27e42012-06-19 02:34:32 +00001479 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001480
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001481 // LDU/STU can only handle immediates that are a multiple of 4.
Owen Anderson9f944592009-08-11 20:47:22 +00001482 if (VT != MVT::i64) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001483 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false))
Chris Lattner474b5b72006-11-15 19:55:13 +00001484 return false;
1485 } else {
Hal Finkelb09680b2013-03-18 23:00:58 +00001486 // LDU/STU need an address with at least 4-byte alignment.
1487 if (Alignment < 4)
1488 return false;
1489
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00001490 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true))
Chris Lattner474b5b72006-11-15 19:55:13 +00001491 return false;
1492 }
Chris Lattnerb314b152006-11-11 00:08:42 +00001493
Chris Lattnerb314b152006-11-11 00:08:42 +00001494 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001495 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1496 // sext i32 to i64 when addr mode is r+i.
Owen Anderson9f944592009-08-11 20:47:22 +00001497 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerb314b152006-11-11 00:08:42 +00001498 LD->getExtensionType() == ISD::SEXTLOAD &&
1499 isa<ConstantSDNode>(Offset))
1500 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001501 }
1502
Chris Lattnerce645542006-11-10 02:08:47 +00001503 AM = ISD::PRE_INC;
1504 return true;
Chris Lattnera801fced2006-11-08 02:15:41 +00001505}
1506
1507//===----------------------------------------------------------------------===//
Chris Lattner4211ca92006-04-14 06:01:58 +00001508// LowerOperation implementation
1509//===----------------------------------------------------------------------===//
1510
Chris Lattneredb9d842010-11-15 02:46:57 +00001511/// GetLabelAccessInfo - Return true if we should reference labels using a
1512/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1513static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Craig Topper062a2ba2014-04-25 05:30:21 +00001514 unsigned &LoOpFlags,
1515 const GlobalValue *GV = nullptr) {
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001516 HiOpFlags = PPCII::MO_HA;
1517 LoOpFlags = PPCII::MO_LO;
Wesley Peck527da1b2010-11-23 03:31:01 +00001518
Hal Finkel3ee2af72014-07-18 23:29:49 +00001519 // Don't use the pic base if not in PIC relocation model.
1520 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
1521
Chris Lattnerdd6df842010-11-15 03:13:19 +00001522 if (isPIC) {
1523 HiOpFlags |= PPCII::MO_PIC_FLAG;
1524 LoOpFlags |= PPCII::MO_PIC_FLAG;
1525 }
1526
1527 // If this is a reference to a global value that requires a non-lazy-ptr, make
1528 // sure that instruction lowering adds it.
1529 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1530 HiOpFlags |= PPCII::MO_NLP_FLAG;
1531 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00001532
Chris Lattnerdd6df842010-11-15 03:13:19 +00001533 if (GV->hasHiddenVisibility()) {
1534 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1535 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1536 }
1537 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001538
Chris Lattneredb9d842010-11-15 02:46:57 +00001539 return isPIC;
1540}
1541
1542static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1543 SelectionDAG &DAG) {
1544 EVT PtrVT = HiPart.getValueType();
1545 SDValue Zero = DAG.getConstant(0, PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001546 SDLoc DL(HiPart);
Chris Lattneredb9d842010-11-15 02:46:57 +00001547
1548 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1549 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peck527da1b2010-11-23 03:31:01 +00001550
Chris Lattneredb9d842010-11-15 02:46:57 +00001551 // With PIC, the first instruction is actually "GR+hi(&G)".
1552 if (isPIC)
1553 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1554 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peck527da1b2010-11-23 03:31:01 +00001555
Chris Lattneredb9d842010-11-15 02:46:57 +00001556 // Generate non-pic code that has direct accesses to the constant pool.
1557 // The address of the global is just (hi(&g)+lo(&g)).
1558 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1559}
1560
Scott Michelcf0da6c2009-02-17 22:15:04 +00001561SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001562 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001563 EVT PtrVT = Op.getValueType();
Chris Lattner4211ca92006-04-14 06:01:58 +00001564 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001565 const Constant *C = CP->getConstVal();
Chris Lattner4211ca92006-04-14 06:01:58 +00001566
Roman Divackyace47072012-08-24 16:26:02 +00001567 // 64-bit SVR4 ABI code is always position-independent.
1568 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001569 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Roman Divackyace47072012-08-24 16:26:02 +00001570 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001571 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001572 DAG.getRegister(PPC::X2, MVT::i64));
1573 }
1574
Chris Lattneredb9d842010-11-15 02:46:57 +00001575 unsigned MOHiFlag, MOLoFlag;
1576 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001577
1578 if (isPIC && Subtarget.isSVR4ABI()) {
1579 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(),
1580 PPCII::MO_PIC_FLAG);
1581 SDLoc DL(CP);
1582 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i32, GA,
1583 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT));
1584 }
1585
Chris Lattneredb9d842010-11-15 02:46:57 +00001586 SDValue CPIHi =
1587 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1588 SDValue CPILo =
1589 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1590 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00001591}
1592
Dan Gohman21cea8a2010-04-17 15:26:15 +00001593SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001594 EVT PtrVT = Op.getValueType();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001595 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peck527da1b2010-11-23 03:31:01 +00001596
Roman Divackyace47072012-08-24 16:26:02 +00001597 // 64-bit SVR4 ABI code is always position-independent.
1598 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001599 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Roman Divackyace47072012-08-24 16:26:02 +00001600 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001601 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA,
Roman Divackyace47072012-08-24 16:26:02 +00001602 DAG.getRegister(PPC::X2, MVT::i64));
1603 }
1604
Chris Lattneredb9d842010-11-15 02:46:57 +00001605 unsigned MOHiFlag, MOLoFlag;
1606 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Hal Finkel3ee2af72014-07-18 23:29:49 +00001607
1608 if (isPIC && Subtarget.isSVR4ABI()) {
1609 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1610 PPCII::MO_PIC_FLAG);
1611 SDLoc DL(GA);
1612 return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), PtrVT, GA,
1613 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT));
1614 }
1615
Chris Lattneredb9d842010-11-15 02:46:57 +00001616 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1617 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1618 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio09d73c02007-07-11 17:19:51 +00001619}
1620
Dan Gohman21cea8a2010-04-17 15:26:15 +00001621SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1622 SelectionDAG &DAG) const {
Bob Wilsonf84f7102009-11-04 21:31:18 +00001623 EVT PtrVT = Op.getValueType();
Bob Wilsonf84f7102009-11-04 21:31:18 +00001624
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001625 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peck527da1b2010-11-23 03:31:01 +00001626
Chris Lattneredb9d842010-11-15 02:46:57 +00001627 unsigned MOHiFlag, MOLoFlag;
1628 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liaoabb87d42012-09-12 21:43:09 +00001629 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1630 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattneredb9d842010-11-15 02:46:57 +00001631 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1632}
1633
Roman Divackye3f15c982012-06-04 17:36:38 +00001634SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1635 SelectionDAG &DAG) const {
1636
Bill Schmidtbdae03f2013-09-17 20:22:05 +00001637 // FIXME: TLS addresses currently use medium model code sequences,
1638 // which is the most useful form. Eventually support for small and
1639 // large models could be added if users need it, at the cost of
1640 // additional complexity.
Roman Divackye3f15c982012-06-04 17:36:38 +00001641 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001642 SDLoc dl(GA);
Roman Divackye3f15c982012-06-04 17:36:38 +00001643 const GlobalValue *GV = GA->getGlobal();
1644 EVT PtrVT = getPointerTy();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001645 bool is64bit = Subtarget.isPPC64();
Roman Divackye3f15c982012-06-04 17:36:38 +00001646
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001647 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackye3f15c982012-06-04 17:36:38 +00001648
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001649 if (Model == TLSModel::LocalExec) {
1650 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001651 PPCII::MO_TPREL_HA);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001652 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001653 PPCII::MO_TPREL_LO);
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001654 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1655 is64bit ? MVT::i64 : MVT::i32);
1656 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1657 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1658 }
Roman Divackye3f15c982012-06-04 17:36:38 +00001659
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001660 if (Model == TLSModel::InitialExec) {
Bill Schmidt732eb912012-12-13 18:45:54 +00001661 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001662 SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1663 PPCII::MO_TLS);
Roman Divacky32143e22013-12-20 18:08:54 +00001664 SDValue GOTPtr;
1665 if (is64bit) {
1666 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1667 GOTPtr = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1668 PtrVT, GOTReg, TGA);
1669 } else
1670 GOTPtr = DAG.getNode(PPCISD::PPC32_GOT, dl, PtrVT);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +00001671 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
Roman Divacky32143e22013-12-20 18:08:54 +00001672 PtrVT, TGA, GOTPtr);
Ulrich Weigand5b427592013-07-05 12:22:36 +00001673 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001674 }
Bill Schmidtca4a0c92012-12-04 16:18:08 +00001675
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001676 if (Model == TLSModel::GeneralDynamic) {
1677 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001678 SDValue GOTPtr;
1679 if (is64bit) {
1680 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1681 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1682 GOTReg, TGA);
1683 } else {
1684 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
1685 }
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001686 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
Hal Finkel7c8ae532014-07-25 17:47:22 +00001687 GOTPtr, TGA);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001688
1689 // We need a chain node, and don't have one handy. The underlying
1690 // call has no side effects, so using the function entry node
1691 // suffices.
1692 SDValue Chain = DAG.getEntryNode();
Hal Finkel7c8ae532014-07-25 17:47:22 +00001693 Chain = DAG.getCopyToReg(Chain, dl,
1694 is64bit ? PPC::X3 : PPC::R3, GOTEntry);
1695 SDValue ParmReg = DAG.getRegister(is64bit ? PPC::X3 : PPC::R3,
1696 is64bit ? MVT::i64 : MVT::i32);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001697 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1698 PtrVT, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001699 // The return value from GET_TLS_ADDR really is in X3 already, but
1700 // some hacks are needed here to tie everything together. The extra
1701 // copies dissolve during subsequent transforms.
Hal Finkel7c8ae532014-07-25 17:47:22 +00001702 Chain = DAG.getCopyToReg(Chain, dl, is64bit ? PPC::X3 : PPC::R3, TLSAddr);
1703 return DAG.getCopyFromReg(Chain, dl, is64bit ? PPC::X3 : PPC::R3, PtrVT);
Bill Schmidtc56f1d32012-12-11 20:30:11 +00001704 }
1705
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001706 if (Model == TLSModel::LocalDynamic) {
1707 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
Hal Finkel7c8ae532014-07-25 17:47:22 +00001708 SDValue GOTPtr;
1709 if (is64bit) {
1710 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1711 GOTPtr = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1712 GOTReg, TGA);
1713 } else {
1714 GOTPtr = DAG.getNode(PPCISD::PPC32_PICGOT, dl, PtrVT);
1715 }
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001716 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
Hal Finkel7c8ae532014-07-25 17:47:22 +00001717 GOTPtr, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001718
1719 // We need a chain node, and don't have one handy. The underlying
1720 // call has no side effects, so using the function entry node
1721 // suffices.
1722 SDValue Chain = DAG.getEntryNode();
Hal Finkel7c8ae532014-07-25 17:47:22 +00001723 Chain = DAG.getCopyToReg(Chain, dl,
1724 is64bit ? PPC::X3 : PPC::R3, GOTEntry);
1725 SDValue ParmReg = DAG.getRegister(is64bit ? PPC::X3 : PPC::R3,
1726 is64bit ? MVT::i64 : MVT::i32);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001727 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1728 PtrVT, ParmReg, TGA);
1729 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1730 // some hacks are needed here to tie everything together. The extra
1731 // copies dissolve during subsequent transforms.
Hal Finkel7c8ae532014-07-25 17:47:22 +00001732 Chain = DAG.getCopyToReg(Chain, dl, is64bit ? PPC::X3 : PPC::R3, TLSAddr);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001733 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt9ed4dbc2012-12-13 20:57:10 +00001734 Chain, ParmReg, TGA);
Bill Schmidt24b8dd62012-12-12 19:29:35 +00001735 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1736 }
1737
1738 llvm_unreachable("Unknown TLS model!");
Roman Divackye3f15c982012-06-04 17:36:38 +00001739}
1740
Chris Lattneredb9d842010-11-15 02:46:57 +00001741SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1742 SelectionDAG &DAG) const {
1743 EVT PtrVT = Op.getValueType();
1744 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001745 SDLoc DL(GSDN);
Chris Lattneredb9d842010-11-15 02:46:57 +00001746 const GlobalValue *GV = GSDN->getGlobal();
1747
Chris Lattneredb9d842010-11-15 02:46:57 +00001748 // 64-bit SVR4 ABI code is always position-independent.
1749 // The actual address of the GlobalValue is stored in the TOC.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00001750 if (Subtarget.isSVR4ABI() && Subtarget.isPPC64()) {
Chris Lattneredb9d842010-11-15 02:46:57 +00001751 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1752 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1753 DAG.getRegister(PPC::X2, MVT::i64));
1754 }
1755
Chris Lattnerdd6df842010-11-15 03:13:19 +00001756 unsigned MOHiFlag, MOLoFlag;
1757 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattneredb9d842010-11-15 02:46:57 +00001758
Hal Finkel3ee2af72014-07-18 23:29:49 +00001759 if (isPIC && Subtarget.isSVR4ABI()) {
1760 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT,
1761 GSDN->getOffset(),
1762 PPCII::MO_PIC_FLAG);
1763 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i32, GA,
1764 DAG.getNode(PPCISD::GlobalBaseReg, DL, MVT::i32));
1765 }
1766
Chris Lattnerdd6df842010-11-15 03:13:19 +00001767 SDValue GAHi =
1768 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1769 SDValue GALo =
1770 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peck527da1b2010-11-23 03:31:01 +00001771
Chris Lattnerdd6df842010-11-15 03:13:19 +00001772 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00001773
Chris Lattnerdd6df842010-11-15 03:13:19 +00001774 // If the global reference is actually to a non-lazy-pointer, we have to do an
1775 // extra load to get the address of the global.
1776 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1777 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001778 false, false, false, 0);
Chris Lattnerdd6df842010-11-15 03:13:19 +00001779 return Ptr;
Chris Lattner4211ca92006-04-14 06:01:58 +00001780}
1781
Dan Gohman21cea8a2010-04-17 15:26:15 +00001782SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00001783 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001784 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001785
Hal Finkel777c9dd2014-03-29 16:04:40 +00001786 if (Op.getValueType() == MVT::v2i64) {
1787 // When the operands themselves are v2i64 values, we need to do something
1788 // special because VSX has no underlying comparison operations for these.
1789 if (Op.getOperand(0).getValueType() == MVT::v2i64) {
1790 // Equality can be handled by casting to the legal type for Altivec
1791 // comparisons, everything else needs to be expanded.
1792 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
1793 return DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
1794 DAG.getSetCC(dl, MVT::v4i32,
1795 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0)),
1796 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(1)),
1797 CC));
1798 }
1799
1800 return SDValue();
1801 }
1802
1803 // We handle most of these in the usual way.
1804 return Op;
1805 }
1806
Chris Lattner4211ca92006-04-14 06:01:58 +00001807 // If we're comparing for equality to zero, expose the fact that this is
1808 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1809 // fold the new nodes.
1810 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1811 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001812 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001813 SDValue Zext = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00001814 if (VT.bitsLT(MVT::i32)) {
1815 VT = MVT::i32;
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001816 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001817 }
Duncan Sands13237ac2008-06-06 12:08:01 +00001818 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001819 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1820 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson9f944592009-08-11 20:47:22 +00001821 DAG.getConstant(Log2b, MVT::i32));
1822 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner4211ca92006-04-14 06:01:58 +00001823 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001824 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner4211ca92006-04-14 06:01:58 +00001825 // optimized. FIXME: revisit this when we can custom lower all setcc
1826 // optimizations.
1827 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001828 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001829 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001830
Chris Lattner4211ca92006-04-14 06:01:58 +00001831 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattner97ff46b2006-11-14 05:28:08 +00001832 // by xor'ing the rhs with the lhs, which is faster than setting a
1833 // condition register, reading it back out, and masking the correct bit. The
1834 // normal approach here uses sub to do this instead of xor. Using xor exposes
1835 // the result to other bit-twiddling opportunities.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001836 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00001837 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001838 EVT VT = Op.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001839 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner4211ca92006-04-14 06:01:58 +00001840 Op.getOperand(1));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00001841 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner4211ca92006-04-14 06:01:58 +00001842 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001843 return SDValue();
Chris Lattner4211ca92006-04-14 06:01:58 +00001844}
1845
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001846SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001847 const PPCSubtarget &Subtarget) const {
Roman Divacky4394e682011-06-28 15:30:42 +00001848 SDNode *Node = Op.getNode();
1849 EVT VT = Node->getValueType(0);
1850 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1851 SDValue InChain = Node->getOperand(0);
1852 SDValue VAListPtr = Node->getOperand(1);
1853 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001854 SDLoc dl(Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001855
Roman Divacky4394e682011-06-28 15:30:42 +00001856 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1857
1858 // gpr_index
1859 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1860 VAListPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001861 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001862 InChain = GprIndex.getValue(1);
1863
1864 if (VT == MVT::i64) {
1865 // Check if GprIndex is even
1866 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1867 DAG.getConstant(1, MVT::i32));
1868 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1869 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1870 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1871 DAG.getConstant(1, MVT::i32));
1872 // Align GprIndex to be even if it isn't
1873 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1874 GprIndex);
1875 }
1876
1877 // fpr index is 1 byte after gpr
1878 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1879 DAG.getConstant(1, MVT::i32));
1880
1881 // fpr
1882 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1883 FprPtr, MachinePointerInfo(SV), MVT::i8,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001884 false, false, false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001885 InChain = FprIndex.getValue(1);
1886
1887 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1888 DAG.getConstant(8, MVT::i32));
1889
1890 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1891 DAG.getConstant(4, MVT::i32));
1892
1893 // areas
1894 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001895 MachinePointerInfo(), false, false,
1896 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001897 InChain = OverflowArea.getValue(1);
1898
1899 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001900 MachinePointerInfo(), false, false,
1901 false, 0);
Roman Divacky4394e682011-06-28 15:30:42 +00001902 InChain = RegSaveArea.getValue(1);
1903
1904 // select overflow_area if index > 8
1905 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1906 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1907
Roman Divacky4394e682011-06-28 15:30:42 +00001908 // adjustment constant gpr_index * 4/8
1909 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1910 VT.isInteger() ? GprIndex : FprIndex,
1911 DAG.getConstant(VT.isInteger() ? 4 : 8,
1912 MVT::i32));
1913
1914 // OurReg = RegSaveArea + RegConstant
1915 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1916 RegConstant);
1917
1918 // Floating types are 32 bytes into RegSaveArea
1919 if (VT.isFloatingPoint())
1920 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1921 DAG.getConstant(32, MVT::i32));
1922
1923 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1924 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1925 VT.isInteger() ? GprIndex : FprIndex,
1926 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1927 MVT::i32));
1928
1929 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1930 VT.isInteger() ? VAListPtr : FprPtr,
1931 MachinePointerInfo(SV),
1932 MVT::i8, false, false, 0);
1933
1934 // determine if we should load from reg_save_area or overflow_area
1935 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1936
1937 // increase overflow_area by 4/8 if gpr/fpr > 8
1938 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1939 DAG.getConstant(VT.isInteger() ? 4 : 8,
1940 MVT::i32));
1941
1942 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1943 OverflowAreaPlusN);
1944
1945 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1946 OverflowAreaPtr,
1947 MachinePointerInfo(),
1948 MVT::i32, false, false, 0);
1949
NAKAMURA Takumi8ad54e02012-08-30 15:52:23 +00001950 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001951 false, false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00001952}
1953
Roman Divackyc3825df2013-07-25 21:36:47 +00001954SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG,
1955 const PPCSubtarget &Subtarget) const {
1956 assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only");
1957
1958 // We have to copy the entire va_list struct:
1959 // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte
1960 return DAG.getMemcpy(Op.getOperand(0), Op,
1961 Op.getOperand(1), Op.getOperand(2),
1962 DAG.getConstant(12, MVT::i32), 8, false, true,
1963 MachinePointerInfo(), MachinePointerInfo());
1964}
1965
Duncan Sandsa0984362011-09-06 13:37:06 +00001966SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1967 SelectionDAG &DAG) const {
1968 return Op.getOperand(0);
1969}
1970
1971SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1972 SelectionDAG &DAG) const {
Bill Wendling95e1af22008-09-17 00:30:57 +00001973 SDValue Chain = Op.getOperand(0);
1974 SDValue Trmp = Op.getOperand(1); // trampoline
1975 SDValue FPtr = Op.getOperand(2); // nested function
1976 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickef9de2a2013-05-25 02:42:55 +00001977 SDLoc dl(Op);
Bill Wendling95e1af22008-09-17 00:30:57 +00001978
Owen Anderson53aa7a92009-08-10 22:56:29 +00001979 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00001980 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattner229907c2011-07-18 04:54:35 +00001981 Type *IntPtrTy =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001982 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruth7ec50852012-11-01 08:07:29 +00001983 *DAG.getContext());
Bill Wendling95e1af22008-09-17 00:30:57 +00001984
Scott Michelcf0da6c2009-02-17 22:15:04 +00001985 TargetLowering::ArgListTy Args;
Bill Wendling95e1af22008-09-17 00:30:57 +00001986 TargetLowering::ArgListEntry Entry;
1987
1988 Entry.Ty = IntPtrTy;
1989 Entry.Node = Trmp; Args.push_back(Entry);
1990
1991 // TrampSize == (isPPC64 ? 48 : 40);
1992 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson9f944592009-08-11 20:47:22 +00001993 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling95e1af22008-09-17 00:30:57 +00001994 Args.push_back(Entry);
1995
1996 Entry.Node = FPtr; Args.push_back(Entry);
1997 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001998
Bill Wendling95e1af22008-09-17 00:30:57 +00001999 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002000 TargetLowering::CallLoweringInfo CLI(DAG);
2001 CLI.setDebugLoc(dl).setChain(Chain)
2002 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002003 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
2004 std::move(Args), 0);
Bill Wendling95e1af22008-09-17 00:30:57 +00002005
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002006 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Duncan Sandsa0984362011-09-06 13:37:06 +00002007 return CallResult.second;
Bill Wendling95e1af22008-09-17 00:30:57 +00002008}
2009
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002010SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002011 const PPCSubtarget &Subtarget) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00002012 MachineFunction &MF = DAG.getMachineFunction();
2013 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2014
Andrew Trickef9de2a2013-05-25 02:42:55 +00002015 SDLoc dl(Op);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002016
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002017 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002018 // vastart just stores the address of the VarArgsFrameIndex slot into the
2019 // memory location argument.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002020 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002021 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002022 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner676c61d2010-09-21 18:41:36 +00002023 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2024 MachinePointerInfo(SV),
David Greene87a5abe2010-02-15 16:56:53 +00002025 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002026 }
2027
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002028 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002029 // We suppose the given va_list is already allocated.
2030 //
2031 // typedef struct {
2032 // char gpr; /* index into the array of 8 GPRs
2033 // * stored in the register save area
2034 // * gpr=0 corresponds to r3,
2035 // * gpr=1 to r4, etc.
2036 // */
2037 // char fpr; /* index into the array of 8 FPRs
2038 // * stored in the register save area
2039 // * fpr=0 corresponds to f1,
2040 // * fpr=1 to f2, etc.
2041 // */
2042 // char *overflow_arg_area;
2043 // /* location on stack that holds
2044 // * the next overflow argument
2045 // */
2046 // char *reg_save_area;
2047 // /* where r3:r10 and f1:f8 (if saved)
2048 // * are stored
2049 // */
2050 // } va_list[1];
2051
2052
Dan Gohman31ae5862010-04-17 14:41:14 +00002053 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
2054 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002055
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002056
Owen Anderson53aa7a92009-08-10 22:56:29 +00002057 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002058
Dan Gohman31ae5862010-04-17 14:41:14 +00002059 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
2060 PtrVT);
2061 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2062 PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002063
Duncan Sands13237ac2008-06-06 12:08:01 +00002064 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002065 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002066
Duncan Sands13237ac2008-06-06 12:08:01 +00002067 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002068 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002069
2070 uint64_t FPROffset = 1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002071 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002072
Dan Gohman2d489b52008-02-06 22:27:42 +00002073 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002074
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002075 // Store first byte : number of int regs
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002076 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattner6963c1f2010-09-21 17:42:31 +00002077 Op.getOperand(1),
2078 MachinePointerInfo(SV),
2079 MVT::i8, false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002080 uint64_t nextOffset = FPROffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002081 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002082 ConstFPROffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002083
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002084 // Store second byte : number of float regs
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002085 SDValue secondStore =
Chris Lattner6963c1f2010-09-21 17:42:31 +00002086 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
2087 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene87a5abe2010-02-15 16:56:53 +00002088 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002089 nextOffset += StackOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002090 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002091
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002092 // Store second word : arguments given on stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002093 SDValue thirdStore =
Chris Lattner676c61d2010-09-21 18:41:36 +00002094 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
2095 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002096 false, false, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00002097 nextOffset += FrameOffset;
Dale Johannesen021052a2009-02-04 20:06:27 +00002098 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002099
2100 // Store third word : arguments given in registers
Chris Lattner676c61d2010-09-21 18:41:36 +00002101 return DAG.getStore(thirdStore, dl, FR, nextPtr,
2102 MachinePointerInfo(SV, nextOffset),
David Greene87a5abe2010-02-15 16:56:53 +00002103 false, false, 0);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00002104
Chris Lattner4211ca92006-04-14 06:01:58 +00002105}
2106
Chris Lattner4f2e4e02007-03-06 00:59:59 +00002107#include "PPCGenCallingConv.inc"
2108
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002109// Function whose sole purpose is to kill compiler warnings
2110// stemming from unused functions included from PPCGenCallingConv.inc.
2111CCAssignFn *PPCTargetLowering::useFastISelCCs(unsigned Flag) const {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00002112 return Flag ? CC_PPC64_ELF_FIS : RetCC_PPC64_ELF_FIS;
Bill Schmidt8c3976e2013-08-26 20:11:46 +00002113}
2114
Bill Schmidt230b4512013-06-12 16:39:22 +00002115bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
2116 CCValAssign::LocInfo &LocInfo,
2117 ISD::ArgFlagsTy &ArgFlags,
2118 CCState &State) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002119 return true;
2120}
2121
Bill Schmidt230b4512013-06-12 16:39:22 +00002122bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
2123 MVT &LocVT,
2124 CCValAssign::LocInfo &LocInfo,
2125 ISD::ArgFlagsTy &ArgFlags,
2126 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002127 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002128 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2129 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2130 };
2131 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002132
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002133 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
2134
2135 // Skip one register if the first unallocated register has an even register
2136 // number and there are still argument registers available which have not been
2137 // allocated yet. RegNum is actually an index into ArgRegs, which means we
2138 // need to skip a register if RegNum is odd.
2139 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
2140 State.AllocateReg(ArgRegs[RegNum]);
2141 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002142
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002143 // Always return false here, as this function only makes sure that the first
2144 // unallocated register has an odd register number and does not actually
2145 // allocate a register for the current argument.
2146 return false;
2147}
2148
Bill Schmidt230b4512013-06-12 16:39:22 +00002149bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
2150 MVT &LocVT,
2151 CCValAssign::LocInfo &LocInfo,
2152 ISD::ArgFlagsTy &ArgFlags,
2153 CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002154 static const MCPhysReg ArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002155 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2156 PPC::F8
2157 };
2158
2159 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peck527da1b2010-11-23 03:31:01 +00002160
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002161 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
2162
2163 // If there is only one Floating-point register left we need to put both f64
2164 // values of a split ppc_fp128 value on the stack.
2165 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
2166 State.AllocateReg(ArgRegs[RegNum]);
2167 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002168
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002169 // Always return false here, as this function only makes sure that the two f64
2170 // values a ppc_fp128 value is split into are both passed in registers or both
2171 // passed on the stack and does not actually allocate a register for the
2172 // current argument.
2173 return false;
2174}
2175
Chris Lattner43df5b32007-02-25 05:34:32 +00002176/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002177/// on Darwin.
Craig Topper840beec2014-04-04 05:16:06 +00002178static const MCPhysReg *GetFPR() {
2179 static const MCPhysReg FPR[] = {
Chris Lattner43df5b32007-02-25 05:34:32 +00002180 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002181 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner43df5b32007-02-25 05:34:32 +00002182 };
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002183
Chris Lattner43df5b32007-02-25 05:34:32 +00002184 return FPR;
2185}
2186
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002187/// CalculateStackSlotSize - Calculates the size reserved for this argument on
2188/// the stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002189static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00002190 unsigned PtrByteSize) {
Hal Finkel940ab932014-02-28 00:27:01 +00002191 unsigned ArgSize = ArgVT.getStoreSize();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002192 if (Flags.isByVal())
2193 ArgSize = Flags.getByValSize();
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002194
2195 // Round up to multiples of the pointer size, except for array members,
2196 // which are always packed.
2197 if (!Flags.isInConsecutiveRegs())
2198 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002199
2200 return ArgSize;
2201}
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002202
2203/// CalculateStackSlotAlignment - Calculates the alignment of this argument
2204/// on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002205static unsigned CalculateStackSlotAlignment(EVT ArgVT, EVT OrigVT,
2206 ISD::ArgFlagsTy Flags,
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002207 unsigned PtrByteSize) {
2208 unsigned Align = PtrByteSize;
2209
2210 // Altivec parameters are padded to a 16 byte boundary.
2211 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2212 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
2213 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64)
2214 Align = 16;
2215
2216 // ByVal parameters are aligned as requested.
2217 if (Flags.isByVal()) {
2218 unsigned BVAlign = Flags.getByValAlign();
2219 if (BVAlign > PtrByteSize) {
2220 if (BVAlign % PtrByteSize != 0)
2221 llvm_unreachable(
2222 "ByVal alignment is not a multiple of the pointer size");
2223
2224 Align = BVAlign;
2225 }
2226 }
2227
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002228 // Array members are always packed to their original alignment.
2229 if (Flags.isInConsecutiveRegs()) {
2230 // If the array member was split into multiple registers, the first
2231 // needs to be aligned to the size of the full type. (Except for
2232 // ppcf128, which is only aligned as its f64 components.)
2233 if (Flags.isSplit() && OrigVT != MVT::ppcf128)
2234 Align = OrigVT.getStoreSize();
2235 else
2236 Align = ArgVT.getStoreSize();
2237 }
2238
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002239 return Align;
2240}
2241
Ulrich Weigand8658f172014-07-20 23:43:15 +00002242/// CalculateStackSlotUsed - Return whether this argument will use its
2243/// stack slot (instead of being passed in registers). ArgOffset,
2244/// AvailableFPRs, and AvailableVRs must hold the current argument
2245/// position, and will be updated to account for this argument.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002246static bool CalculateStackSlotUsed(EVT ArgVT, EVT OrigVT,
2247 ISD::ArgFlagsTy Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00002248 unsigned PtrByteSize,
2249 unsigned LinkageSize,
2250 unsigned ParamAreaSize,
2251 unsigned &ArgOffset,
2252 unsigned &AvailableFPRs,
2253 unsigned &AvailableVRs) {
2254 bool UseMemory = false;
2255
2256 // Respect alignment of argument on the stack.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002257 unsigned Align =
2258 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigand8658f172014-07-20 23:43:15 +00002259 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
2260 // If there's no space left in the argument save area, we must
2261 // use memory (this check also catches zero-sized arguments).
2262 if (ArgOffset >= LinkageSize + ParamAreaSize)
2263 UseMemory = true;
2264
2265 // Allocate argument on the stack.
2266 ArgOffset += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002267 if (Flags.isInConsecutiveRegsLast())
2268 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand8658f172014-07-20 23:43:15 +00002269 // If we overran the argument save area, we must use memory
2270 // (this check catches arguments passed partially in memory)
2271 if (ArgOffset > LinkageSize + ParamAreaSize)
2272 UseMemory = true;
2273
2274 // However, if the argument is actually passed in an FPR or a VR,
2275 // we don't use memory after all.
2276 if (!Flags.isByVal()) {
2277 if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
2278 if (AvailableFPRs > 0) {
2279 --AvailableFPRs;
2280 return false;
2281 }
2282 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
2283 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
2284 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64)
2285 if (AvailableVRs > 0) {
2286 --AvailableVRs;
2287 return false;
2288 }
2289 }
2290
2291 return UseMemory;
2292}
2293
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002294/// EnsureStackAlignment - Round stack frame size up from NumBytes to
2295/// ensure minimum alignment required for target.
2296static unsigned EnsureStackAlignment(const TargetMachine &Target,
2297 unsigned NumBytes) {
2298 unsigned TargetAlign = Target.getFrameLowering()->getStackAlignment();
2299 unsigned AlignMask = TargetAlign - 1;
2300 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2301 return NumBytes;
2302}
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002303
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002304SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002305PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002306 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002307 const SmallVectorImpl<ISD::InputArg>
2308 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002309 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002310 SmallVectorImpl<SDValue> &InVals)
2311 const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002312 if (Subtarget.isSVR4ABI()) {
2313 if (Subtarget.isPPC64())
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002314 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
2315 dl, DAG, InVals);
2316 else
2317 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
2318 dl, DAG, InVals);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002319 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002320 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
2321 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002322 }
2323}
2324
2325SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002326PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002327 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002328 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002329 const SmallVectorImpl<ISD::InputArg>
2330 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002331 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002332 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002333
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002334 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002335 // +-----------------------------------+
2336 // +--> | Back chain |
2337 // | +-----------------------------------+
2338 // | | Floating-point register save area |
2339 // | +-----------------------------------+
2340 // | | General register save area |
2341 // | +-----------------------------------+
2342 // | | CR save word |
2343 // | +-----------------------------------+
2344 // | | VRSAVE save word |
2345 // | +-----------------------------------+
2346 // | | Alignment padding |
2347 // | +-----------------------------------+
2348 // | | Vector register save area |
2349 // | +-----------------------------------+
2350 // | | Local variable space |
2351 // | +-----------------------------------+
2352 // | | Parameter list area |
2353 // | +-----------------------------------+
2354 // | | LR save word |
2355 // | +-----------------------------------+
2356 // SP--> +--- | Back chain |
2357 // +-----------------------------------+
2358 //
2359 // Specifications:
2360 // System V Application Binary Interface PowerPC Processor Supplement
2361 // AltiVec Technology Programming Interface Manual
Wesley Peck527da1b2010-11-23 03:31:01 +00002362
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002363 MachineFunction &MF = DAG.getMachineFunction();
2364 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002365 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002366
Owen Anderson53aa7a92009-08-10 22:56:29 +00002367 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002368 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002369 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2370 (CallConv == CallingConv::Fast));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002371 unsigned PtrByteSize = 4;
2372
2373 // Assign locations to all of the incoming arguments.
2374 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002375 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002376 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002377
2378 // Reserve space for the linkage area on the stack.
Ulrich Weigand8658f172014-07-20 23:43:15 +00002379 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(false, false, false);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002380 CCInfo.AllocateStack(LinkageSize, PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002381
Bill Schmidtef17c142013-02-06 17:33:58 +00002382 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peck527da1b2010-11-23 03:31:01 +00002383
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002384 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2385 CCValAssign &VA = ArgLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00002386
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002387 // Arguments stored in registers.
2388 if (VA.isRegLoc()) {
Craig Topper760b1342012-02-22 05:59:10 +00002389 const TargetRegisterClass *RC;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002390 EVT ValVT = VA.getValVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002391
Owen Anderson9f944592009-08-11 20:47:22 +00002392 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002393 default:
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002394 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Hal Finkel940ab932014-02-28 00:27:01 +00002395 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002396 case MVT::i32:
Craig Topperabadc662012-04-20 06:31:50 +00002397 RC = &PPC::GPRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002398 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002399 case MVT::f32:
Craig Topperabadc662012-04-20 06:31:50 +00002400 RC = &PPC::F4RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002401 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002402 case MVT::f64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002403 if (Subtarget.hasVSX())
Hal Finkel19be5062014-03-29 05:29:01 +00002404 RC = &PPC::VSFRCRegClass;
2405 else
2406 RC = &PPC::F8RCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002407 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002408 case MVT::v16i8:
2409 case MVT::v8i16:
2410 case MVT::v4i32:
2411 case MVT::v4f32:
Hal Finkel7811c612014-03-28 19:58:11 +00002412 RC = &PPC::VRRCRegClass;
2413 break;
Hal Finkel27774d92014-03-13 07:58:58 +00002414 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002415 case MVT::v2i64:
Hal Finkel7811c612014-03-28 19:58:11 +00002416 RC = &PPC::VSHRCRegClass;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002417 break;
2418 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002419
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002420 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002421 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Hal Finkel940ab932014-02-28 00:27:01 +00002422 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2423 ValVT == MVT::i1 ? MVT::i32 : ValVT);
2424
2425 if (ValVT == MVT::i1)
2426 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002427
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002428 InVals.push_back(ArgValue);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002429 } else {
2430 // Argument stored in memory.
2431 assert(VA.isMemLoc());
2432
Hal Finkel940ab932014-02-28 00:27:01 +00002433 unsigned ArgSize = VA.getLocVT().getStoreSize();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002434 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Cheng0664a672010-07-03 00:40:23 +00002435 isImmutable);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002436
2437 // Create load nodes to retrieve arguments from the stack.
2438 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00002439 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2440 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002441 false, false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002442 }
2443 }
2444
2445 // Assign locations to all of the incoming aggregate by value arguments.
2446 // Aggregates passed by value are stored in the local variable space of the
2447 // caller's stack frame, right above the parameter list area.
2448 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002449 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00002450 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002451
2452 // Reserve stack space for the allocations in CCInfo.
2453 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2454
Bill Schmidtef17c142013-02-06 17:33:58 +00002455 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002456
2457 // Area that is at least reserved in the caller of this function.
2458 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002459 MinReservedArea = std::max(MinReservedArea, LinkageSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00002460
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002461 // Set the size that is at least reserved in caller of this function. Tail
2462 // call optimized function's reserved stack space needs to be aligned so that
2463 // taking the difference between two stack areas will result in an aligned
2464 // stack.
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002465 MinReservedArea = EnsureStackAlignment(MF.getTarget(), MinReservedArea);
2466 FuncInfo->setMinReservedArea(MinReservedArea);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002467
2468 SmallVector<SDValue, 8> MemOps;
Wesley Peck527da1b2010-11-23 03:31:01 +00002469
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002470 // If the function takes variable number of arguments, make a frame index for
2471 // the start of the first vararg value... for expansion of llvm.va_start.
2472 if (isVarArg) {
Craig Topper840beec2014-04-04 05:16:06 +00002473 static const MCPhysReg GPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002474 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2475 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2476 };
2477 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2478
Craig Topper840beec2014-04-04 05:16:06 +00002479 static const MCPhysReg FPArgRegs[] = {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002480 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2481 PPC::F8
2482 };
2483 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2484
Dan Gohman31ae5862010-04-17 14:41:14 +00002485 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2486 NumGPArgRegs));
2487 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2488 NumFPArgRegs));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002489
2490 // Make room for NumGPArgRegs and NumFPArgRegs.
2491 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson9f944592009-08-11 20:47:22 +00002492 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002493
Dan Gohman31ae5862010-04-17 14:41:14 +00002494 FuncInfo->setVarArgsStackOffset(
2495 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00002496 CCInfo.getNextStackOffset(), true));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002497
Dan Gohman31ae5862010-04-17 14:41:14 +00002498 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2499 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002500
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002501 // The fixed integer arguments of a variadic function are stored to the
2502 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2503 // the result of va_next.
2504 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2505 // Get an existing live-in vreg, or add a new one.
2506 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2507 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002508 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002509
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002510 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00002511 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2512 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002513 MemOps.push_back(Store);
2514 // Increment the address by four for the next argument to store
2515 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2516 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2517 }
2518
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00002519 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2520 // is set.
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002521 // The double arguments are stored to the VarArgsFrameIndex
2522 // on the stack.
Jakob Stoklund Olesen6c4353e2010-10-11 20:43:09 +00002523 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2524 // Get an existing live-in vreg, or add a new one.
2525 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2526 if (!VReg)
Devang Patelf3292b22011-02-21 23:21:26 +00002527 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002528
Owen Anderson9f944592009-08-11 20:47:22 +00002529 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner676c61d2010-09-21 18:41:36 +00002530 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2531 MachinePointerInfo(), false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002532 MemOps.push_back(Store);
2533 // Increment the address by eight for the next argument to store
Owen Anderson9f944592009-08-11 20:47:22 +00002534 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002535 PtrVT);
2536 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2537 }
2538 }
2539
2540 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002541 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002542
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002543 return Chain;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002544}
2545
Bill Schmidt57d6de52012-10-23 15:51:16 +00002546// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2547// value to MVT::i64 and then truncate to the correct register size.
2548SDValue
2549PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2550 SelectionDAG &DAG, SDValue ArgVal,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002551 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00002552 if (Flags.isSExt())
2553 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2554 DAG.getValueType(ObjectVT));
2555 else if (Flags.isZExt())
2556 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2557 DAG.getValueType(ObjectVT));
Matt Arsenault758659232013-05-18 00:21:46 +00002558
Hal Finkel940ab932014-02-28 00:27:01 +00002559 return DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, ArgVal);
Bill Schmidt57d6de52012-10-23 15:51:16 +00002560}
2561
Tilmann Schellerb93960d2009-07-03 06:45:56 +00002562SDValue
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002563PPCTargetLowering::LowerFormalArguments_64SVR4(
2564 SDValue Chain,
2565 CallingConv::ID CallConv, bool isVarArg,
2566 const SmallVectorImpl<ISD::InputArg>
2567 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002568 SDLoc dl, SelectionDAG &DAG,
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002569 SmallVectorImpl<SDValue> &InVals) const {
2570 // TODO: add description of PPC stack frame format, or at least some docs.
2571 //
Ulrich Weigand8658f172014-07-20 23:43:15 +00002572 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00002573 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002574 MachineFunction &MF = DAG.getMachineFunction();
2575 MachineFrameInfo *MFI = MF.getFrameInfo();
2576 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2577
2578 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2579 // Potential tail calls could cause overwriting of argument stack slots.
2580 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2581 (CallConv == CallingConv::Fast));
2582 unsigned PtrByteSize = 8;
2583
Ulrich Weigand8658f172014-07-20 23:43:15 +00002584 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(true, false,
2585 isELFv2ABI);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002586
Craig Topper840beec2014-04-04 05:16:06 +00002587 static const MCPhysReg GPR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002588 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2589 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2590 };
2591
Craig Topper840beec2014-04-04 05:16:06 +00002592 static const MCPhysReg *FPR = GetFPR();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002593
Craig Topper840beec2014-04-04 05:16:06 +00002594 static const MCPhysReg VR[] = {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002595 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2596 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2597 };
Craig Topper840beec2014-04-04 05:16:06 +00002598 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00002599 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
2600 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
2601 };
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002602
2603 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2604 const unsigned Num_FPR_Regs = 13;
2605 const unsigned Num_VR_Regs = array_lengthof(VR);
2606
Ulrich Weigand8658f172014-07-20 23:43:15 +00002607 // Do a first pass over the arguments to determine whether the ABI
2608 // guarantees that our caller has allocated the parameter save area
2609 // on its stack frame. In the ELFv1 ABI, this is always the case;
2610 // in the ELFv2 ABI, it is true if this is a vararg function or if
2611 // any parameter is located in a stack slot.
2612
2613 bool HasParameterArea = !isELFv2ABI || isVarArg;
2614 unsigned ParamAreaSize = Num_GPR_Regs * PtrByteSize;
2615 unsigned NumBytes = LinkageSize;
2616 unsigned AvailableFPRs = Num_FPR_Regs;
2617 unsigned AvailableVRs = Num_VR_Regs;
2618 for (unsigned i = 0, e = Ins.size(); i != e; ++i)
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002619 if (CalculateStackSlotUsed(Ins[i].VT, Ins[i].ArgVT, Ins[i].Flags,
Ulrich Weigand8658f172014-07-20 23:43:15 +00002620 PtrByteSize, LinkageSize, ParamAreaSize,
2621 NumBytes, AvailableFPRs, AvailableVRs))
2622 HasParameterArea = true;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002623
2624 // Add DAG nodes to load the arguments or copy them out of registers. On
2625 // entry to a function on PPC, the arguments start after the linkage area,
2626 // although the first ones are often in registers.
2627
Ulrich Weigand8658f172014-07-20 23:43:15 +00002628 unsigned ArgOffset = LinkageSize;
2629 unsigned GPR_idx, FPR_idx = 0, VR_idx = 0;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002630 SmallVector<SDValue, 8> MemOps;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002631 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt6631e942013-02-20 17:31:41 +00002632 unsigned CurArgIdx = 0;
2633 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002634 SDValue ArgVal;
2635 bool needsLoad = false;
2636 EVT ObjectVT = Ins[ArgNo].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002637 EVT OrigVT = Ins[ArgNo].ArgVT;
Hal Finkel940ab932014-02-28 00:27:01 +00002638 unsigned ObjSize = ObjectVT.getStoreSize();
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002639 unsigned ArgSize = ObjSize;
2640 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt6631e942013-02-20 17:31:41 +00002641 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2642 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002643
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002644 /* Respect alignment of argument on the stack. */
2645 unsigned Align =
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002646 CalculateStackSlotAlignment(ObjectVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002647 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002648 unsigned CurArgOffset = ArgOffset;
2649
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002650 /* Compute GPR index associated with argument offset. */
2651 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
2652 GPR_idx = std::min(GPR_idx, Num_GPR_Regs);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002653
2654 // FIXME the codegen can be much improved in some cases.
2655 // We do not have to keep everything in memory.
2656 if (Flags.isByVal()) {
2657 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2658 ObjSize = Flags.getByValSize();
2659 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt9953cf22012-10-31 01:15:05 +00002660 // Empty aggregate parameters do not take up registers. Examples:
2661 // struct { } a;
2662 // union { } b;
2663 // int c[0];
2664 // etc. However, we have to provide a place-holder in InVals, so
2665 // pretend we have an 8-byte item at the current address for that
2666 // purpose.
2667 if (!ObjSize) {
2668 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2669 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2670 InVals.push_back(FIN);
2671 continue;
2672 }
Hal Finkel262a2242013-09-12 23:20:06 +00002673
Ulrich Weigand24195972014-07-20 22:36:52 +00002674 // Create a stack object covering all stack doublewords occupied
Ulrich Weigand8658f172014-07-20 23:43:15 +00002675 // by the argument. If the argument is (fully or partially) on
2676 // the stack, or if the argument is fully in registers but the
2677 // caller has allocated the parameter save anyway, we can refer
2678 // directly to the caller's stack frame. Otherwise, create a
2679 // local copy in our own frame.
2680 int FI;
2681 if (HasParameterArea ||
2682 ArgSize + ArgOffset > LinkageSize + Num_GPR_Regs * PtrByteSize)
Ulrich Weigand08760682014-08-01 14:35:58 +00002683 FI = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Ulrich Weigand8658f172014-07-20 23:43:15 +00002684 else
2685 FI = MFI->CreateStackObject(ArgSize, Align, false);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002686 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002687
Ulrich Weigand24195972014-07-20 22:36:52 +00002688 // Handle aggregates smaller than 8 bytes.
2689 if (ObjSize < PtrByteSize) {
2690 // The value of the object is its address, which differs from the
2691 // address of the enclosing doubleword on big-endian systems.
2692 SDValue Arg = FIN;
2693 if (!isLittleEndian) {
2694 SDValue ArgOff = DAG.getConstant(PtrByteSize - ObjSize, PtrVT);
2695 Arg = DAG.getNode(ISD::ADD, dl, ArgOff.getValueType(), Arg, ArgOff);
2696 }
2697 InVals.push_back(Arg);
2698
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002699 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002700 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002701 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002702 SDValue Store;
2703
2704 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2705 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2706 (ObjSize == 2 ? MVT::i16 : MVT::i32));
Ulrich Weigand24195972014-07-20 22:36:52 +00002707 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, Arg,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002708 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002709 ObjType, false, false, 0);
2710 } else {
2711 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2712 // store the whole register as-is to the parameter save area
Ulrich Weigand24195972014-07-20 22:36:52 +00002713 // slot.
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002714 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00002715 MachinePointerInfo(FuncArg),
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002716 false, false, 0);
2717 }
2718
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002719 MemOps.push_back(Store);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002720 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002721 // Whether we copied from a register or not, advance the offset
2722 // into the parameter save area by a full doubleword.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002723 ArgOffset += PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002724 continue;
2725 }
Bill Schmidt6ed3b992012-10-25 13:38:09 +00002726
Ulrich Weigand24195972014-07-20 22:36:52 +00002727 // The value of the object is its address, which is the address of
2728 // its first stack doubleword.
2729 InVals.push_back(FIN);
2730
2731 // Store whatever pieces of the object are in registers to memory.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002732 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
Ulrich Weigand24195972014-07-20 22:36:52 +00002733 if (GPR_idx == Num_GPR_Regs)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002734 break;
Ulrich Weigand24195972014-07-20 22:36:52 +00002735
2736 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2737 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2738 SDValue Addr = FIN;
2739 if (j) {
2740 SDValue Off = DAG.getConstant(j, PtrVT);
2741 Addr = DAG.getNode(ISD::ADD, dl, Off.getValueType(), Addr, Off);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002742 }
Ulrich Weigand24195972014-07-20 22:36:52 +00002743 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, Addr,
2744 MachinePointerInfo(FuncArg, j),
2745 false, false, 0);
2746 MemOps.push_back(Store);
2747 ++GPR_idx;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002748 }
Ulrich Weigand24195972014-07-20 22:36:52 +00002749 ArgOffset += ArgSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002750 continue;
2751 }
2752
2753 switch (ObjectVT.getSimpleVT().SimpleTy) {
2754 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel940ab932014-02-28 00:27:01 +00002755 case MVT::i1:
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002756 case MVT::i32:
2757 case MVT::i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002758 // These can be scalar arguments or elements of an integer array type
2759 // passed directly. Clang may use those instead of "byval" aggregate
2760 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002761 if (GPR_idx != Num_GPR_Regs) {
2762 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2763 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2764
Hal Finkel940ab932014-02-28 00:27:01 +00002765 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002766 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2767 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00002768 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002769 } else {
2770 needsLoad = true;
2771 ArgSize = PtrByteSize;
2772 }
2773 ArgOffset += 8;
2774 break;
2775
2776 case MVT::f32:
2777 case MVT::f64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002778 // These can be scalar arguments or elements of a float array type
2779 // passed directly. The latter are used to implement ELFv2 homogenous
2780 // float aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002781 if (FPR_idx != Num_FPR_Regs) {
2782 unsigned VReg;
2783
2784 if (ObjectVT == MVT::f32)
2785 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2786 else
Eric Christopherb1aaebe2014-06-12 22:38:18 +00002787 VReg = MF.addLiveIn(FPR[FPR_idx], Subtarget.hasVSX() ?
Hal Finkel19be5062014-03-29 05:29:01 +00002788 &PPC::VSFRCRegClass :
2789 &PPC::F8RCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002790
2791 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2792 ++FPR_idx;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002793 } else if (GPR_idx != Num_GPR_Regs) {
2794 // This can only ever happen in the presence of f32 array types,
2795 // since otherwise we never run out of FPRs before running out
2796 // of GPRs.
2797 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2798 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2799
2800 if (ObjectVT == MVT::f32) {
2801 if ((ArgOffset % PtrByteSize) == (isLittleEndian ? 4 : 0))
2802 ArgVal = DAG.getNode(ISD::SRL, dl, MVT::i64, ArgVal,
2803 DAG.getConstant(32, MVT::i32));
2804 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
2805 }
2806
2807 ArgVal = DAG.getNode(ISD::BITCAST, dl, ObjectVT, ArgVal);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002808 } else {
2809 needsLoad = true;
2810 }
2811
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002812 // When passing an array of floats, the array occupies consecutive
2813 // space in the argument area; only round up to the next doubleword
2814 // at the end of the array. Otherwise, each float takes 8 bytes.
2815 ArgSize = Flags.isInConsecutiveRegs() ? ObjSize : PtrByteSize;
2816 ArgOffset += ArgSize;
2817 if (Flags.isInConsecutiveRegsLast())
2818 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002819 break;
2820 case MVT::v4f32:
2821 case MVT::v4i32:
2822 case MVT::v8i16:
2823 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00002824 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00002825 case MVT::v2i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00002826 // These can be scalar arguments or elements of a vector array type
2827 // passed directly. The latter are used to implement ELFv2 homogenous
2828 // vector aggregates.
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002829 if (VR_idx != Num_VR_Regs) {
Hal Finkel7811c612014-03-28 19:58:11 +00002830 unsigned VReg = (ObjectVT == MVT::v2f64 || ObjectVT == MVT::v2i64) ?
2831 MF.addLiveIn(VSRH[VR_idx], &PPC::VSHRCRegClass) :
2832 MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002833 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002834 ++VR_idx;
2835 } else {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002836 needsLoad = true;
2837 }
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00002838 ArgOffset += 16;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002839 break;
2840 }
2841
2842 // We need to load the argument to a virtual register if we determined
2843 // above that we ran out of physical registers of the appropriate type.
2844 if (needsLoad) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00002845 if (ObjSize < ArgSize && !isLittleEndian)
2846 CurArgOffset += ArgSize - ObjSize;
2847 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, isImmutable);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002848 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2849 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2850 false, false, false, 0);
2851 }
2852
2853 InVals.push_back(ArgVal);
2854 }
2855
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002856 // Area that is at least reserved in the caller of this function.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002857 unsigned MinReservedArea;
Ulrich Weigand8658f172014-07-20 23:43:15 +00002858 if (HasParameterArea)
2859 MinReservedArea = std::max(ArgOffset, LinkageSize + 8 * PtrByteSize);
2860 else
2861 MinReservedArea = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002862
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002863 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00002864 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002865 // taking the difference between two stack areas will result in an aligned
2866 // stack.
Ulrich Weigand2bffb952014-06-23 13:08:27 +00002867 MinReservedArea = EnsureStackAlignment(MF.getTarget(), MinReservedArea);
2868 FuncInfo->setMinReservedArea(MinReservedArea);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002869
2870 // If the function takes variable number of arguments, make a frame index for
2871 // the start of the first vararg value... for expansion of llvm.va_start.
2872 if (isVarArg) {
2873 int Depth = ArgOffset;
2874
2875 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt57d6de52012-10-23 15:51:16 +00002876 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002877 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2878
2879 // If this function is vararg, store any remaining integer argument regs
2880 // to their spots on the stack so that they may be loaded by deferencing the
2881 // result of va_next.
Ulrich Weigandec2bf932014-07-07 19:26:41 +00002882 for (GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
2883 GPR_idx < Num_GPR_Regs; ++GPR_idx) {
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002884 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2885 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2886 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2887 MachinePointerInfo(), false, false, 0);
2888 MemOps.push_back(Store);
2889 // Increment the address by four for the next argument to store
Bill Schmidt57d6de52012-10-23 15:51:16 +00002890 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002891 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2892 }
2893 }
2894
2895 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002896 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00002897
2898 return Chain;
2899}
2900
2901SDValue
2902PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002903 SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002904 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002905 const SmallVectorImpl<ISD::InputArg>
2906 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002907 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002908 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002909 // TODO: add description of PPC stack frame format, or at least some docs.
2910 //
2911 MachineFunction &MF = DAG.getMachineFunction();
2912 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00002913 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002914
Owen Anderson53aa7a92009-08-10 22:56:29 +00002915 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00002916 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002917 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002918 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2919 (CallConv == CallingConv::Fast));
Jim Laskeyf4e2e002006-11-28 14:53:52 +00002920 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey48850c12006-11-16 22:43:37 +00002921
Ulrich Weigand8658f172014-07-20 23:43:15 +00002922 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(isPPC64, true,
2923 false);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00002924 unsigned ArgOffset = LinkageSize;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00002925 // Area that is at least reserved in caller of this function.
2926 unsigned MinReservedArea = ArgOffset;
2927
Craig Topper840beec2014-04-04 05:16:06 +00002928 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattner4302e8f2006-05-16 18:18:50 +00002929 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2930 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2931 };
Craig Topper840beec2014-04-04 05:16:06 +00002932 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00002933 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2934 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2935 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00002936
Craig Topper840beec2014-04-04 05:16:06 +00002937 static const MCPhysReg *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002938
Craig Topper840beec2014-04-04 05:16:06 +00002939 static const MCPhysReg VR[] = {
Chris Lattner4302e8f2006-05-16 18:18:50 +00002940 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2941 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2942 };
Chris Lattnerec78cad2006-06-26 22:48:35 +00002943
Owen Andersone2f23a32007-09-07 04:06:50 +00002944 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00002945 const unsigned Num_FPR_Regs = 13;
Owen Andersone2f23a32007-09-07 04:06:50 +00002946 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey48850c12006-11-16 22:43:37 +00002947
2948 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002949
Craig Topper840beec2014-04-04 05:16:06 +00002950 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002951
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002952 // In 32-bit non-varargs functions, the stack space for vectors is after the
2953 // stack space for non-vectors. We do not use this space unless we have
2954 // too many vectors to fit in registers, something that only occurs in
Scott Michelcf0da6c2009-02-17 22:15:04 +00002955 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002956 // that out...for the pathological case, compute VecArgOffset as the
2957 // start of the vector parameter area. Computing VecArgOffset is the
2958 // entire point of the following loop.
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002959 unsigned VecArgOffset = ArgOffset;
2960 if (!isVarArg && !isPPC64) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002961 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002962 ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002963 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002964 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002965
Duncan Sandsd97eea32008-03-21 09:14:45 +00002966 if (Flags.isByVal()) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002967 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer084b9f42012-01-20 14:42:32 +00002968 unsigned ObjSize = Flags.getByValSize();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002969 unsigned ArgSize =
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002970 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2971 VecArgOffset += ArgSize;
2972 continue;
2973 }
2974
Owen Anderson9f944592009-08-11 20:47:22 +00002975 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002976 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00002977 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00002978 case MVT::i32:
2979 case MVT::f32:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002980 VecArgOffset += 4;
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002981 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002982 case MVT::i64: // PPC64
2983 case MVT::f64:
Bill Schmidt019cc6f2012-09-19 15:42:13 +00002984 // FIXME: We are guaranteed to be !isPPC64 at this point.
2985 // Does MVT::i64 apply?
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002986 VecArgOffset += 8;
2987 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002988 case MVT::v4f32:
2989 case MVT::v4i32:
2990 case MVT::v8i16:
2991 case MVT::v16i8:
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00002992 // Nothing to do, we're only looking at Nonvector args here.
2993 break;
2994 }
2995 }
2996 }
2997 // We've found where the vector parameter area in memory is. Skip the
2998 // first 12 parameters; these don't use that memory.
2999 VecArgOffset = ((VecArgOffset+15)/16)*16;
3000 VecArgOffset += 12*16;
3001
Chris Lattner4302e8f2006-05-16 18:18:50 +00003002 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey48850c12006-11-16 22:43:37 +00003003 // entry to a function on PPC, the arguments start after the linkage area,
3004 // although the first ones are often in registers.
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00003005
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003006 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003007 unsigned nAltivecParamsAtEnd = 0;
Roman Divackyca103892012-09-24 20:47:19 +00003008 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt38b6cb52013-05-08 17:22:33 +00003009 unsigned CurArgIdx = 0;
3010 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003011 SDValue ArgVal;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003012 bool needsLoad = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00003013 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00003014 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey152671f2006-11-29 13:37:09 +00003015 unsigned ArgSize = ObjSize;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003016 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt38b6cb52013-05-08 17:22:33 +00003017 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
3018 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003019
Chris Lattner318f0d22006-05-16 18:51:52 +00003020 unsigned CurArgOffset = ArgOffset;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003021
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003022 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson9f944592009-08-11 20:47:22 +00003023 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
3024 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003025 if (isVarArg || isPPC64) {
3026 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003027 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003028 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003029 PtrByteSize);
3030 } else nAltivecParamsAtEnd++;
3031 } else
3032 // Calculate min reserved area.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003033 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohmand3fe1742008-09-13 01:54:27 +00003034 Flags,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003035 PtrByteSize);
3036
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003037 // FIXME the codegen can be much improved in some cases.
3038 // We do not have to keep everything in memory.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003039 if (Flags.isByVal()) {
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003040 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sandsd97eea32008-03-21 09:14:45 +00003041 ObjSize = Flags.getByValSize();
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003042 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003043 // Objects of size 1 and 2 are right justified, everything else is
3044 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen21a8f142008-03-08 01:41:42 +00003045 if (ObjSize==1 || ObjSize==2) {
3046 CurArgOffset = CurArgOffset + (4 - ObjSize);
3047 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003048 // The value of the object is its address.
Evan Cheng0664a672010-07-03 00:40:23 +00003049 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003050 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003051 InVals.push_back(FIN);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003052 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen21a8f142008-03-08 01:41:42 +00003053 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003054 unsigned VReg;
3055 if (isPPC64)
3056 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3057 else
3058 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003059 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt57d6de52012-10-23 15:51:16 +00003060 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003061 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003062 MachinePointerInfo(FuncArg),
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003063 ObjType, false, false, 0);
Dale Johannesen21a8f142008-03-08 01:41:42 +00003064 MemOps.push_back(Store);
3065 ++GPR_idx;
Dale Johannesen21a8f142008-03-08 01:41:42 +00003066 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003067
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003068 ArgOffset += PtrByteSize;
Wesley Peck527da1b2010-11-23 03:31:01 +00003069
Dale Johannesen21a8f142008-03-08 01:41:42 +00003070 continue;
3071 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003072 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
3073 // Store whatever pieces of the object are in registers
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003074 // to memory. ArgOffset will be the address of the beginning
3075 // of the object.
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003076 if (GPR_idx != Num_GPR_Regs) {
Roman Divackyd0419622011-06-17 15:21:10 +00003077 unsigned VReg;
3078 if (isPPC64)
3079 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
3080 else
3081 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Cheng0664a672010-07-03 00:40:23 +00003082 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003083 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003084 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtd1fa36f2012-10-05 21:27:08 +00003085 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Hal Finkel3e4a34c2014-01-21 20:15:58 +00003086 MachinePointerInfo(FuncArg, j),
David Greene87a5abe2010-02-15 16:56:53 +00003087 false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003088 MemOps.push_back(Store);
3089 ++GPR_idx;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003090 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003091 } else {
3092 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
3093 break;
3094 }
3095 }
3096 continue;
3097 }
3098
Owen Anderson9f944592009-08-11 20:47:22 +00003099 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003100 default: llvm_unreachable("Unhandled argument type!");
Hal Finkel5cae2162014-02-28 01:17:25 +00003101 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00003102 case MVT::i32:
Bill Wendling968f32c2008-03-07 20:49:02 +00003103 if (!isPPC64) {
Bill Wendling968f32c2008-03-07 20:49:02 +00003104 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003105 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003106 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Hal Finkel7f908e82014-03-06 00:45:19 +00003107
3108 if (ObjectVT == MVT::i1)
3109 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, ArgVal);
3110
Bill Wendling968f32c2008-03-07 20:49:02 +00003111 ++GPR_idx;
3112 } else {
3113 needsLoad = true;
3114 ArgSize = PtrByteSize;
3115 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003116 // All int arguments reserve stack space in the Darwin ABI.
3117 ArgOffset += PtrByteSize;
Bill Wendling968f32c2008-03-07 20:49:02 +00003118 break;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003119 }
Bill Wendling968f32c2008-03-07 20:49:02 +00003120 // FALLTHROUGH
Owen Anderson9f944592009-08-11 20:47:22 +00003121 case MVT::i64: // PPC64
Chris Lattnerec78cad2006-06-26 22:48:35 +00003122 if (GPR_idx != Num_GPR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003123 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003124 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling968f32c2008-03-07 20:49:02 +00003125
Hal Finkel940ab932014-02-28 00:27:01 +00003126 if (ObjectVT == MVT::i32 || ObjectVT == MVT::i1)
Bill Wendling968f32c2008-03-07 20:49:02 +00003127 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson9f944592009-08-11 20:47:22 +00003128 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt57d6de52012-10-23 15:51:16 +00003129 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling968f32c2008-03-07 20:49:02 +00003130
Chris Lattnerec78cad2006-06-26 22:48:35 +00003131 ++GPR_idx;
3132 } else {
3133 needsLoad = true;
Evan Cheng0f0aee22008-07-24 08:17:07 +00003134 ArgSize = PtrByteSize;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003135 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003136 // All int arguments reserve stack space in the Darwin ABI.
3137 ArgOffset += 8;
Chris Lattnerec78cad2006-06-26 22:48:35 +00003138 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003139
Owen Anderson9f944592009-08-11 20:47:22 +00003140 case MVT::f32:
3141 case MVT::f64:
Chris Lattner318f0d22006-05-16 18:51:52 +00003142 // Every 4 bytes of argument space consumes one of the GPRs available for
3143 // argument passing.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003144 if (GPR_idx != Num_GPR_Regs) {
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003145 ++GPR_idx;
Chris Lattner2cca3852006-11-18 01:57:19 +00003146 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003147 ++GPR_idx;
Chris Lattner318f0d22006-05-16 18:51:52 +00003148 }
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003149 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner4302e8f2006-05-16 18:18:50 +00003150 unsigned VReg;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003151
Owen Anderson9f944592009-08-11 20:47:22 +00003152 if (ObjectVT == MVT::f32)
Devang Patelf3292b22011-02-21 23:21:26 +00003153 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003154 else
Devang Patelf3292b22011-02-21 23:21:26 +00003155 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00003156
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003157 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003158 ++FPR_idx;
3159 } else {
3160 needsLoad = true;
3161 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003162
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003163 // All FP arguments reserve stack space in the Darwin ABI.
3164 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003165 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003166 case MVT::v4f32:
3167 case MVT::v4i32:
3168 case MVT::v8i16:
3169 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00003170 // Note that vector arguments in registers don't reserve stack space,
3171 // except in varargs functions.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003172 if (VR_idx != Num_VR_Regs) {
Devang Patelf3292b22011-02-21 23:21:26 +00003173 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003174 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesenb28456e2008-03-12 00:22:17 +00003175 if (isVarArg) {
3176 while ((ArgOffset % 16) != 0) {
3177 ArgOffset += PtrByteSize;
3178 if (GPR_idx != Num_GPR_Regs)
3179 GPR_idx++;
3180 }
3181 ArgOffset += 16;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003182 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesenb28456e2008-03-12 00:22:17 +00003183 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003184 ++VR_idx;
3185 } else {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00003186 if (!isVarArg && !isPPC64) {
3187 // Vectors go after all the nonvectors.
3188 CurArgOffset = VecArgOffset;
3189 VecArgOffset += 16;
3190 } else {
3191 // Vectors are aligned.
3192 ArgOffset = ((ArgOffset+15)/16)*16;
3193 CurArgOffset = ArgOffset;
3194 ArgOffset += 16;
Dale Johannesen0d982562008-03-12 00:49:20 +00003195 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003196 needsLoad = true;
3197 }
3198 break;
3199 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003200
Chris Lattner4302e8f2006-05-16 18:18:50 +00003201 // We need to load the argument to a virtual register if we determined above
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003202 // that we ran out of physical registers of the appropriate type.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003203 if (needsLoad) {
Chris Lattnerf6518cf2008-02-13 07:35:30 +00003204 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003205 CurArgOffset + (ArgSize - ObjSize),
Evan Cheng0664a672010-07-03 00:40:23 +00003206 isImmutable);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003207 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner7727d052010-09-21 06:44:06 +00003208 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003209 false, false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003210 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003211
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003212 InVals.push_back(ArgVal);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003213 }
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003214
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003215 // Allow for Altivec parameters at the end, if needed.
3216 if (nAltivecParamsAtEnd) {
3217 MinReservedArea = ((MinReservedArea+15)/16)*16;
3218 MinReservedArea += 16*nAltivecParamsAtEnd;
3219 }
3220
3221 // Area that is at least reserved in the caller of this function.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00003222 MinReservedArea = std::max(MinReservedArea, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003223
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003224 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt57d6de52012-10-23 15:51:16 +00003225 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003226 // taking the difference between two stack areas will result in an aligned
3227 // stack.
Ulrich Weigand2bffb952014-06-23 13:08:27 +00003228 MinReservedArea = EnsureStackAlignment(MF.getTarget(), MinReservedArea);
3229 FuncInfo->setMinReservedArea(MinReservedArea);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003230
Chris Lattner4302e8f2006-05-16 18:18:50 +00003231 // If the function takes variable number of arguments, make a frame index for
3232 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner4302e8f2006-05-16 18:18:50 +00003233 if (isVarArg) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003234 int Depth = ArgOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003235
Dan Gohman31ae5862010-04-17 14:41:14 +00003236 FuncInfo->setVarArgsFrameIndex(
3237 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Cheng0664a672010-07-03 00:40:23 +00003238 Depth, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00003239 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003240
Chris Lattner4302e8f2006-05-16 18:18:50 +00003241 // If this function is vararg, store any remaining integer argument regs
3242 // to their spots on the stack so that they may be loaded by deferencing the
3243 // result of va_next.
Chris Lattner26e2fcd2006-05-16 18:58:15 +00003244 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattner2cca3852006-11-18 01:57:19 +00003245 unsigned VReg;
Wesley Peck527da1b2010-11-23 03:31:01 +00003246
Chris Lattner2cca3852006-11-18 01:57:19 +00003247 if (isPPC64)
Devang Patelf3292b22011-02-21 23:21:26 +00003248 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003249 else
Devang Patelf3292b22011-02-21 23:21:26 +00003250 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattner2cca3852006-11-18 01:57:19 +00003251
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003252 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner676c61d2010-09-21 18:41:36 +00003253 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3254 MachinePointerInfo(), false, false, 0);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003255 MemOps.push_back(Store);
3256 // Increment the address by four for the next argument to store
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003257 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen679073b2009-02-04 02:34:38 +00003258 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner4302e8f2006-05-16 18:18:50 +00003259 }
Chris Lattner4302e8f2006-05-16 18:18:50 +00003260 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003261
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003262 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003263 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00003264
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003265 return Chain;
Chris Lattner4302e8f2006-05-16 18:18:50 +00003266}
3267
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003268/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003269/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesen86dcae12009-11-24 01:09:07 +00003270static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003271 unsigned ParamSize) {
3272
Dale Johannesen86dcae12009-11-24 01:09:07 +00003273 if (!isTailCall) return 0;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003274
3275 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
3276 unsigned CallerMinReservedArea = FI->getMinReservedArea();
3277 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
3278 // Remember only if the new adjustement is bigger.
3279 if (SPDiff < FI->getTailCallSPDelta())
3280 FI->setTailCallSPDelta(SPDiff);
3281
3282 return SPDiff;
3283}
3284
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003285/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3286/// for tail call optimization. Targets which want to do tail call
3287/// optimization should implement this function.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003288bool
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003289PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003290 CallingConv::ID CalleeCC,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003291 bool isVarArg,
3292 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003293 SelectionDAG& DAG) const {
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003294 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng25217ff2010-01-29 23:05:56 +00003295 return false;
3296
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003297 // Variable argument functions are not supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003298 if (isVarArg)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003299 return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003300
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003301 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel68c5f472009-09-02 08:44:58 +00003302 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003303 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
3304 // Functions containing by val parameters are not supported.
3305 for (unsigned i = 0; i != Ins.size(); i++) {
3306 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3307 if (Flags.isByVal()) return false;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003308 }
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003309
Alp Tokerf907b892013-12-05 05:44:44 +00003310 // Non-PIC/GOT tail calls are supported.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003311 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
3312 return true;
3313
3314 // At the moment we can only do local tail calls (in same module, hidden
3315 // or protected) if we are generating PIC.
3316 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
3317 return G->getGlobal()->hasHiddenVisibility()
3318 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003319 }
3320
3321 return false;
3322}
3323
Chris Lattnereb755fc2006-05-17 19:00:46 +00003324/// isCallCompatibleAddress - Return the immediate to use if the specified
3325/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003326static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnereb755fc2006-05-17 19:00:46 +00003327 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003328 if (!C) return nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003329
Dan Gohmaneffb8942008-09-12 16:56:44 +00003330 int Addr = C->getZExtValue();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003331 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith228e6d42012-08-24 23:29:28 +00003332 SignExtend32<26>(Addr) != Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +00003333 return nullptr; // Top 6 bits have to be sext of immediate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003334
Dan Gohmaneffb8942008-09-12 16:56:44 +00003335 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greiff304a7a2008-08-28 21:40:38 +00003336 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnereb755fc2006-05-17 19:00:46 +00003337}
3338
Dan Gohmand78c4002008-05-13 00:00:25 +00003339namespace {
3340
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003341struct TailCallArgumentInfo {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003342 SDValue Arg;
3343 SDValue FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003344 int FrameIdx;
3345
3346 TailCallArgumentInfo() : FrameIdx(0) {}
3347};
3348
Dan Gohmand78c4002008-05-13 00:00:25 +00003349}
3350
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003351/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3352static void
3353StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00003354 SDValue Chain,
Craig Topperb94011f2013-07-14 04:42:23 +00003355 const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs,
3356 SmallVectorImpl<SDValue> &MemOpChains,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003357 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003358 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003359 SDValue Arg = TailCallArgs[i].Arg;
3360 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003361 int FI = TailCallArgs[i].FrameIdx;
3362 // Store relative to framepointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00003363 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003364 MachinePointerInfo::getFixedStack(FI),
3365 false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003366 }
3367}
3368
3369/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3370/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003371static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003372 MachineFunction &MF,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003373 SDValue Chain,
3374 SDValue OldRetAddr,
3375 SDValue OldFP,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003376 int SPDiff,
3377 bool isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003378 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003379 SDLoc dl) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003380 if (SPDiff) {
3381 // Calculate the new stack slot for the return address.
3382 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003383 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003384 isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003385 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Cheng0664a672010-07-03 00:40:23 +00003386 NewRetAddrLoc, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003387 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003388 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen021052a2009-02-04 20:06:27 +00003389 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003390 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene87a5abe2010-02-15 16:56:53 +00003391 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003392
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003393 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3394 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003395 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003396 int NewFPLoc =
Anton Korobeynikov2f931282011-01-10 12:39:04 +00003397 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene1fbe0542009-11-12 20:49:22 +00003398 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Cheng0664a672010-07-03 00:40:23 +00003399 true);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003400 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3401 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattner7727d052010-09-21 06:44:06 +00003402 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene87a5abe2010-02-15 16:56:53 +00003403 false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003404 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003405 }
3406 return Chain;
3407}
3408
3409/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3410/// the position of the argument.
3411static void
3412CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003413 SDValue Arg, int SPDiff, unsigned ArgOffset,
Craig Topperb94011f2013-07-14 04:42:23 +00003414 SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003415 int Offset = ArgOffset + SPDiff;
Duncan Sands13237ac2008-06-06 12:08:01 +00003416 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Cheng0664a672010-07-03 00:40:23 +00003417 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson9f944592009-08-11 20:47:22 +00003418 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003419 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003420 TailCallArgumentInfo Info;
3421 Info.Arg = Arg;
3422 Info.FrameIdxOp = FIN;
3423 Info.FrameIdx = FI;
3424 TailCallArguments.push_back(Info);
3425}
3426
3427/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3428/// stack slot. Returns the chain as result and the loaded frame pointers in
3429/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003430SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen021052a2009-02-04 20:06:27 +00003431 int SPDiff,
3432 SDValue Chain,
3433 SDValue &LROpOut,
3434 SDValue &FPOpOut,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003435 bool isDarwinABI,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003436 SDLoc dl) const {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003437 if (SPDiff) {
3438 // Load the LR and FP stack slot for later adjusting.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003439 EVT VT = Subtarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003440 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003441 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003442 false, false, false, 0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003443 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peck527da1b2010-11-23 03:31:01 +00003444
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003445 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3446 // slot as the FP is never overwritten.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003447 if (isDarwinABI) {
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003448 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattner7727d052010-09-21 06:44:06 +00003449 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003450 false, false, false, 0);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003451 Chain = SDValue(FPOpOut.getNode(), 1);
3452 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003453 }
3454 return Chain;
3455}
3456
Dale Johannesen85d41a12008-03-04 23:17:14 +00003457/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelcf0da6c2009-02-17 22:15:04 +00003458/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen85d41a12008-03-04 23:17:14 +00003459/// specified by the specific parameter attribute. The copy will be passed as
3460/// a byval function parameter.
3461/// Sometimes what we are copying is the end of a larger object, the part that
3462/// does not fit in registers.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003463static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003464CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsd97eea32008-03-21 09:14:45 +00003465 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003466 SDLoc dl) {
Owen Anderson9f944592009-08-11 20:47:22 +00003467 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen85263882009-02-04 01:17:06 +00003468 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003469 false, false, MachinePointerInfo(),
3470 MachinePointerInfo());
Dale Johannesen85d41a12008-03-04 23:17:14 +00003471}
Chris Lattner43df5b32007-02-25 05:34:32 +00003472
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003473/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3474/// tail calls.
3475static void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003476LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3477 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003478 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003479 bool isVector, SmallVectorImpl<SDValue> &MemOpChains,
3480 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003481 SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003482 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003483 if (!isTailCall) {
3484 if (isVector) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003485 SDValue StackPtr;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003486 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00003487 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003488 else
Owen Anderson9f944592009-08-11 20:47:22 +00003489 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00003490 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003491 DAG.getConstant(ArgOffset, PtrVT));
3492 }
Chris Lattner676c61d2010-09-21 18:41:36 +00003493 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3494 MachinePointerInfo(), false, false, 0));
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00003495 // Calculate and remember argument location.
3496 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3497 TailCallArguments);
3498}
3499
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003500static
3501void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003502 SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003503 SDValue LROp, SDValue FPOp, bool isDarwinABI,
Craig Topperb94011f2013-07-14 04:42:23 +00003504 SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003505 MachineFunction &MF = DAG.getMachineFunction();
3506
3507 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3508 // might overwrite each other in case of tail call optimization.
3509 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003510 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003511 InFlag = SDValue();
3512 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3513 MemOpChains2, dl);
3514 if (!MemOpChains2.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003515 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003516
3517 // Store the return address to the appropriate stack slot.
3518 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3519 isPPC64, isDarwinABI, dl);
3520
3521 // Emit callseq_end just before tailcall node.
3522 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003523 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003524 InFlag = Chain.getValue(1);
3525}
3526
3527static
3528unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003529 SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall,
Craig Topperb94011f2013-07-14 04:42:23 +00003530 SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass,
3531 SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003532 const PPCSubtarget &Subtarget) {
Wesley Peck527da1b2010-11-23 03:31:01 +00003533
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003534 bool isPPC64 = Subtarget.isPPC64();
3535 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003536 bool isELFv2ABI = Subtarget.isELFv2ABI();
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003537
Owen Anderson53aa7a92009-08-10 22:56:29 +00003538 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00003539 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003540 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003541
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003542 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003543
Torok Edwin31e90d22010-08-04 20:47:44 +00003544 bool needIndirectCall = true;
Ulrich Weigand9aa09ef2014-06-18 16:14:04 +00003545 if (!isSVR4ABI || !isPPC64)
3546 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
3547 // If this is an absolute destination address, use the munged value.
3548 Callee = SDValue(Dest, 0);
3549 needIndirectCall = false;
3550 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003551
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003552 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3553 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3554 // Use indirect calls for ALL functions calls in JIT mode, since the
3555 // far-call stubs may be outside relocation limits for a BL instruction.
3556 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3557 unsigned OpFlags = 0;
Hal Finkel3ee2af72014-07-18 23:29:49 +00003558 if ((DAG.getTarget().getRelocationModel() != Reloc::Static &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003559 (Subtarget.getTargetTriple().isMacOSX() &&
3560 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003561 (G->getGlobal()->isDeclaration() ||
Hal Finkel3ee2af72014-07-18 23:29:49 +00003562 G->getGlobal()->isWeakForLinker())) ||
3563 (Subtarget.isTargetELF() && !isPPC64 &&
3564 !G->getGlobal()->hasLocalLinkage() &&
3565 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003566 // PC-relative references to external symbols should go through $stub,
3567 // unless we're building with the leopard linker or later, which
3568 // automatically synthesizes these stubs.
Hal Finkel3ee2af72014-07-18 23:29:49 +00003569 OpFlags = PPCII::MO_PLT_OR_STUB;
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003570 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003571
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003572 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3573 // every direct call is) turn it into a TargetGlobalAddress /
3574 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin31e90d22010-08-04 20:47:44 +00003575 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003576 Callee.getValueType(),
3577 0, OpFlags);
Torok Edwin31e90d22010-08-04 20:47:44 +00003578 needIndirectCall = false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003579 }
Torok Edwin31e90d22010-08-04 20:47:44 +00003580 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003581
Torok Edwin31e90d22010-08-04 20:47:44 +00003582 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003583 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003584
Hal Finkel3ee2af72014-07-18 23:29:49 +00003585 if ((DAG.getTarget().getRelocationModel() != Reloc::Static &&
3586 (Subtarget.getTargetTriple().isMacOSX() &&
3587 Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5))) ||
3588 (Subtarget.isTargetELF() && !isPPC64 &&
3589 DAG.getTarget().getRelocationModel() == Reloc::PIC_) ) {
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003590 // PC-relative references to external symbols should go through $stub,
3591 // unless we're building with the leopard linker or later, which
3592 // automatically synthesizes these stubs.
Hal Finkel3ee2af72014-07-18 23:29:49 +00003593 OpFlags = PPCII::MO_PLT_OR_STUB;
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003594 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003595
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003596 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3597 OpFlags);
3598 needIndirectCall = false;
Torok Edwin31e90d22010-08-04 20:47:44 +00003599 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003600
Torok Edwin31e90d22010-08-04 20:47:44 +00003601 if (needIndirectCall) {
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003602 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3603 // to do the call, we can't use PPCISD::CALL.
3604 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller79fef932009-12-18 13:00:15 +00003605
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003606 if (isSVR4ABI && isPPC64 && !isELFv2ABI) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003607 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3608 // entry point, but to the function descriptor (the function entry point
3609 // address is part of the function descriptor though).
3610 // The function descriptor is a three doubleword structure with the
3611 // following fields: function entry point, TOC base address and
3612 // environment pointer.
3613 // Thus for a call through a function pointer, the following actions need
3614 // to be performed:
3615 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt57d6de52012-10-23 15:51:16 +00003616 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller79fef932009-12-18 13:00:15 +00003617 // 2. Load the address of the function entry point from the function
3618 // descriptor.
3619 // 3. Load the TOC of the callee from the function descriptor into r2.
3620 // 4. Load the environment pointer from the function descriptor into
3621 // r11.
3622 // 5. Branch to the function entry point address.
3623 // 6. On return of the callee, the TOC of the caller needs to be
3624 // restored (this is done in FinishCall()).
3625 //
3626 // All those operations are flagged together to ensure that no other
3627 // operations can be scheduled in between. E.g. without flagging the
3628 // operations together, a TOC access in the caller could be scheduled
3629 // between the load of the callee TOC and the branch to the callee, which
3630 // results in the TOC access going through the TOC of the callee instead
3631 // of going through the TOC of the caller, which leads to incorrect code.
3632
3633 // Load the address of the function entry point from the function
3634 // descriptor.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003635 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00003636 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00003637 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
Tilmann Scheller79fef932009-12-18 13:00:15 +00003638 Chain = LoadFuncPtr.getValue(1);
3639 InFlag = LoadFuncPtr.getValue(2);
3640
3641 // Load environment pointer into r11.
3642 // Offset of the environment pointer within the function descriptor.
3643 SDValue PtrOff = DAG.getIntPtrConstant(16);
3644
3645 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3646 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3647 InFlag);
3648 Chain = LoadEnvPtr.getValue(1);
3649 InFlag = LoadEnvPtr.getValue(2);
3650
3651 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3652 InFlag);
3653 Chain = EnvVal.getValue(0);
3654 InFlag = EnvVal.getValue(1);
3655
3656 // Load TOC of the callee into r2. We are using a target-specific load
3657 // with r2 hard coded, because the result of a target-independent load
3658 // would never go directly into r2, since r2 is a reserved register (which
3659 // prevents the register allocator from allocating it), resulting in an
3660 // additional register being allocated and an unnecessary move instruction
3661 // being generated.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003662 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Ulrich Weigandad0cb912014-06-18 17:52:49 +00003663 SDValue TOCOff = DAG.getIntPtrConstant(8);
3664 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, TOCOff);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003665 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
Ulrich Weigandad0cb912014-06-18 17:52:49 +00003666 AddTOC, InFlag);
Tilmann Scheller79fef932009-12-18 13:00:15 +00003667 Chain = LoadTOCPtr.getValue(0);
3668 InFlag = LoadTOCPtr.getValue(1);
3669
3670 MTCTROps[0] = Chain;
3671 MTCTROps[1] = LoadFuncPtr;
3672 MTCTROps[2] = InFlag;
3673 }
3674
Craig Topper48d114b2014-04-26 18:35:24 +00003675 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00003676 makeArrayRef(MTCTROps, InFlag.getNode() ? 3 : 2));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003677 InFlag = Chain.getValue(1);
3678
3679 NodeTys.clear();
Owen Anderson9f944592009-08-11 20:47:22 +00003680 NodeTys.push_back(MVT::Other);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003681 NodeTys.push_back(MVT::Glue);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003682 Ops.push_back(Chain);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003683 CallOpc = PPCISD::BCTRL;
Craig Topper062a2ba2014-04-25 05:30:21 +00003684 Callee.setNode(nullptr);
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003685 // Add use of X11 (holding environment pointer)
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003686 if (isSVR4ABI && isPPC64 && !isELFv2ABI)
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003687 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003688 // Add CTR register as callee so a bctr can be emitted later.
3689 if (isTailCall)
Roman Divackya4a59ae2011-06-03 15:47:49 +00003690 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003691 }
3692
3693 // If this is a direct call, pass the chain and the callee.
3694 if (Callee.getNode()) {
3695 Ops.push_back(Chain);
3696 Ops.push_back(Callee);
3697 }
3698 // If this is a tail call add stack pointer delta.
3699 if (isTailCall)
Owen Anderson9f944592009-08-11 20:47:22 +00003700 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003701
3702 // Add argument registers to the end of the list so that they are known live
3703 // into the call.
3704 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3705 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3706 RegsToPass[i].second.getValueType()));
3707
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00003708 // Direct calls in the ELFv2 ABI need the TOC register live into the call.
3709 if (Callee.getNode() && isELFv2ABI)
3710 Ops.push_back(DAG.getRegister(PPC::X2, PtrVT));
3711
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003712 return CallOpc;
3713}
3714
Roman Divacky76293062012-09-18 16:47:58 +00003715static
3716bool isLocalCall(const SDValue &Callee)
3717{
3718 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky09adf3d2012-09-18 18:27:49 +00003719 return !G->getGlobal()->isDeclaration() &&
3720 !G->getGlobal()->isWeakForLinker();
Roman Divacky76293062012-09-18 16:47:58 +00003721 return false;
3722}
3723
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003724SDValue
3725PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003726 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003727 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003728 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003729 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003730
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003731 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003732 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003733 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003734 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003735
3736 // Copy all of the result registers out of their specified physreg.
3737 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3738 CCValAssign &VA = RVLocs[i];
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003739 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00003740
3741 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3742 VA.getLocReg(), VA.getLocVT(), InFlag);
3743 Chain = Val.getValue(1);
3744 InFlag = Val.getValue(2);
3745
3746 switch (VA.getLocInfo()) {
3747 default: llvm_unreachable("Unknown loc info!");
3748 case CCValAssign::Full: break;
3749 case CCValAssign::AExt:
3750 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3751 break;
3752 case CCValAssign::ZExt:
3753 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3754 DAG.getValueType(VA.getValVT()));
3755 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3756 break;
3757 case CCValAssign::SExt:
3758 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3759 DAG.getValueType(VA.getValVT()));
3760 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3761 break;
3762 }
3763
3764 InVals.push_back(Val);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003765 }
3766
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003767 return Chain;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003768}
3769
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003770SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00003771PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003772 bool isTailCall, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003773 SelectionDAG &DAG,
3774 SmallVector<std::pair<unsigned, SDValue>, 8>
3775 &RegsToPass,
3776 SDValue InFlag, SDValue Chain,
3777 SDValue &Callee,
3778 int SPDiff, unsigned NumBytes,
3779 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003780 SmallVectorImpl<SDValue> &InVals) const {
Ulrich Weigand8658f172014-07-20 23:43:15 +00003781
3782 bool isELFv2ABI = Subtarget.isELFv2ABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003783 std::vector<EVT> NodeTys;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003784 SmallVector<SDValue, 8> Ops;
3785 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3786 isTailCall, RegsToPass, Ops, NodeTys,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003787 Subtarget);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003788
Hal Finkel5ab37802012-08-28 02:10:27 +00003789 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003790 if (isVarArg && Subtarget.isSVR4ABI() && !Subtarget.isPPC64())
Hal Finkel5ab37802012-08-28 02:10:27 +00003791 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3792
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003793 // When performing tail call optimization the callee pops its arguments off
3794 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky8da87162013-02-21 20:05:00 +00003795 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003796 int BytesCalleePops =
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003797 (CallConv == CallingConv::Fast &&
3798 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003799
Roman Divackyef21be22012-03-06 16:41:49 +00003800 // Add a register mask operand representing the call-preserved registers.
3801 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3802 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3803 assert(Mask && "Missing call preserved mask for calling convention");
3804 Ops.push_back(DAG.getRegisterMask(Mask));
3805
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003806 if (InFlag.getNode())
3807 Ops.push_back(InFlag);
3808
3809 // Emit tail call.
3810 if (isTailCall) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003811 assert(((Callee.getOpcode() == ISD::Register &&
3812 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3813 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3814 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3815 isa<ConstantSDNode>(Callee)) &&
3816 "Expecting an global address, external symbol, absolute value or register");
3817
Craig Topper48d114b2014-04-26 18:35:24 +00003818 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, Ops);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003819 }
3820
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003821 // Add a NOP immediately after the branch instruction when using the 64-bit
3822 // SVR4 ABI. At link time, if caller and callee are in a different module and
3823 // thus have a different TOC, the call will be replaced with a call to a stub
3824 // function which saves the current TOC, loads the TOC of the callee and
3825 // branches to the callee. The NOP will be replaced with a load instruction
3826 // which restores the TOC of the caller from the TOC save slot of the current
3827 // stack frame. If caller and callee belong to the same module (and have the
3828 // same TOC), the NOP will remain unchanged.
Hal Finkel51861b42012-03-31 14:45:15 +00003829
3830 bool needsTOCRestore = false;
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003831 if (!isTailCall && Subtarget.isSVR4ABI()&& Subtarget.isPPC64()) {
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003832 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller79fef932009-12-18 13:00:15 +00003833 // This is a call through a function pointer.
3834 // Restore the caller TOC from the save area into R2.
3835 // See PrepareCall() for more information about calls through function
3836 // pointers in the 64-bit SVR4 ABI.
3837 // We are using a target-specific load with r2 hard coded, because the
3838 // result of a target-independent load would never go directly into r2,
3839 // since r2 is a reserved register (which prevents the register allocator
3840 // from allocating it), resulting in an additional register being
3841 // allocated and an unnecessary move instruction being generated.
Hal Finkel51861b42012-03-31 14:45:15 +00003842 needsTOCRestore = true;
Bill Schmidtcea15962013-09-26 17:09:28 +00003843 } else if ((CallOpc == PPCISD::CALL) &&
3844 (!isLocalCall(Callee) ||
3845 DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
Roman Divacky76293062012-09-18 16:47:58 +00003846 // Otherwise insert NOP for non-local calls.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00003847 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller79fef932009-12-18 13:00:15 +00003848 }
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003849 }
3850
Craig Topper48d114b2014-04-26 18:35:24 +00003851 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Hal Finkel51861b42012-03-31 14:45:15 +00003852 InFlag = Chain.getValue(1);
3853
3854 if (needsTOCRestore) {
3855 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Ulrich Weigandad0cb912014-06-18 17:52:49 +00003856 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3857 SDValue StackPtr = DAG.getRegister(PPC::X1, PtrVT);
Ulrich Weigand8658f172014-07-20 23:43:15 +00003858 unsigned TOCSaveOffset = PPCFrameLowering::getTOCSaveOffset(isELFv2ABI);
Ulrich Weigandad0cb912014-06-18 17:52:49 +00003859 SDValue TOCOff = DAG.getIntPtrConstant(TOCSaveOffset);
3860 SDValue AddTOC = DAG.getNode(ISD::ADD, dl, MVT::i64, StackPtr, TOCOff);
3861 Chain = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain, AddTOC, InFlag);
Hal Finkel51861b42012-03-31 14:45:15 +00003862 InFlag = Chain.getValue(1);
3863 }
3864
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003865 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3866 DAG.getIntPtrConstant(BytesCalleePops, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00003867 InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003868 if (!Ins.empty())
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003869 InFlag = Chain.getValue(1);
3870
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003871 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3872 Ins, dl, DAG, InVals);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00003873}
3874
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003875SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00003876PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003877 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00003878 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +00003879 SDLoc &dl = CLI.DL;
3880 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3881 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3882 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00003883 SDValue Chain = CLI.Chain;
3884 SDValue Callee = CLI.Callee;
3885 bool &isTailCall = CLI.IsTailCall;
3886 CallingConv::ID CallConv = CLI.CallConv;
3887 bool isVarArg = CLI.IsVarArg;
3888
Evan Cheng67a69dd2010-01-27 00:07:07 +00003889 if (isTailCall)
3890 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3891 Ins, DAG);
3892
Reid Kleckner5772b772014-04-24 20:14:34 +00003893 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
3894 report_fatal_error("failed to perform tail call elimination on a call "
3895 "site marked musttail");
3896
Eric Christopherb1aaebe2014-06-12 22:38:18 +00003897 if (Subtarget.isSVR4ABI()) {
3898 if (Subtarget.isPPC64())
Bill Schmidt57d6de52012-10-23 15:51:16 +00003899 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3900 isTailCall, Outs, OutVals, Ins,
3901 dl, DAG, InVals);
3902 else
3903 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3904 isTailCall, Outs, OutVals, Ins,
3905 dl, DAG, InVals);
3906 }
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00003907
Bill Schmidt57d6de52012-10-23 15:51:16 +00003908 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3909 isTailCall, Outs, OutVals, Ins,
3910 dl, DAG, InVals);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003911}
3912
3913SDValue
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003914PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3915 CallingConv::ID CallConv, bool isVarArg,
3916 bool isTailCall,
3917 const SmallVectorImpl<ISD::OutputArg> &Outs,
3918 const SmallVectorImpl<SDValue> &OutVals,
3919 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003920 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt019cc6f2012-09-19 15:42:13 +00003921 SmallVectorImpl<SDValue> &InVals) const {
3922 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Schellerd1aaa322009-08-15 11:54:46 +00003923 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003924
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003925 assert((CallConv == CallingConv::C ||
3926 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003927
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003928 unsigned PtrByteSize = 4;
3929
3930 MachineFunction &MF = DAG.getMachineFunction();
3931
3932 // Mark this function as potentially containing a function that contains a
3933 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3934 // and restoring the callers stack pointer in this functions epilog. This is
3935 // done because by tail calling the called function might overwrite the value
3936 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003937 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3938 CallConv == CallingConv::Fast)
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003939 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peck527da1b2010-11-23 03:31:01 +00003940
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003941 // Count how many bytes are to be pushed on the stack, including the linkage
3942 // area, parameter list area and the part of the local variable space which
3943 // contains copies of aggregates which are passed by value.
3944
3945 // Assign locations to all of the outgoing arguments.
3946 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003947 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003948 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003949
3950 // Reserve space for the linkage area on the stack.
Ulrich Weigand8658f172014-07-20 23:43:15 +00003951 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false, false),
3952 PtrByteSize);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003953
3954 if (isVarArg) {
3955 // Handle fixed and variable vector arguments differently.
3956 // Fixed vector arguments go into registers as long as registers are
3957 // available. Variable vector arguments always go into memory.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003958 unsigned NumArgs = Outs.size();
Wesley Peck527da1b2010-11-23 03:31:01 +00003959
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003960 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003961 MVT ArgVT = Outs[i].VT;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003962 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003963 bool Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00003964
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003965 if (Outs[i].IsFixed) {
Bill Schmidtef17c142013-02-06 17:33:58 +00003966 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3967 CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003968 } else {
Bill Schmidtef17c142013-02-06 17:33:58 +00003969 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3970 ArgFlags, CCInfo);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003971 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003972
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003973 if (Result) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003974#ifndef NDEBUG
Chris Lattner13626022009-08-23 06:03:38 +00003975 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sandsf5dda012010-11-03 11:35:31 +00003976 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00003977#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003978 llvm_unreachable(nullptr);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003979 }
3980 }
3981 } else {
3982 // All arguments are treated the same.
Bill Schmidtef17c142013-02-06 17:33:58 +00003983 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003984 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003985
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003986 // Assign locations to all of the outgoing aggregate by value arguments.
3987 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003988 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00003989 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003990
3991 // Reserve stack space for the allocations in CCInfo.
3992 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3993
Bill Schmidtef17c142013-02-06 17:33:58 +00003994 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00003995
3996 // Size of the linkage area, parameter list area and the part of the local
3997 // space variable where copies of aggregates which are passed by value are
3998 // stored.
3999 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004000
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004001 // Calculate by how many bytes the stack has to be adjusted in case of tail
4002 // call optimization.
4003 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4004
4005 // Adjust the stack pointer for the new arguments...
4006 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004007 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4008 dl);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004009 SDValue CallSeqStart = Chain;
4010
4011 // Load the return address and frame pointer so it can be moved somewhere else
4012 // later.
4013 SDValue LROp, FPOp;
4014 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
4015 dl);
4016
4017 // Set up a copy of the stack pointer for use loading and storing any
4018 // arguments that may not fit in the registers available for argument
4019 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00004020 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00004021
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004022 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4023 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4024 SmallVector<SDValue, 8> MemOpChains;
4025
Roman Divacky71038e72011-08-30 17:04:16 +00004026 bool seenFloatArg = false;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004027 // Walk the register/memloc assignments, inserting copies/loads.
4028 for (unsigned i = 0, j = 0, e = ArgLocs.size();
4029 i != e;
4030 ++i) {
4031 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004032 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004033 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peck527da1b2010-11-23 03:31:01 +00004034
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004035 if (Flags.isByVal()) {
4036 // Argument is an aggregate which is passed by value, thus we need to
4037 // create a copy of it in the local variable space of the current stack
4038 // frame (which is the stack frame of the caller) and pass the address of
4039 // this copy to the callee.
4040 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
4041 CCValAssign &ByValVA = ByValArgLocs[j++];
4042 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peck527da1b2010-11-23 03:31:01 +00004043
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004044 // Memory reserved in the local variable space of the callers stack frame.
4045 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peck527da1b2010-11-23 03:31:01 +00004046
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004047 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
4048 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peck527da1b2010-11-23 03:31:01 +00004049
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004050 // Create a copy of the argument in the local area of the current
4051 // stack frame.
4052 SDValue MemcpyCall =
4053 CreateCopyOfByValArgument(Arg, PtrOff,
4054 CallSeqStart.getNode()->getOperand(0),
4055 Flags, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00004056
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004057 // This must go outside the CALLSEQ_START..END.
4058 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004059 CallSeqStart.getNode()->getOperand(1),
4060 SDLoc(MemcpyCall));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004061 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4062 NewCallSeqStart.getNode());
4063 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peck527da1b2010-11-23 03:31:01 +00004064
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004065 // Pass the address of the aggregate copy on the stack either in a
4066 // physical register or in the parameter list area of the current stack
4067 // frame to the callee.
4068 Arg = PtrOff;
4069 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004070
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004071 if (VA.isRegLoc()) {
Hal Finkel2a9d3182014-03-06 00:23:33 +00004072 if (Arg.getValueType() == MVT::i1)
4073 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Arg);
4074
Roman Divacky71038e72011-08-30 17:04:16 +00004075 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004076 // Put argument in a physical register.
4077 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
4078 } else {
4079 // Put argument in the parameter list area of the current stack frame.
4080 assert(VA.isMemLoc());
4081 unsigned LocMemOffset = VA.getLocMemOffset();
4082
4083 if (!isTailCall) {
4084 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
4085 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
4086
4087 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner676c61d2010-09-21 18:41:36 +00004088 MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00004089 false, false, 0));
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004090 } else {
4091 // Calculate and remember argument location.
4092 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
4093 TailCallArguments);
4094 }
4095 }
4096 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004097
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004098 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004099 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Wesley Peck527da1b2010-11-23 03:31:01 +00004100
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004101 // Build a sequence of copy-to-reg nodes chained together with token chain
4102 // and flag operands which copy the outgoing args into the appropriate regs.
4103 SDValue InFlag;
4104 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4105 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4106 RegsToPass[i].second, InFlag);
4107 InFlag = Chain.getValue(1);
4108 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004109
Hal Finkel5ab37802012-08-28 02:10:27 +00004110 // Set CR bit 6 to true if this is a vararg call with floating args passed in
4111 // registers.
4112 if (isVarArg) {
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004113 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
4114 SDValue Ops[] = { Chain, InFlag };
4115
Hal Finkel5ab37802012-08-28 02:10:27 +00004116 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00004117 dl, VTs, makeArrayRef(Ops, InFlag.getNode() ? 2 : 1));
NAKAMURA Takumiac490292012-08-30 15:52:29 +00004118
Hal Finkel5ab37802012-08-28 02:10:27 +00004119 InFlag = Chain.getValue(1);
4120 }
4121
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00004122 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004123 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
4124 false, TailCallArguments);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004125
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004126 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4127 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4128 Ins, InVals);
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004129}
4130
Bill Schmidt57d6de52012-10-23 15:51:16 +00004131// Copy an argument into memory, being careful to do this outside the
4132// call sequence for the call to which the argument belongs.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004133SDValue
Bill Schmidt57d6de52012-10-23 15:51:16 +00004134PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
4135 SDValue CallSeqStart,
4136 ISD::ArgFlagsTy Flags,
4137 SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004138 SDLoc dl) const {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004139 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
4140 CallSeqStart.getNode()->getOperand(0),
4141 Flags, DAG, dl);
4142 // The MEMCPY must go outside the CALLSEQ_START..END.
4143 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Andrew Trickad6d08a2013-05-29 22:03:55 +00004144 CallSeqStart.getNode()->getOperand(1),
4145 SDLoc(MemcpyCall));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004146 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
4147 NewCallSeqStart.getNode());
4148 return NewCallSeqStart;
4149}
4150
4151SDValue
4152PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00004153 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004154 bool isTailCall,
4155 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004156 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004157 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004158 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00004159 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004160
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004161 bool isELFv2ABI = Subtarget.isELFv2ABI();
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004162 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004163 unsigned NumOps = Outs.size();
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004164
Bill Schmidt57d6de52012-10-23 15:51:16 +00004165 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
4166 unsigned PtrByteSize = 8;
4167
4168 MachineFunction &MF = DAG.getMachineFunction();
4169
4170 // Mark this function as potentially containing a function that contains a
4171 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4172 // and restoring the callers stack pointer in this functions epilog. This is
4173 // done because by tail calling the called function might overwrite the value
4174 // in this function's (MF) stack pointer stack slot 0(SP).
4175 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4176 CallConv == CallingConv::Fast)
4177 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4178
Bill Schmidt57d6de52012-10-23 15:51:16 +00004179 // Count how many bytes are to be pushed on the stack, including the linkage
Ulrich Weigand8658f172014-07-20 23:43:15 +00004180 // area, and parameter passing area. On ELFv1, the linkage area is 48 bytes
4181 // reserved space for [SP][CR][LR][2 x unused][TOC]; on ELFv2, the linkage
4182 // area is 32 bytes reserved space for [SP][CR][LR][TOC].
4183 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(true, false,
4184 isELFv2ABI);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004185 unsigned NumBytes = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004186
4187 // Add up all the space actually used.
4188 for (unsigned i = 0; i != NumOps; ++i) {
4189 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4190 EVT ArgVT = Outs[i].VT;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004191 EVT OrigVT = Outs[i].ArgVT;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004192
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004193 /* Respect alignment of argument on the stack. */
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004194 unsigned Align =
4195 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004196 NumBytes = ((NumBytes + Align - 1) / Align) * Align;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004197
4198 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004199 if (Flags.isInConsecutiveRegsLast())
4200 NumBytes = ((NumBytes + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004201 }
4202
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004203 unsigned NumBytesActuallyUsed = NumBytes;
4204
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004205 // The prolog code of the callee may store up to 8 GPR argument registers to
4206 // the stack, allowing va_start to index over them in memory if its varargs.
4207 // Because we cannot tell if this is needed on the caller side, we have to
4208 // conservatively assume that it is needed. As such, make sure we have at
4209 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00004210 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004211 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004212
4213 // Tail call needs the stack to be aligned.
4214 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4215 CallConv == CallingConv::Fast)
4216 NumBytes = EnsureStackAlignment(MF.getTarget(), NumBytes);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004217
4218 // Calculate by how many bytes the stack has to be adjusted in case of tail
4219 // call optimization.
4220 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
4221
4222 // To protect arguments on the stack from being clobbered in a tail call,
4223 // force all the loads to happen before doing any other lowering.
4224 if (isTailCall)
4225 Chain = DAG.getStackArgumentTokenFactor(Chain);
4226
4227 // Adjust the stack pointer for the new arguments...
4228 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004229 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4230 dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004231 SDValue CallSeqStart = Chain;
4232
4233 // Load the return address and frame pointer so it can be move somewhere else
4234 // later.
4235 SDValue LROp, FPOp;
4236 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4237 dl);
4238
4239 // Set up a copy of the stack pointer for use loading and storing any
4240 // arguments that may not fit in the registers available for argument
4241 // passing.
4242 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
4243
4244 // Figure out which arguments are going to go in registers, and which in
4245 // memory. Also, if this is a vararg function, floating point operations
4246 // must be stored to our stack, and loaded into integer regs as well, if
4247 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004248 unsigned ArgOffset = LinkageSize;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004249 unsigned GPR_idx, FPR_idx = 0, VR_idx = 0;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004250
Craig Topper840beec2014-04-04 05:16:06 +00004251 static const MCPhysReg GPR[] = {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004252 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4253 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4254 };
Craig Topper840beec2014-04-04 05:16:06 +00004255 static const MCPhysReg *FPR = GetFPR();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004256
Craig Topper840beec2014-04-04 05:16:06 +00004257 static const MCPhysReg VR[] = {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004258 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4259 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4260 };
Craig Topper840beec2014-04-04 05:16:06 +00004261 static const MCPhysReg VSRH[] = {
Hal Finkel7811c612014-03-28 19:58:11 +00004262 PPC::VSH2, PPC::VSH3, PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7, PPC::VSH8,
4263 PPC::VSH9, PPC::VSH10, PPC::VSH11, PPC::VSH12, PPC::VSH13
4264 };
4265
Bill Schmidt57d6de52012-10-23 15:51:16 +00004266 const unsigned NumGPRs = array_lengthof(GPR);
4267 const unsigned NumFPRs = 13;
4268 const unsigned NumVRs = array_lengthof(VR);
4269
4270 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
4271 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4272
4273 SmallVector<SDValue, 8> MemOpChains;
4274 for (unsigned i = 0; i != NumOps; ++i) {
4275 SDValue Arg = OutVals[i];
4276 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004277 EVT ArgVT = Outs[i].VT;
4278 EVT OrigVT = Outs[i].ArgVT;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004279
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004280 /* Respect alignment of argument on the stack. */
4281 unsigned Align =
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004282 CalculateStackSlotAlignment(ArgVT, OrigVT, Flags, PtrByteSize);
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004283 ArgOffset = ((ArgOffset + Align - 1) / Align) * Align;
4284
4285 /* Compute GPR index associated with argument offset. */
4286 GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
4287 GPR_idx = std::min(GPR_idx, NumGPRs);
4288
Bill Schmidt57d6de52012-10-23 15:51:16 +00004289 // PtrOff will be used to store the current argument to the stack if a
4290 // register cannot be found for it.
4291 SDValue PtrOff;
4292
4293 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
4294
4295 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4296
4297 // Promote integers to 64-bit values.
Hal Finkel940ab932014-02-28 00:27:01 +00004298 if (Arg.getValueType() == MVT::i32 || Arg.getValueType() == MVT::i1) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004299 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4300 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4301 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
4302 }
4303
4304 // FIXME memcpy is used way more than necessary. Correctness first.
4305 // Note: "by value" is code for passing a structure by value, not
4306 // basic types.
4307 if (Flags.isByVal()) {
4308 // Note: Size includes alignment padding, so
4309 // struct x { short a; char b; }
4310 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
4311 // These are the proper values we need for right-justifying the
4312 // aggregate in a parameter register.
4313 unsigned Size = Flags.getByValSize();
Bill Schmidt9953cf22012-10-31 01:15:05 +00004314
4315 // An empty aggregate parameter takes up no storage and no
4316 // registers.
4317 if (Size == 0)
4318 continue;
4319
Bill Schmidt57d6de52012-10-23 15:51:16 +00004320 // All aggregates smaller than 8 bytes must be passed right-justified.
4321 if (Size==1 || Size==2 || Size==4) {
4322 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
4323 if (GPR_idx != NumGPRs) {
4324 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
4325 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004326 false, false, false, 0);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004327 MemOpChains.push_back(Load.getValue(1));
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004328 RegsToPass.push_back(std::make_pair(GPR[GPR_idx], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004329
4330 ArgOffset += PtrByteSize;
4331 continue;
4332 }
4333 }
4334
4335 if (GPR_idx == NumGPRs && Size < 8) {
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004336 SDValue AddPtr = PtrOff;
4337 if (!isLittleEndian) {
4338 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4339 PtrOff.getValueType());
4340 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4341 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004342 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4343 CallSeqStart,
4344 Flags, DAG, dl);
4345 ArgOffset += PtrByteSize;
4346 continue;
4347 }
4348 // Copy entire object into memory. There are cases where gcc-generated
4349 // code assumes it is there, even if it could be put entirely into
4350 // registers. (This is not what the doc says.)
4351
4352 // FIXME: The above statement is likely due to a misunderstanding of the
4353 // documents. All arguments must be copied into the parameter area BY
4354 // THE CALLEE in the event that the callee takes the address of any
4355 // formal argument. That has not yet been implemented. However, it is
4356 // reasonable to use the stack area as a staging area for the register
4357 // load.
4358
4359 // Skip this for small aggregates, as we will use the same slot for a
4360 // right-justified copy, below.
4361 if (Size >= 8)
4362 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4363 CallSeqStart,
4364 Flags, DAG, dl);
4365
4366 // When a register is available, pass a small aggregate right-justified.
4367 if (Size < 8 && GPR_idx != NumGPRs) {
4368 // The easiest way to get this right-justified in a register
4369 // is to copy the structure into the rightmost portion of a
4370 // local variable slot, then load the whole slot into the
4371 // register.
4372 // FIXME: The memcpy seems to produce pretty awful code for
4373 // small aggregates, particularly for packed ones.
Matt Arsenault758659232013-05-18 00:21:46 +00004374 // FIXME: It would be preferable to use the slot in the
Bill Schmidt57d6de52012-10-23 15:51:16 +00004375 // parameter save area instead of a new local variable.
Ulrich Weigand59c6ab22014-06-20 16:34:05 +00004376 SDValue AddPtr = PtrOff;
4377 if (!isLittleEndian) {
4378 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
4379 AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
4380 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004381 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4382 CallSeqStart,
4383 Flags, DAG, dl);
4384
4385 // Load the slot into the register.
4386 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
4387 MachinePointerInfo(),
4388 false, false, false, 0);
4389 MemOpChains.push_back(Load.getValue(1));
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004390 RegsToPass.push_back(std::make_pair(GPR[GPR_idx], Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004391
4392 // Done with this argument.
4393 ArgOffset += PtrByteSize;
4394 continue;
4395 }
4396
4397 // For aggregates larger than PtrByteSize, copy the pieces of the
4398 // object that fit into registers from the parameter save area.
4399 for (unsigned j=0; j<Size; j+=PtrByteSize) {
4400 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
4401 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
4402 if (GPR_idx != NumGPRs) {
4403 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4404 MachinePointerInfo(),
4405 false, false, false, 0);
4406 MemOpChains.push_back(Load.getValue(1));
4407 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4408 ArgOffset += PtrByteSize;
4409 } else {
4410 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
4411 break;
4412 }
4413 }
4414 continue;
4415 }
4416
Craig Topper56710102013-08-15 02:33:50 +00004417 switch (Arg.getSimpleValueType().SimpleTy) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004418 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel940ab932014-02-28 00:27:01 +00004419 case MVT::i1:
Bill Schmidt57d6de52012-10-23 15:51:16 +00004420 case MVT::i32:
4421 case MVT::i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004422 // These can be scalar arguments or elements of an integer array type
4423 // passed directly. Clang may use those instead of "byval" aggregate
4424 // types to avoid forcing arguments to memory unnecessarily.
Bill Schmidt57d6de52012-10-23 15:51:16 +00004425 if (GPR_idx != NumGPRs) {
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004426 RegsToPass.push_back(std::make_pair(GPR[GPR_idx], Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004427 } else {
4428 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4429 true, isTailCall, false, MemOpChains,
4430 TailCallArguments, dl);
4431 }
4432 ArgOffset += PtrByteSize;
4433 break;
4434 case MVT::f32:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004435 case MVT::f64: {
4436 // These can be scalar arguments or elements of a float array type
4437 // passed directly. The latter are used to implement ELFv2 homogenous
4438 // float aggregates.
4439
4440 // Named arguments go into FPRs first, and once they overflow, the
4441 // remaining arguments go into GPRs and then the parameter save area.
4442 // Unnamed arguments for vararg functions always go to GPRs and
4443 // then the parameter save area. For now, put all arguments to vararg
4444 // routines always in both locations (FPR *and* GPR or stack slot).
4445 bool NeedGPROrStack = isVarArg || FPR_idx == NumFPRs;
4446
4447 // First load the argument into the next available FPR.
4448 if (FPR_idx != NumFPRs)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004449 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4450
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004451 // Next, load the argument into GPR or stack slot if needed.
4452 if (!NeedGPROrStack)
4453 ;
4454 else if (GPR_idx != NumGPRs) {
4455 // In the non-vararg case, this can only ever happen in the
4456 // presence of f32 array types, since otherwise we never run
4457 // out of FPRs before running out of GPRs.
4458 SDValue ArgVal;
Bill Schmidtbd4ac262012-10-29 21:18:16 +00004459
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004460 // Double values are always passed in a single GPR.
4461 if (Arg.getValueType() != MVT::f32) {
4462 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004463
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004464 // Non-array float values are extended and passed in a GPR.
4465 } else if (!Flags.isInConsecutiveRegs()) {
4466 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4467 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
4468
4469 // If we have an array of floats, we collect every odd element
4470 // together with its predecessor into one GPR.
4471 } else if (ArgOffset % PtrByteSize != 0) {
4472 SDValue Lo, Hi;
4473 Lo = DAG.getNode(ISD::BITCAST, dl, MVT::i32, OutVals[i - 1]);
4474 Hi = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4475 if (!isLittleEndian)
4476 std::swap(Lo, Hi);
4477 ArgVal = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
4478
4479 // The final element, if even, goes into the first half of a GPR.
4480 } else if (Flags.isInConsecutiveRegsLast()) {
4481 ArgVal = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
4482 ArgVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i64, ArgVal);
4483 if (!isLittleEndian)
4484 ArgVal = DAG.getNode(ISD::SHL, dl, MVT::i64, ArgVal,
4485 DAG.getConstant(32, MVT::i32));
4486
4487 // Non-final even elements are skipped; they will be handled
4488 // together the with subsequent argument on the next go-around.
4489 } else
4490 ArgVal = SDValue();
4491
4492 if (ArgVal.getNode())
4493 RegsToPass.push_back(std::make_pair(GPR[GPR_idx], ArgVal));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004494 } else {
4495 // Single-precision floating-point values are mapped to the
4496 // second (rightmost) word of the stack doubleword.
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004497 if (Arg.getValueType() == MVT::f32 &&
4498 !isLittleEndian && !Flags.isInConsecutiveRegs()) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004499 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4500 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4501 }
4502
4503 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4504 true, isTailCall, false, MemOpChains,
4505 TailCallArguments, dl);
4506 }
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004507 // When passing an array of floats, the array occupies consecutive
4508 // space in the argument area; only round up to the next doubleword
4509 // at the end of the array. Otherwise, each float takes 8 bytes.
4510 ArgOffset += (Arg.getValueType() == MVT::f32 &&
4511 Flags.isInConsecutiveRegs()) ? 4 : 8;
4512 if (Flags.isInConsecutiveRegsLast())
4513 ArgOffset = ((ArgOffset + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004514 break;
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004515 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004516 case MVT::v4f32:
4517 case MVT::v4i32:
4518 case MVT::v8i16:
4519 case MVT::v16i8:
Hal Finkel27774d92014-03-13 07:58:58 +00004520 case MVT::v2f64:
Hal Finkela6c8b512014-03-26 16:12:58 +00004521 case MVT::v2i64:
Ulrich Weigand85d5df22014-07-21 00:13:26 +00004522 // These can be scalar arguments or elements of a vector array type
4523 // passed directly. The latter are used to implement ELFv2 homogenous
4524 // vector aggregates.
4525
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00004526 // For a varargs call, named arguments go into VRs or on the stack as
4527 // usual; unnamed arguments always go to the stack or the corresponding
4528 // GPRs when within range. For now, we always put the value in both
4529 // locations (or even all three).
Bill Schmidt57d6de52012-10-23 15:51:16 +00004530 if (isVarArg) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004531 // We could elide this store in the case where the object fits
4532 // entirely in R registers. Maybe later.
Bill Schmidt57d6de52012-10-23 15:51:16 +00004533 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4534 MachinePointerInfo(), false, false, 0);
4535 MemOpChains.push_back(Store);
4536 if (VR_idx != NumVRs) {
4537 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4538 MachinePointerInfo(),
4539 false, false, false, 0);
4540 MemOpChains.push_back(Load.getValue(1));
Hal Finkel7811c612014-03-28 19:58:11 +00004541
4542 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4543 Arg.getSimpleValueType() == MVT::v2i64) ?
4544 VSRH[VR_idx] : VR[VR_idx];
4545 ++VR_idx;
4546
4547 RegsToPass.push_back(std::make_pair(VReg, Load));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004548 }
4549 ArgOffset += 16;
4550 for (unsigned i=0; i<16; i+=PtrByteSize) {
4551 if (GPR_idx == NumGPRs)
4552 break;
4553 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4554 DAG.getConstant(i, PtrVT));
4555 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4556 false, false, false, 0);
4557 MemOpChains.push_back(Load.getValue(1));
4558 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4559 }
4560 break;
4561 }
4562
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00004563 // Non-varargs Altivec params go into VRs or on the stack.
Bill Schmidt57d6de52012-10-23 15:51:16 +00004564 if (VR_idx != NumVRs) {
Hal Finkel7811c612014-03-28 19:58:11 +00004565 unsigned VReg = (Arg.getSimpleValueType() == MVT::v2f64 ||
4566 Arg.getSimpleValueType() == MVT::v2i64) ?
4567 VSRH[VR_idx] : VR[VR_idx];
4568 ++VR_idx;
4569
4570 RegsToPass.push_back(std::make_pair(VReg, Arg));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004571 } else {
4572 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4573 true, isTailCall, true, MemOpChains,
4574 TailCallArguments, dl);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004575 }
Ulrich Weigand9ba552d2014-06-23 12:36:34 +00004576 ArgOffset += 16;
Bill Schmidt57d6de52012-10-23 15:51:16 +00004577 break;
4578 }
4579 }
4580
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004581 assert(NumBytesActuallyUsed == ArgOffset);
Ulrich Weigandde8641b2014-07-07 19:39:44 +00004582 (void)NumBytesActuallyUsed;
Ulrich Weigandec2bf932014-07-07 19:26:41 +00004583
Bill Schmidt57d6de52012-10-23 15:51:16 +00004584 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004585 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004586
4587 // Check if this is an indirect call (MTCTR/BCTRL).
4588 // See PrepareCall() for more information about calls through function
4589 // pointers in the 64-bit SVR4 ABI.
4590 if (!isTailCall &&
4591 !dyn_cast<GlobalAddressSDNode>(Callee) &&
Ulrich Weigand9aa09ef2014-06-18 16:14:04 +00004592 !dyn_cast<ExternalSymbolSDNode>(Callee)) {
Bill Schmidt57d6de52012-10-23 15:51:16 +00004593 // Load r2 into a virtual register and store it to the TOC save area.
4594 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4595 // TOC save area offset.
Ulrich Weigand8658f172014-07-20 23:43:15 +00004596 unsigned TOCSaveOffset = PPCFrameLowering::getTOCSaveOffset(isELFv2ABI);
Ulrich Weigandad0cb912014-06-18 17:52:49 +00004597 SDValue PtrOff = DAG.getIntPtrConstant(TOCSaveOffset);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004598 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4599 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4600 false, false, 0);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00004601 // In the ELFv2 ABI, R12 must contain the address of an indirect callee.
4602 // This does not mean the MTCTR instruction must use R12; it's easier
4603 // to model this as an extra parameter, so do that.
4604 if (isELFv2ABI)
4605 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
Bill Schmidt57d6de52012-10-23 15:51:16 +00004606 }
4607
4608 // Build a sequence of copy-to-reg nodes chained together with token chain
4609 // and flag operands which copy the outgoing args into the appropriate regs.
4610 SDValue InFlag;
4611 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4612 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4613 RegsToPass[i].second, InFlag);
4614 InFlag = Chain.getValue(1);
4615 }
4616
4617 if (isTailCall)
4618 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4619 FPOp, true, TailCallArguments);
4620
4621 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4622 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4623 Ins, InVals);
4624}
4625
4626SDValue
4627PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4628 CallingConv::ID CallConv, bool isVarArg,
4629 bool isTailCall,
4630 const SmallVectorImpl<ISD::OutputArg> &Outs,
4631 const SmallVectorImpl<SDValue> &OutVals,
4632 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004633 SDLoc dl, SelectionDAG &DAG,
Bill Schmidt57d6de52012-10-23 15:51:16 +00004634 SmallVectorImpl<SDValue> &InVals) const {
4635
4636 unsigned NumOps = Outs.size();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004637
Owen Anderson53aa7a92009-08-10 22:56:29 +00004638 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00004639 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004640 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004641
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004642 MachineFunction &MF = DAG.getMachineFunction();
4643
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004644 // Mark this function as potentially containing a function that contains a
4645 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4646 // and restoring the callers stack pointer in this functions epilog. This is
4647 // done because by tail calling the called function might overwrite the value
4648 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004649 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4650 CallConv == CallingConv::Fast)
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004651 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4652
Chris Lattneraa40ec12006-05-16 22:56:08 +00004653 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerec78cad2006-06-26 22:48:35 +00004654 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerb7552a82006-05-17 00:15:40 +00004655 // prereserved space for [SP][CR][LR][3 x unused].
Ulrich Weigand8658f172014-07-20 23:43:15 +00004656 unsigned LinkageSize = PPCFrameLowering::getLinkageSize(isPPC64, true,
4657 false);
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004658 unsigned NumBytes = LinkageSize;
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004659
4660 // Add up all the space actually used.
4661 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
4662 // they all go in registers, but we must reserve stack space for them for
4663 // possible use by the caller. In varargs or 64-bit calls, parameters are
4664 // assigned stack space in order, with padding so Altivec parameters are
4665 // 16-byte aligned.
4666 unsigned nAltivecParamsAtEnd = 0;
4667 for (unsigned i = 0; i != NumOps; ++i) {
4668 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4669 EVT ArgVT = Outs[i].VT;
4670 // Varargs Altivec parameters are padded to a 16 byte boundary.
4671 if (ArgVT == MVT::v4f32 || ArgVT == MVT::v4i32 ||
4672 ArgVT == MVT::v8i16 || ArgVT == MVT::v16i8 ||
4673 ArgVT == MVT::v2f64 || ArgVT == MVT::v2i64) {
4674 if (!isVarArg && !isPPC64) {
4675 // Non-varargs Altivec parameters go after all the non-Altivec
4676 // parameters; handle those later so we know how much padding we need.
4677 nAltivecParamsAtEnd++;
4678 continue;
4679 }
4680 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
4681 NumBytes = ((NumBytes+15)/16)*16;
4682 }
4683 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
4684 }
4685
4686 // Allow for Altivec parameters at the end, if needed.
4687 if (nAltivecParamsAtEnd) {
4688 NumBytes = ((NumBytes+15)/16)*16;
4689 NumBytes += 16*nAltivecParamsAtEnd;
4690 }
4691
4692 // The prolog code of the callee may store up to 8 GPR argument registers to
4693 // the stack, allowing va_start to index over them in memory if its varargs.
4694 // Because we cannot tell if this is needed on the caller side, we have to
4695 // conservatively assume that it is needed. As such, make sure we have at
4696 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004697 NumBytes = std::max(NumBytes, LinkageSize + 8 * PtrByteSize);
Ulrich Weigand2bffb952014-06-23 13:08:27 +00004698
4699 // Tail call needs the stack to be aligned.
4700 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4701 CallConv == CallingConv::Fast)
4702 NumBytes = EnsureStackAlignment(MF.getTarget(), NumBytes);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004703
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004704 // Calculate by how many bytes the stack has to be adjusted in case of tail
4705 // call optimization.
4706 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004707
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004708 // To protect arguments on the stack from being clobbered in a tail call,
4709 // force all the loads to happen before doing any other lowering.
4710 if (isTailCall)
4711 Chain = DAG.getStackArgumentTokenFactor(Chain);
4712
Chris Lattnerb7552a82006-05-17 00:15:40 +00004713 // Adjust the stack pointer for the new arguments...
4714 // These operations are automatically eliminated by the prolog/epilog pass
Andrew Trickad6d08a2013-05-29 22:03:55 +00004715 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
4716 dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004717 SDValue CallSeqStart = Chain;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004718
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004719 // Load the return address and frame pointer so it can be move somewhere else
4720 // later.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004721 SDValue LROp, FPOp;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00004722 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4723 dl);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004724
Chris Lattnerb7552a82006-05-17 00:15:40 +00004725 // Set up a copy of the stack pointer for use loading and storing any
4726 // arguments that may not fit in the registers available for argument
4727 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004728 SDValue StackPtr;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004729 if (isPPC64)
Owen Anderson9f944592009-08-11 20:47:22 +00004730 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004731 else
Owen Anderson9f944592009-08-11 20:47:22 +00004732 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004733
Chris Lattnerb7552a82006-05-17 00:15:40 +00004734 // Figure out which arguments are going to go in registers, and which in
4735 // memory. Also, if this is a vararg function, floating point operations
4736 // must be stored to our stack, and loaded into integer regs as well, if
4737 // any integer regs are available for argument passing.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00004738 unsigned ArgOffset = LinkageSize;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004739 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004740
Craig Topper840beec2014-04-04 05:16:06 +00004741 static const MCPhysReg GPR_32[] = { // 32-bit registers.
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004742 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4743 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4744 };
Craig Topper840beec2014-04-04 05:16:06 +00004745 static const MCPhysReg GPR_64[] = { // 64-bit registers.
Chris Lattnerec78cad2006-06-26 22:48:35 +00004746 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4747 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4748 };
Craig Topper840beec2014-04-04 05:16:06 +00004749 static const MCPhysReg *FPR = GetFPR();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004750
Craig Topper840beec2014-04-04 05:16:06 +00004751 static const MCPhysReg VR[] = {
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004752 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4753 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4754 };
Owen Andersone2f23a32007-09-07 04:06:50 +00004755 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004756 const unsigned NumFPRs = 13;
Tilmann Scheller98bdaaa2009-07-03 06:43:35 +00004757 const unsigned NumVRs = array_lengthof(VR);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004758
Craig Topper840beec2014-04-04 05:16:06 +00004759 const MCPhysReg *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerec78cad2006-06-26 22:48:35 +00004760
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004761 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004762 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4763
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004764 SmallVector<SDValue, 8> MemOpChains;
Evan Chengc2cd4732006-05-25 00:57:32 +00004765 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004766 SDValue Arg = OutVals[i];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00004767 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004768
Chris Lattnerb7552a82006-05-17 00:15:40 +00004769 // PtrOff will be used to store the current argument to the stack if a
4770 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004771 SDValue PtrOff;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004772
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004773 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffray7aad9282007-03-13 15:02:46 +00004774
Dale Johannesen679073b2009-02-04 02:34:38 +00004775 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004776
4777 // On PPC64, promote integers to 64-bit values.
Owen Anderson9f944592009-08-11 20:47:22 +00004778 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sandsd97eea32008-03-21 09:14:45 +00004779 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4780 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson9f944592009-08-11 20:47:22 +00004781 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerec78cad2006-06-26 22:48:35 +00004782 }
Dale Johannesen85d41a12008-03-04 23:17:14 +00004783
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004784 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004785 // Note: "by value" is code for passing a structure by value, not
4786 // basic types.
Duncan Sandsd97eea32008-03-21 09:14:45 +00004787 if (Flags.isByVal()) {
4788 unsigned Size = Flags.getByValSize();
Bill Schmidt57d6de52012-10-23 15:51:16 +00004789 // Very small objects are passed right-justified. Everything else is
4790 // passed left-justified.
4791 if (Size==1 || Size==2) {
4792 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004793 if (GPR_idx != NumGPRs) {
Stuart Hastings81c43062011-02-16 16:23:55 +00004794 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d178ed2010-09-21 17:04:51 +00004795 MachinePointerInfo(), VT,
Louis Gerbarg67474e32014-07-31 21:45:05 +00004796 false, false, false, 0);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004797 MemOpChains.push_back(Load.getValue(1));
4798 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004799
4800 ArgOffset += PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004801 } else {
Bill Schmidt48081ca2012-10-16 13:30:53 +00004802 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4803 PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004804 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt57d6de52012-10-23 15:51:16 +00004805 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4806 CallSeqStart,
4807 Flags, DAG, dl);
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004808 ArgOffset += PtrByteSize;
4809 }
4810 continue;
4811 }
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004812 // Copy entire object into memory. There are cases where gcc-generated
4813 // code assumes it is there, even if it could be put entirely into
4814 // registers. (This is not what the doc says.)
Bill Schmidt57d6de52012-10-23 15:51:16 +00004815 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4816 CallSeqStart,
4817 Flags, DAG, dl);
Bill Schmidt019cc6f2012-09-19 15:42:13 +00004818
4819 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4820 // copy the pieces of the object that fit into registers from the
4821 // parameter save area.
Dale Johannesen85d41a12008-03-04 23:17:14 +00004822 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004823 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004824 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen85d41a12008-03-04 23:17:14 +00004825 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004826 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4827 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004828 false, false, false, 0);
Dale Johannesen0d235052008-03-05 23:31:27 +00004829 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen85d41a12008-03-04 23:17:14 +00004830 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004831 ArgOffset += PtrByteSize;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004832 } else {
Dale Johannesen92dcf1e2008-03-17 02:13:43 +00004833 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesenbfa252d2008-03-07 20:27:40 +00004834 break;
Dale Johannesen85d41a12008-03-04 23:17:14 +00004835 }
4836 }
4837 continue;
4838 }
4839
Craig Topper56710102013-08-15 02:33:50 +00004840 switch (Arg.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004841 default: llvm_unreachable("Unexpected ValueType for argument!");
Hal Finkel5cae2162014-02-28 01:17:25 +00004842 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +00004843 case MVT::i32:
4844 case MVT::i64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004845 if (GPR_idx != NumGPRs) {
Hal Finkel7f908e82014-03-06 00:45:19 +00004846 if (Arg.getValueType() == MVT::i1)
4847 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, PtrVT, Arg);
4848
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004849 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004850 } else {
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004851 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4852 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004853 TailCallArguments, dl);
Chris Lattnerb7552a82006-05-17 00:15:40 +00004854 }
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004855 ArgOffset += PtrByteSize;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004856 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004857 case MVT::f32:
4858 case MVT::f64:
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004859 if (FPR_idx != NumFPRs) {
4860 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4861
Chris Lattnerb7552a82006-05-17 00:15:40 +00004862 if (isVarArg) {
Chris Lattner676c61d2010-09-21 18:41:36 +00004863 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4864 MachinePointerInfo(), false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004865 MemOpChains.push_back(Store);
4866
Chris Lattnerb7552a82006-05-17 00:15:40 +00004867 // Float varargs are always shadowed in available integer registers
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004868 if (GPR_idx != NumGPRs) {
Chris Lattner7727d052010-09-21 06:44:06 +00004869 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004870 MachinePointerInfo(), false, false,
4871 false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004872 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004873 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerb7552a82006-05-17 00:15:40 +00004874 }
Owen Anderson9f944592009-08-11 20:47:22 +00004875 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004876 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen679073b2009-02-04 02:34:38 +00004877 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattner7727d052010-09-21 06:44:06 +00004878 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4879 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004880 false, false, false, 0);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004881 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004882 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattneraa40ec12006-05-16 22:56:08 +00004883 }
4884 } else {
Chris Lattnerb7552a82006-05-17 00:15:40 +00004885 // If we have any FPRs remaining, we may also have GPRs remaining.
4886 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4887 // GPRs.
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004888 if (GPR_idx != NumGPRs)
4889 ++GPR_idx;
Owen Anderson9f944592009-08-11 20:47:22 +00004890 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004891 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4892 ++GPR_idx;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004893 }
Bill Schmidt57d6de52012-10-23 15:51:16 +00004894 } else
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004895 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4896 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004897 TailCallArguments, dl);
Tilmann Scheller773f14c2009-07-03 06:47:08 +00004898 if (isPPC64)
4899 ArgOffset += 8;
4900 else
Owen Anderson9f944592009-08-11 20:47:22 +00004901 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerb7552a82006-05-17 00:15:40 +00004902 break;
Owen Anderson9f944592009-08-11 20:47:22 +00004903 case MVT::v4f32:
4904 case MVT::v4i32:
4905 case MVT::v8i16:
4906 case MVT::v16i8:
Dale Johannesenb28456e2008-03-12 00:22:17 +00004907 if (isVarArg) {
4908 // These go aligned on the stack, or in the corresponding R registers
Scott Michelcf0da6c2009-02-17 22:15:04 +00004909 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesenb28456e2008-03-12 00:22:17 +00004910 // V registers; in fact gcc does this only for arguments that are
4911 // prototyped, not for those that match the ... We do it for all
4912 // arguments, seems to work.
4913 while (ArgOffset % 16 !=0) {
4914 ArgOffset += PtrByteSize;
4915 if (GPR_idx != NumGPRs)
4916 GPR_idx++;
4917 }
4918 // We could elide this store in the case where the object fits
4919 // entirely in R registers. Maybe later.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004920 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004921 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner676c61d2010-09-21 18:41:36 +00004922 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4923 MachinePointerInfo(), false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004924 MemOpChains.push_back(Store);
4925 if (VR_idx != NumVRs) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004926 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattner7727d052010-09-21 06:44:06 +00004927 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004928 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004929 MemOpChains.push_back(Load.getValue(1));
4930 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4931 }
4932 ArgOffset += 16;
4933 for (unsigned i=0; i<16; i+=PtrByteSize) {
4934 if (GPR_idx == NumGPRs)
4935 break;
Dale Johannesen679073b2009-02-04 02:34:38 +00004936 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesenb28456e2008-03-12 00:22:17 +00004937 DAG.getConstant(i, PtrVT));
Chris Lattner7727d052010-09-21 06:44:06 +00004938 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004939 false, false, false, 0);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004940 MemOpChains.push_back(Load.getValue(1));
4941 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4942 }
4943 break;
4944 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004945
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004946 // Non-varargs Altivec params generally go in registers, but have
4947 // stack space allocated at the end.
4948 if (VR_idx != NumVRs) {
4949 // Doesn't have GPR space allocated.
4950 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4951 } else if (nAltivecParamsAtEnd==0) {
4952 // We are emitting Altivec params in order.
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004953 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4954 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004955 TailCallArguments, dl);
Dale Johannesenb28456e2008-03-12 00:22:17 +00004956 ArgOffset += 16;
Dale Johannesenb28456e2008-03-12 00:22:17 +00004957 }
Chris Lattnerb7552a82006-05-17 00:15:40 +00004958 break;
Chris Lattneraa40ec12006-05-16 22:56:08 +00004959 }
Chris Lattneraa40ec12006-05-16 22:56:08 +00004960 }
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004961 // If all Altivec parameters fit in registers, as they usually do,
4962 // they get stack space following the non-Altivec parameters. We
4963 // don't track this here because nobody below needs it.
4964 // If there are more Altivec parameters than fit in registers emit
4965 // the stores here.
4966 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4967 unsigned j = 0;
4968 // Offset is aligned; skip 1st 12 params which go in V registers.
4969 ArgOffset = ((ArgOffset+15)/16)*16;
4970 ArgOffset += 12*16;
4971 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00004972 SDValue Arg = OutVals[i];
4973 EVT ArgType = Outs[i].VT;
Owen Anderson9f944592009-08-11 20:47:22 +00004974 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4975 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004976 if (++j > NumVRs) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004977 SDValue PtrOff;
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00004978 // We are emitting Altivec params in order.
4979 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4980 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen021052a2009-02-04 20:06:27 +00004981 TailCallArguments, dl);
Dale Johannesen0dfd3f32008-03-14 17:41:26 +00004982 ArgOffset += 16;
4983 }
4984 }
4985 }
4986 }
4987
Chris Lattnerb1e9e372006-05-17 06:01:33 +00004988 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00004989 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004990
Dale Johannesen90eab672010-03-09 20:15:42 +00004991 // On Darwin, R12 must contain the address of an indirect callee. This does
4992 // not mean the MTCTR instruction must use R12; it's easier to model this as
4993 // an extra parameter, so do that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004994 if (!isTailCall &&
Dale Johannesen90eab672010-03-09 20:15:42 +00004995 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4996 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4997 !isBLACompatibleAddress(Callee, DAG))
4998 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4999 PPC::R12), Callee));
5000
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005001 // Build a sequence of copy-to-reg nodes chained together with token chain
5002 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005003 SDValue InFlag;
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005004 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00005005 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen679073b2009-02-04 02:34:38 +00005006 RegsToPass[i].second, InFlag);
Chris Lattnerb1e9e372006-05-17 06:01:33 +00005007 InFlag = Chain.getValue(1);
5008 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005009
Chris Lattnerdf8e17d2010-11-14 23:42:06 +00005010 if (isTailCall)
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005011 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
5012 FPOp, true, TailCallArguments);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005013
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005014 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
5015 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
5016 Ins, InVals);
Chris Lattneraa40ec12006-05-16 22:56:08 +00005017}
5018
Hal Finkel450128a2011-10-14 19:51:36 +00005019bool
5020PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
5021 MachineFunction &MF, bool isVarArg,
5022 const SmallVectorImpl<ISD::OutputArg> &Outs,
5023 LLVMContext &Context) const {
5024 SmallVector<CCValAssign, 16> RVLocs;
5025 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
5026 RVLocs, Context);
5027 return CCInfo.CheckReturn(Outs, RetCC_PPC);
5028}
5029
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005030SDValue
5031PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00005032 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005033 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00005034 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005035 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005036
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005037 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00005038 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greif180c4442012-04-19 15:16:31 +00005039 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00005040 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005041
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005042 SDValue Flag;
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005043 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005044
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005045 // Copy the result values into the output registers.
5046 for (unsigned i = 0; i != RVLocs.size(); ++i) {
5047 CCValAssign &VA = RVLocs[i];
5048 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand339d0592012-11-05 19:39:45 +00005049
5050 SDValue Arg = OutVals[i];
5051
5052 switch (VA.getLocInfo()) {
5053 default: llvm_unreachable("Unknown loc info!");
5054 case CCValAssign::Full: break;
5055 case CCValAssign::AExt:
5056 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
5057 break;
5058 case CCValAssign::ZExt:
5059 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
5060 break;
5061 case CCValAssign::SExt:
5062 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
5063 break;
5064 }
5065
5066 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005067 Flag = Chain.getValue(1);
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005068 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner4f2e4e02007-03-06 00:59:59 +00005069 }
5070
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005071 RetOps[0] = Chain; // Update chain.
5072
5073 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005074 if (Flag.getNode())
Jakob Stoklund Olesen8660a8c2013-02-05 18:12:00 +00005075 RetOps.push_back(Flag);
5076
Craig Topper48d114b2014-04-26 18:35:24 +00005077 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, RetOps);
Chris Lattner4211ca92006-04-14 06:01:58 +00005078}
5079
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005080SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005081 const PPCSubtarget &Subtarget) const {
Jim Laskeye4f4d042006-12-04 22:04:42 +00005082 // When we pop the dynamic allocation we need to restore the SP link.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005083 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005084
Jim Laskeye4f4d042006-12-04 22:04:42 +00005085 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005086 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeye4f4d042006-12-04 22:04:42 +00005087
5088 // Construct the stack pointer operand.
Dale Johannesen86dcae12009-11-24 01:09:07 +00005089 bool isPPC64 = Subtarget.isPPC64();
5090 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005091 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeye4f4d042006-12-04 22:04:42 +00005092
5093 // Get the operands for the STACKRESTORE.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005094 SDValue Chain = Op.getOperand(0);
5095 SDValue SaveSP = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005096
Jim Laskeye4f4d042006-12-04 22:04:42 +00005097 // Load the old link SP.
Chris Lattner7727d052010-09-21 06:44:06 +00005098 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
5099 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005100 false, false, false, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005101
Jim Laskeye4f4d042006-12-04 22:04:42 +00005102 // Restore the stack pointer.
Dale Johannesen021052a2009-02-04 20:06:27 +00005103 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005104
Jim Laskeye4f4d042006-12-04 22:04:42 +00005105 // Store the old link SP.
Chris Lattner676c61d2010-09-21 18:41:36 +00005106 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00005107 false, false, 0);
Jim Laskeye4f4d042006-12-04 22:04:42 +00005108}
5109
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005110
5111
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005112SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005113PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey48850c12006-11-16 22:43:37 +00005114 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005115 bool isPPC64 = Subtarget.isPPC64();
5116 bool isDarwinABI = Subtarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005117 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005118
5119 // Get current frame pointer save index. The users of this index will be
5120 // primarily DYNALLOC instructions.
5121 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
5122 int RASI = FI->getReturnAddrSaveIndex();
5123
5124 // If the frame pointer save index hasn't been defined yet.
5125 if (!RASI) {
5126 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00005127 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005128 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00005129 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005130 // Save the result.
5131 FI->setReturnAddrSaveIndex(RASI);
5132 }
5133 return DAG.getFrameIndex(RASI, PtrVT);
5134}
5135
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005136SDValue
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005137PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
5138 MachineFunction &MF = DAG.getMachineFunction();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005139 bool isPPC64 = Subtarget.isPPC64();
5140 bool isDarwinABI = Subtarget.isDarwinABI();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005141 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00005142
5143 // Get current frame pointer save index. The users of this index will be
5144 // primarily DYNALLOC instructions.
5145 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
5146 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005147
Jim Laskey48850c12006-11-16 22:43:37 +00005148 // If the frame pointer save index hasn't been defined yet.
5149 if (!FPSI) {
5150 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00005151 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller773f14c2009-07-03 06:47:08 +00005152 isDarwinABI);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005153
Jim Laskey48850c12006-11-16 22:43:37 +00005154 // Allocate the frame index for frame pointer save area.
Evan Cheng0664a672010-07-03 00:40:23 +00005155 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey48850c12006-11-16 22:43:37 +00005156 // Save the result.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005157 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey48850c12006-11-16 22:43:37 +00005158 }
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005159 return DAG.getFrameIndex(FPSI, PtrVT);
5160}
Jim Laskey48850c12006-11-16 22:43:37 +00005161
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005162SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighoferbe0de342008-04-30 09:16:33 +00005163 SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005164 const PPCSubtarget &Subtarget) const {
Jim Laskey48850c12006-11-16 22:43:37 +00005165 // Get the inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005166 SDValue Chain = Op.getOperand(0);
5167 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005168 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005169
Jim Laskey48850c12006-11-16 22:43:37 +00005170 // Get the corect type for pointers.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005171 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey48850c12006-11-16 22:43:37 +00005172 // Negate the size.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005173 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey48850c12006-11-16 22:43:37 +00005174 DAG.getConstant(0, PtrVT), Size);
5175 // Construct a node for the frame pointer save index.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005176 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey48850c12006-11-16 22:43:37 +00005177 // Build a DYNALLOC node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005178 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson9f944592009-08-11 20:47:22 +00005179 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Craig Topper48d114b2014-04-26 18:35:24 +00005180 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops);
Jim Laskey48850c12006-11-16 22:43:37 +00005181}
5182
Hal Finkel756810f2013-03-21 21:37:52 +00005183SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
5184 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005185 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00005186 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
5187 DAG.getVTList(MVT::i32, MVT::Other),
5188 Op.getOperand(0), Op.getOperand(1));
5189}
5190
5191SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
5192 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005193 SDLoc DL(Op);
Hal Finkel756810f2013-03-21 21:37:52 +00005194 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
5195 Op.getOperand(0), Op.getOperand(1));
5196}
5197
Hal Finkel940ab932014-02-28 00:27:01 +00005198SDValue PPCTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
5199 assert(Op.getValueType() == MVT::i1 &&
5200 "Custom lowering only for i1 loads");
5201
5202 // First, load 8 bits into 32 bits, then truncate to 1 bit.
5203
5204 SDLoc dl(Op);
5205 LoadSDNode *LD = cast<LoadSDNode>(Op);
5206
5207 SDValue Chain = LD->getChain();
5208 SDValue BasePtr = LD->getBasePtr();
5209 MachineMemOperand *MMO = LD->getMemOperand();
5210
5211 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, dl, getPointerTy(), Chain,
5212 BasePtr, MVT::i8, MMO);
5213 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewLD);
5214
5215 SDValue Ops[] = { Result, SDValue(NewLD.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00005216 return DAG.getMergeValues(Ops, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00005217}
5218
5219SDValue PPCTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
5220 assert(Op.getOperand(1).getValueType() == MVT::i1 &&
5221 "Custom lowering only for i1 stores");
5222
5223 // First, zero extend to 32 bits, then use a truncating store to 8 bits.
5224
5225 SDLoc dl(Op);
5226 StoreSDNode *ST = cast<StoreSDNode>(Op);
5227
5228 SDValue Chain = ST->getChain();
5229 SDValue BasePtr = ST->getBasePtr();
5230 SDValue Value = ST->getValue();
5231 MachineMemOperand *MMO = ST->getMemOperand();
5232
5233 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, getPointerTy(), Value);
5234 return DAG.getTruncStore(Chain, dl, Value, BasePtr, MVT::i8, MMO);
5235}
5236
5237// FIXME: Remove this once the ANDI glue bug is fixed:
5238SDValue PPCTargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
5239 assert(Op.getValueType() == MVT::i1 &&
5240 "Custom lowering only for i1 results");
5241
5242 SDLoc DL(Op);
5243 return DAG.getNode(PPCISD::ANDIo_1_GT_BIT, DL, MVT::i1,
5244 Op.getOperand(0));
5245}
5246
Chris Lattner4211ca92006-04-14 06:01:58 +00005247/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
5248/// possible.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005249SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner4211ca92006-04-14 06:01:58 +00005250 // Not FP? Not a fsel.
Duncan Sands13237ac2008-06-06 12:08:01 +00005251 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
5252 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedman5806e182009-05-28 04:31:08 +00005253 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005254
Hal Finkel81f87992013-04-07 22:11:09 +00005255 // We might be able to do better than this under some circumstances, but in
5256 // general, fsel-based lowering of select is a finite-math-only optimization.
5257 // For more information, see section F.3 of the 2.06 ISA specification.
5258 if (!DAG.getTarget().Options.NoInfsFPMath ||
5259 !DAG.getTarget().Options.NoNaNsFPMath)
5260 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005261
Hal Finkel81f87992013-04-07 22:11:09 +00005262 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005263
Owen Anderson53aa7a92009-08-10 22:56:29 +00005264 EVT ResVT = Op.getValueType();
5265 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005266 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
5267 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005268 SDLoc dl(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005269
Chris Lattner4211ca92006-04-14 06:01:58 +00005270 // If the RHS of the comparison is a 0.0, we don't need to do the
5271 // subtraction at all.
Hal Finkel81f87992013-04-07 22:11:09 +00005272 SDValue Sel1;
Chris Lattner4211ca92006-04-14 06:01:58 +00005273 if (isFloatingPointZero(RHS))
5274 switch (CC) {
5275 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00005276 case ISD::SETNE:
5277 std::swap(TV, FV);
5278 case ISD::SETEQ:
5279 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5280 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
5281 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
5282 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
5283 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
5284 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5285 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005286 case ISD::SETULT:
5287 case ISD::SETLT:
5288 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005289 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005290 case ISD::SETGE:
Owen Anderson9f944592009-08-11 20:47:22 +00005291 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5292 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005293 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005294 case ISD::SETUGT:
5295 case ISD::SETGT:
5296 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005297 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005298 case ISD::SETLE:
Owen Anderson9f944592009-08-11 20:47:22 +00005299 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
5300 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005301 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005302 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005303 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005304
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005305 SDValue Cmp;
Chris Lattner4211ca92006-04-14 06:01:58 +00005306 switch (CC) {
5307 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel81f87992013-04-07 22:11:09 +00005308 case ISD::SETNE:
5309 std::swap(TV, FV);
5310 case ISD::SETEQ:
5311 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
5312 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5313 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
5314 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
5315 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
5316 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
5317 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
5318 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005319 case ISD::SETULT:
5320 case ISD::SETLT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005321 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005322 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5323 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005324 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005325 case ISD::SETOGE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005326 case ISD::SETGE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005327 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005328 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5329 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005330 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005331 case ISD::SETUGT:
5332 case ISD::SETGT:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005333 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005334 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5335 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005336 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattnerb56d22c2006-05-24 00:06:44 +00005337 case ISD::SETOLE:
Chris Lattner4211ca92006-04-14 06:01:58 +00005338 case ISD::SETLE:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00005339 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson9f944592009-08-11 20:47:22 +00005340 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
5341 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel81f87992013-04-07 22:11:09 +00005342 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner4211ca92006-04-14 06:01:58 +00005343 }
Eli Friedman5806e182009-05-28 04:31:08 +00005344 return Op;
Chris Lattner4211ca92006-04-14 06:01:58 +00005345}
5346
Chris Lattner57ee7c62007-11-28 18:44:47 +00005347// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005348SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005349 SDLoc dl) const {
Duncan Sands13237ac2008-06-06 12:08:01 +00005350 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005351 SDValue Src = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00005352 if (Src.getValueType() == MVT::f32)
5353 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sands2a287912008-07-19 16:26:02 +00005354
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005355 SDValue Tmp;
Craig Topper56710102013-08-15 02:33:50 +00005356 switch (Op.getSimpleValueType().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005357 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson9f944592009-08-11 20:47:22 +00005358 case MVT::i32:
Dale Johannesen37bc85f2009-06-04 20:53:52 +00005359 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005360 (Subtarget.hasFPCVT() ? PPCISD::FCTIWUZ :
Hal Finkelf6d45f22013-04-01 17:52:07 +00005361 PPCISD::FCTIDZ),
Owen Anderson9f944592009-08-11 20:47:22 +00005362 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005363 break;
Owen Anderson9f944592009-08-11 20:47:22 +00005364 case MVT::i64:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005365 assert((Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT()) &&
Hal Finkel3f88d082013-04-01 18:42:58 +00005366 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkelf6d45f22013-04-01 17:52:07 +00005367 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
5368 PPCISD::FCTIDUZ,
5369 dl, MVT::f64, Src);
Chris Lattner4211ca92006-04-14 06:01:58 +00005370 break;
5371 }
Duncan Sands2a287912008-07-19 16:26:02 +00005372
Chris Lattner4211ca92006-04-14 06:01:58 +00005373 // Convert the FP value to an int value through memory.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005374 bool i32Stack = Op.getValueType() == MVT::i32 && Subtarget.hasSTFIWX() &&
5375 (Op.getOpcode() == ISD::FP_TO_SINT || Subtarget.hasFPCVT());
Hal Finkelf6d45f22013-04-01 17:52:07 +00005376 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
5377 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
5378 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sands2a287912008-07-19 16:26:02 +00005379
Chris Lattner06a49542007-10-15 20:14:52 +00005380 // Emit a store to the stack slot.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005381 SDValue Chain;
5382 if (i32Stack) {
5383 MachineFunction &MF = DAG.getMachineFunction();
5384 MachineMemOperand *MMO =
5385 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
5386 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
5387 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00005388 DAG.getVTList(MVT::Other), Ops, MVT::i32, MMO);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005389 } else
5390 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
5391 MPI, false, false, 0);
Chris Lattner06a49542007-10-15 20:14:52 +00005392
5393 // Result is a load from the stack slot. If loading 4 bytes, make sure to
5394 // add in a bias.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005395 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen021052a2009-02-04 20:06:27 +00005396 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner06a49542007-10-15 20:14:52 +00005397 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkelf6d45f22013-04-01 17:52:07 +00005398 MPI = MachinePointerInfo();
5399 }
5400
5401 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooper82cd9e82011-11-08 18:42:53 +00005402 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00005403}
5404
Hal Finkelf6d45f22013-04-01 17:52:07 +00005405SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00005406 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005407 SDLoc dl(Op);
Dan Gohmand6819da2008-03-11 01:59:03 +00005408 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson9f944592009-08-11 20:47:22 +00005409 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005410 return SDValue();
Dan Gohmand6819da2008-03-11 01:59:03 +00005411
Hal Finkel6a56b212014-03-05 22:14:00 +00005412 if (Op.getOperand(0).getValueType() == MVT::i1)
5413 return DAG.getNode(ISD::SELECT, dl, Op.getValueType(), Op.getOperand(0),
5414 DAG.getConstantFP(1.0, Op.getValueType()),
5415 DAG.getConstantFP(0.0, Op.getValueType()));
5416
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005417 assert((Op.getOpcode() == ISD::SINT_TO_FP || Subtarget.hasFPCVT()) &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005418 "UINT_TO_FP is supported only with FPCVT");
5419
5420 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel93d75ea2013-04-02 03:29:51 +00005421 // Otherwise, convert to double-precision and then round.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005422 unsigned FCFOp = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
Hal Finkelf6d45f22013-04-01 17:52:07 +00005423 (Op.getOpcode() == ISD::UINT_TO_FP ?
5424 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
5425 (Op.getOpcode() == ISD::UINT_TO_FP ?
5426 PPCISD::FCFIDU : PPCISD::FCFID);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005427 MVT FCFTy = (Subtarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
Hal Finkelf6d45f22013-04-01 17:52:07 +00005428 MVT::f32 : MVT::f64;
5429
Owen Anderson9f944592009-08-11 20:47:22 +00005430 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005431 SDValue SINT = Op.getOperand(0);
5432 // When converting to single-precision, we actually need to convert
5433 // to double-precision first and then round to single-precision.
5434 // To avoid double-rounding effects during that operation, we have
5435 // to prepare the input operand. Bits that might be truncated when
5436 // converting to double-precision are replaced by a bit that won't
5437 // be lost at this stage, but is below the single-precision rounding
5438 // position.
5439 //
5440 // However, if -enable-unsafe-fp-math is in effect, accept double
5441 // rounding to avoid the extra overhead.
5442 if (Op.getValueType() == MVT::f32 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005443 !Subtarget.hasFPCVT() &&
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005444 !DAG.getTarget().Options.UnsafeFPMath) {
5445
5446 // Twiddle input to make sure the low 11 bits are zero. (If this
5447 // is the case, we are guaranteed the value will fit into the 53 bit
5448 // mantissa of an IEEE double-precision value without rounding.)
5449 // If any of those low 11 bits were not zero originally, make sure
5450 // bit 12 (value 2048) is set instead, so that the final rounding
5451 // to single-precision gets the correct result.
5452 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5453 SINT, DAG.getConstant(2047, MVT::i64));
5454 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
5455 Round, DAG.getConstant(2047, MVT::i64));
5456 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
5457 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
5458 Round, DAG.getConstant(-2048, MVT::i64));
5459
5460 // However, we cannot use that value unconditionally: if the magnitude
5461 // of the input value is small, the bit-twiddling we did above might
5462 // end up visibly changing the output. Fortunately, in that case, we
5463 // don't need to twiddle bits since the original input will convert
5464 // exactly to double-precision floating-point already. Therefore,
5465 // construct a conditional to use the original value if the top 11
5466 // bits are all sign-bit copies, and use the rounded value computed
5467 // above otherwise.
5468 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
5469 SINT, DAG.getConstant(53, MVT::i32));
5470 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
5471 Cond, DAG.getConstant(1, MVT::i64));
5472 Cond = DAG.getSetCC(dl, MVT::i32,
5473 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
5474
5475 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
5476 }
Hal Finkelf6d45f22013-04-01 17:52:07 +00005477
Ulrich Weigandd34b5bd2012-10-18 13:16:11 +00005478 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkelf6d45f22013-04-01 17:52:07 +00005479 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
5480
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005481 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Scott Michelcf0da6c2009-02-17 22:15:04 +00005482 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005483 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005484 return FP;
5485 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005486
Owen Anderson9f944592009-08-11 20:47:22 +00005487 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005488 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner4211ca92006-04-14 06:01:58 +00005489 // Since we only generate this in 64-bit mode, we can take advantage of
5490 // 64-bit registers. In particular, sign extend the input value into the
5491 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
5492 // then lfd it and fcfid it.
Dan Gohman48b185d2009-09-25 20:36:54 +00005493 MachineFunction &MF = DAG.getMachineFunction();
5494 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005495 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005496
Hal Finkelbeb296b2013-03-31 10:12:51 +00005497 SDValue Ld;
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005498 if (Subtarget.hasLFIWAX() || Subtarget.hasFPCVT()) {
Hal Finkelbeb296b2013-03-31 10:12:51 +00005499 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
5500 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005501
Hal Finkelbeb296b2013-03-31 10:12:51 +00005502 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
5503 MachinePointerInfo::getFixedStack(FrameIdx),
5504 false, false, 0);
Hal Finkele53429a2013-03-31 01:58:02 +00005505
Hal Finkelbeb296b2013-03-31 10:12:51 +00005506 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
5507 "Expected an i32 store");
5508 MachineMemOperand *MMO =
5509 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
5510 MachineMemOperand::MOLoad, 4, 4);
5511 SDValue Ops[] = { Store, FIdx };
Hal Finkelf6d45f22013-04-01 17:52:07 +00005512 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
5513 PPCISD::LFIWZX : PPCISD::LFIWAX,
5514 dl, DAG.getVTList(MVT::f64, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00005515 Ops, MVT::i32, MMO);
Hal Finkelbeb296b2013-03-31 10:12:51 +00005516 } else {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005517 assert(Subtarget.isPPC64() &&
Hal Finkelf6d45f22013-04-01 17:52:07 +00005518 "i32->FP without LFIWAX supported only on PPC64");
5519
Hal Finkelbeb296b2013-03-31 10:12:51 +00005520 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
5521 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
5522
5523 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
5524 Op.getOperand(0));
5525
5526 // STD the extended value into the stack slot.
5527 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
5528 MachinePointerInfo::getFixedStack(FrameIdx),
5529 false, false, 0);
5530
5531 // Load the value as a double.
5532 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
5533 MachinePointerInfo::getFixedStack(FrameIdx),
5534 false, false, false, 0);
5535 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005536
Chris Lattner4211ca92006-04-14 06:01:58 +00005537 // FCFID it and return it.
Hal Finkelf6d45f22013-04-01 17:52:07 +00005538 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005539 if (Op.getValueType() == MVT::f32 && !Subtarget.hasFPCVT())
Owen Anderson9f944592009-08-11 20:47:22 +00005540 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner4211ca92006-04-14 06:01:58 +00005541 return FP;
5542}
5543
Dan Gohman21cea8a2010-04-17 15:26:15 +00005544SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
5545 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005546 SDLoc dl(Op);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005547 /*
5548 The rounding mode is in bits 30:31 of FPSR, and has the following
5549 settings:
5550 00 Round to nearest
5551 01 Round to 0
5552 10 Round to +inf
5553 11 Round to -inf
5554
5555 FLT_ROUNDS, on the other hand, expects the following:
5556 -1 Undefined
5557 0 Round to 0
5558 1 Round to nearest
5559 2 Round to +inf
5560 3 Round to -inf
5561
5562 To perform the conversion, we do:
5563 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
5564 */
5565
5566 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005567 EVT VT = Op.getValueType();
5568 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005569
5570 // Save FP Control Word to register
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005571 EVT NodeTys[] = {
5572 MVT::f64, // return register
5573 MVT::Glue // unused in this context
5574 };
Craig Topper2d2aa0c2014-04-30 07:17:30 +00005575 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, None);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005576
5577 // Save FP register to stack slot
David Greene1fbe0542009-11-12 20:49:22 +00005578 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005579 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005580 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner676c61d2010-09-21 18:41:36 +00005581 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005582
5583 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005584 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00005585 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattner7727d052010-09-21 06:44:06 +00005586 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005587 false, false, false, 0);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005588
5589 // Transform as necessary
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005590 SDValue CWD1 =
Owen Anderson9f944592009-08-11 20:47:22 +00005591 DAG.getNode(ISD::AND, dl, MVT::i32,
5592 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005593 SDValue CWD2 =
Owen Anderson9f944592009-08-11 20:47:22 +00005594 DAG.getNode(ISD::SRL, dl, MVT::i32,
5595 DAG.getNode(ISD::AND, dl, MVT::i32,
5596 DAG.getNode(ISD::XOR, dl, MVT::i32,
5597 CWD, DAG.getConstant(3, MVT::i32)),
5598 DAG.getConstant(3, MVT::i32)),
5599 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005600
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005601 SDValue RetVal =
Owen Anderson9f944592009-08-11 20:47:22 +00005602 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005603
Duncan Sands13237ac2008-06-06 12:08:01 +00005604 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen021052a2009-02-04 20:06:27 +00005605 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c94cb32008-01-18 19:55:37 +00005606}
5607
Dan Gohman21cea8a2010-04-17 15:26:15 +00005608SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005609 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005610 unsigned BitWidth = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005611 SDLoc dl(Op);
Dan Gohman8d2ead22008-03-07 20:36:53 +00005612 assert(Op.getNumOperands() == 3 &&
5613 VT == Op.getOperand(1).getValueType() &&
5614 "Unexpected SHL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005615
Chris Lattner601b8652006-09-20 03:47:40 +00005616 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005617 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005618 SDValue Lo = Op.getOperand(0);
5619 SDValue Hi = Op.getOperand(1);
5620 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005621 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005622
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005623 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005624 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005625 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5626 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5627 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5628 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005629 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005630 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5631 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5632 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005633 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005634 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005635}
5636
Dan Gohman21cea8a2010-04-17 15:26:15 +00005637SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005638 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005639 SDLoc dl(Op);
Duncan Sands13237ac2008-06-06 12:08:01 +00005640 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005641 assert(Op.getNumOperands() == 3 &&
5642 VT == Op.getOperand(1).getValueType() &&
5643 "Unexpected SRL!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005644
Dan Gohman8d2ead22008-03-07 20:36:53 +00005645 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner4211ca92006-04-14 06:01:58 +00005646 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005647 SDValue Lo = Op.getOperand(0);
5648 SDValue Hi = Op.getOperand(1);
5649 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005650 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005651
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005652 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005653 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005654 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5655 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5656 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5657 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005658 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen7ae8c8b2009-02-05 00:20:09 +00005659 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5660 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5661 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005662 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005663 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005664}
5665
Dan Gohman21cea8a2010-04-17 15:26:15 +00005666SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005667 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005668 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005669 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman8d2ead22008-03-07 20:36:53 +00005670 assert(Op.getNumOperands() == 3 &&
5671 VT == Op.getOperand(1).getValueType() &&
5672 "Unexpected SRA!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005673
Dan Gohman8d2ead22008-03-07 20:36:53 +00005674 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005675 SDValue Lo = Op.getOperand(0);
5676 SDValue Hi = Op.getOperand(1);
5677 SDValue Amt = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005678 EVT AmtVT = Amt.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005679
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005680 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands13105742008-10-30 19:28:32 +00005681 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005682 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5683 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5684 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5685 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands13105742008-10-30 19:28:32 +00005686 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00005687 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5688 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5689 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands13105742008-10-30 19:28:32 +00005690 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005691 SDValue OutOps[] = { OutLo, OutHi };
Craig Topper64941d92014-04-27 19:20:57 +00005692 return DAG.getMergeValues(OutOps, dl);
Chris Lattner4211ca92006-04-14 06:01:58 +00005693}
5694
5695//===----------------------------------------------------------------------===//
5696// Vector related lowering.
5697//
5698
Chris Lattner2a099c02006-04-17 06:00:21 +00005699/// BuildSplatI - Build a canonical splati of Val with an element size of
5700/// SplatSize. Cast the result to VT.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005701static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005702 SelectionDAG &DAG, SDLoc dl) {
Chris Lattner2a099c02006-04-17 06:00:21 +00005703 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005704
Owen Anderson53aa7a92009-08-10 22:56:29 +00005705 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson9f944592009-08-11 20:47:22 +00005706 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner2a099c02006-04-17 06:00:21 +00005707 };
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005708
Owen Anderson9f944592009-08-11 20:47:22 +00005709 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005710
Chris Lattner09ed0ff2006-12-01 01:45:39 +00005711 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5712 if (Val == -1)
5713 SplatSize = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005714
Owen Anderson53aa7a92009-08-10 22:56:29 +00005715 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelcf0da6c2009-02-17 22:15:04 +00005716
Chris Lattner2a099c02006-04-17 06:00:21 +00005717 // Build a canonical splat for this value.
Owen Anderson9f944592009-08-11 20:47:22 +00005718 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005719 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00005720 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Craig Topper48d114b2014-04-26 18:35:24 +00005721 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005722 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005723}
5724
Hal Finkelcf2e9082013-05-24 23:00:14 +00005725/// BuildIntrinsicOp - Return a unary operator intrinsic node with the
5726/// specified intrinsic ID.
5727static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005728 SelectionDAG &DAG, SDLoc dl,
Hal Finkelcf2e9082013-05-24 23:00:14 +00005729 EVT DestVT = MVT::Other) {
5730 if (DestVT == MVT::Other) DestVT = Op.getValueType();
5731 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
5732 DAG.getConstant(IID, MVT::i32), Op);
5733}
5734
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005735/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner1b3806a2006-04-17 06:58:41 +00005736/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005737static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005738 SelectionDAG &DAG, SDLoc dl,
Owen Anderson9f944592009-08-11 20:47:22 +00005739 EVT DestVT = MVT::Other) {
5740 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005741 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005742 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005743}
5744
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005745/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5746/// specified intrinsic ID.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005747static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005748 SDValue Op2, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005749 SDLoc dl, EVT DestVT = MVT::Other) {
Owen Anderson9f944592009-08-11 20:47:22 +00005750 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005751 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson9f944592009-08-11 20:47:22 +00005752 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00005753}
5754
5755
Chris Lattner264c9082006-04-17 17:55:10 +00005756/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5757/// amount. The result has the specified value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005758static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005759 EVT VT, SelectionDAG &DAG, SDLoc dl) {
Chris Lattner264c9082006-04-17 17:55:10 +00005760 // Force LHS/RHS to be the right type.
Wesley Peck527da1b2010-11-23 03:31:01 +00005761 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5762 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsb0e39382008-07-21 10:20:31 +00005763
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005764 int Ops[16];
Chris Lattner264c9082006-04-17 17:55:10 +00005765 for (unsigned i = 0; i != 16; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005766 Ops[i] = i + Amt;
Owen Anderson9f944592009-08-11 20:47:22 +00005767 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005768 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner264c9082006-04-17 17:55:10 +00005769}
5770
Chris Lattner19e90552006-04-14 05:19:18 +00005771// If this is a case we can't handle, return null and let the default
5772// expansion code take care of it. If we CAN select this case, and if it
5773// selects to a single instruction, return Op. Otherwise, if we can codegen
5774// this case more efficiently than a constant pool load, lower it to the
5775// sequence of ops that should be used.
Dan Gohman21cea8a2010-04-17 15:26:15 +00005776SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5777 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005778 SDLoc dl(Op);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005779 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
Craig Toppere73658d2014-04-28 04:05:08 +00005780 assert(BVN && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Michelbb878282009-02-25 03:12:50 +00005781
Bob Wilson85cefe82009-03-02 23:24:16 +00005782 // Check if this is a splat of a constant value.
5783 APInt APSplatBits, APSplatUndef;
5784 unsigned SplatBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00005785 bool HasAnyUndefs;
Bob Wilson530e0382009-03-03 19:26:27 +00005786 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00005787 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilson530e0382009-03-03 19:26:27 +00005788 return SDValue();
Evan Chenga49de9d2009-02-25 22:49:59 +00005789
Bob Wilson530e0382009-03-03 19:26:27 +00005790 unsigned SplatBits = APSplatBits.getZExtValue();
5791 unsigned SplatUndef = APSplatUndef.getZExtValue();
5792 unsigned SplatSize = SplatBitSize / 8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005793
Bob Wilson530e0382009-03-03 19:26:27 +00005794 // First, handle single instruction cases.
5795
5796 // All zeros?
5797 if (SplatBits == 0) {
5798 // Canonicalize all zero vectors to be v4i32.
Owen Anderson9f944592009-08-11 20:47:22 +00005799 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5800 SDValue Z = DAG.getConstant(0, MVT::i32);
5801 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peck527da1b2010-11-23 03:31:01 +00005802 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattner19e90552006-04-14 05:19:18 +00005803 }
Bob Wilson530e0382009-03-03 19:26:27 +00005804 return Op;
5805 }
Chris Lattnerfa5aa392006-04-16 01:01:29 +00005806
Bob Wilson530e0382009-03-03 19:26:27 +00005807 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5808 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5809 (32-SplatBitSize));
5810 if (SextVal >= -16 && SextVal <= 15)
5811 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005812
5813
Bob Wilson530e0382009-03-03 19:26:27 +00005814 // Two instruction sequences.
Scott Michelcf0da6c2009-02-17 22:15:04 +00005815
Bob Wilson530e0382009-03-03 19:26:27 +00005816 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00005817 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5818 // If this value is in the range [17,31] and is odd, use:
5819 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5820 // If this value is in the range [-31,-17] and is odd, use:
5821 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5822 // Note the last two are three-instruction sequences.
5823 if (SextVal >= -32 && SextVal <= 31) {
5824 // To avoid having these optimizations undone by constant folding,
5825 // we convert to a pseudo that will be expanded later into one of
5826 // the above forms.
5827 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidt71dddd52014-05-27 15:57:51 +00005828 EVT VT = (SplatSize == 1 ? MVT::v16i8 :
5829 (SplatSize == 2 ? MVT::v8i16 : MVT::v4i32));
5830 SDValue EltSize = DAG.getConstant(SplatSize, MVT::i32);
5831 SDValue RetVal = DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
5832 if (VT == Op.getValueType())
5833 return RetVal;
5834 else
5835 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), RetVal);
Bob Wilson530e0382009-03-03 19:26:27 +00005836 }
5837
5838 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5839 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5840 // for fneg/fabs.
5841 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5842 // Make -1 and vspltisw -1:
Owen Anderson9f944592009-08-11 20:47:22 +00005843 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005844
5845 // Make the VSLW intrinsic, computing 0x8000_0000.
5846 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5847 OnesV, DAG, dl);
5848
5849 // xor by OnesV to invert it.
Owen Anderson9f944592009-08-11 20:47:22 +00005850 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peck527da1b2010-11-23 03:31:01 +00005851 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005852 }
5853
Bill Schmidt4aedff82014-06-06 14:06:26 +00005854 // The remaining cases assume either big endian element order or
5855 // a splat-size that equates to the element size of the vector
5856 // to be built. An example that doesn't work for little endian is
5857 // {0, -1, 0, -1, 0, -1, 0, -1} which has a splat size of 32 bits
5858 // and a vector element size of 16 bits. The code below will
5859 // produce the vector in big endian element order, which for little
5860 // endian is {-1, 0, -1, 0, -1, 0, -1, 0}.
5861
5862 // For now, just avoid these optimizations in that case.
5863 // FIXME: Develop correct optimizations for LE with mismatched
5864 // splat and element sizes.
5865
Eric Christopherb1aaebe2014-06-12 22:38:18 +00005866 if (Subtarget.isLittleEndian() &&
Bill Schmidt4aedff82014-06-06 14:06:26 +00005867 SplatSize != Op.getValueType().getVectorElementType().getSizeInBits())
5868 return SDValue();
5869
Bob Wilson530e0382009-03-03 19:26:27 +00005870 // Check to see if this is a wide variety of vsplti*, binop self cases.
5871 static const signed char SplatCsts[] = {
5872 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5873 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5874 };
5875
5876 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5877 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5878 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5879 int i = SplatCsts[idx];
5880
5881 // Figure out what shift amount will be used by altivec if shifted by i in
5882 // this splat size.
5883 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5884
5885 // vsplti + shl self.
Richard Smith228e6d42012-08-24 23:29:28 +00005886 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005887 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005888 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5889 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5890 Intrinsic::ppc_altivec_vslw
5891 };
5892 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005893 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner2a099c02006-04-17 06:00:21 +00005894 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005895
Bob Wilson530e0382009-03-03 19:26:27 +00005896 // vsplti + srl self.
5897 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005898 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005899 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5900 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5901 Intrinsic::ppc_altivec_vsrw
5902 };
5903 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005904 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005905 }
5906
Bob Wilson530e0382009-03-03 19:26:27 +00005907 // vsplti + sra self.
5908 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson9f944592009-08-11 20:47:22 +00005909 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005910 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5911 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5912 Intrinsic::ppc_altivec_vsraw
5913 };
5914 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005915 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner1b3806a2006-04-17 06:58:41 +00005916 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005917
Bob Wilson530e0382009-03-03 19:26:27 +00005918 // vsplti + rol self.
5919 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5920 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005921 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005922 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5923 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5924 Intrinsic::ppc_altivec_vrlw
5925 };
5926 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peck527da1b2010-11-23 03:31:01 +00005927 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilson530e0382009-03-03 19:26:27 +00005928 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005929
Bob Wilson530e0382009-03-03 19:26:27 +00005930 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith228e6d42012-08-24 23:29:28 +00005931 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005932 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005933 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnere54133c2006-04-17 18:09:22 +00005934 }
Bob Wilson530e0382009-03-03 19:26:27 +00005935 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith228e6d42012-08-24 23:29:28 +00005936 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005937 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005938 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattner19e90552006-04-14 05:19:18 +00005939 }
Bob Wilson530e0382009-03-03 19:26:27 +00005940 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith228e6d42012-08-24 23:29:28 +00005941 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson9f944592009-08-11 20:47:22 +00005942 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilson530e0382009-03-03 19:26:27 +00005943 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5944 }
5945 }
5946
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005947 return SDValue();
Chris Lattner19e90552006-04-14 05:19:18 +00005948}
5949
Chris Lattner071ad012006-04-17 05:28:54 +00005950/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5951/// the specified operations to build the shuffle.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005952static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005953 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005954 SDLoc dl) {
Chris Lattner071ad012006-04-17 05:28:54 +00005955 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling95e1af22008-09-17 00:30:57 +00005956 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner071ad012006-04-17 05:28:54 +00005957 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005958
Chris Lattner071ad012006-04-17 05:28:54 +00005959 enum {
Chris Lattnerd2ca9ab2006-05-16 04:20:24 +00005960 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 +00005961 OP_VMRGHW,
5962 OP_VMRGLW,
5963 OP_VSPLTISW0,
5964 OP_VSPLTISW1,
5965 OP_VSPLTISW2,
5966 OP_VSPLTISW3,
5967 OP_VSLDOI4,
5968 OP_VSLDOI8,
Chris Lattneraa2372562006-05-24 17:04:05 +00005969 OP_VSLDOI12
Chris Lattner071ad012006-04-17 05:28:54 +00005970 };
Scott Michelcf0da6c2009-02-17 22:15:04 +00005971
Chris Lattner071ad012006-04-17 05:28:54 +00005972 if (OpNum == OP_COPY) {
5973 if (LHSID == (1*9+2)*9+3) return LHS;
5974 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5975 return RHS;
5976 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005977
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005978 SDValue OpLHS, OpRHS;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00005979 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5980 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005981
Nate Begeman8d6d4b92009-04-27 18:41:29 +00005982 int ShufIdxs[16];
Chris Lattner071ad012006-04-17 05:28:54 +00005983 switch (OpNum) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005984 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner071ad012006-04-17 05:28:54 +00005985 case OP_VMRGHW:
5986 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5987 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5988 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5989 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5990 break;
5991 case OP_VMRGLW:
5992 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5993 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5994 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5995 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5996 break;
5997 case OP_VSPLTISW0:
5998 for (unsigned i = 0; i != 16; ++i)
5999 ShufIdxs[i] = (i&3)+0;
6000 break;
6001 case OP_VSPLTISW1:
6002 for (unsigned i = 0; i != 16; ++i)
6003 ShufIdxs[i] = (i&3)+4;
6004 break;
6005 case OP_VSPLTISW2:
6006 for (unsigned i = 0; i != 16; ++i)
6007 ShufIdxs[i] = (i&3)+8;
6008 break;
6009 case OP_VSPLTISW3:
6010 for (unsigned i = 0; i != 16; ++i)
6011 ShufIdxs[i] = (i&3)+12;
6012 break;
6013 case OP_VSLDOI4:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006014 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006015 case OP_VSLDOI8:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006016 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006017 case OP_VSLDOI12:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006018 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006019 }
Owen Anderson53aa7a92009-08-10 22:56:29 +00006020 EVT VT = OpLHS.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00006021 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
6022 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson9f944592009-08-11 20:47:22 +00006023 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peck527da1b2010-11-23 03:31:01 +00006024 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner071ad012006-04-17 05:28:54 +00006025}
6026
Chris Lattner19e90552006-04-14 05:19:18 +00006027/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
6028/// is a shuffle we can handle in a single instruction, return it. Otherwise,
6029/// return the code it can be lowered into. Worst case, it can always be
6030/// lowered into a vperm.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006031SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006032 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006033 SDLoc dl(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006034 SDValue V1 = Op.getOperand(0);
6035 SDValue V2 = Op.getOperand(1);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006036 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006037 EVT VT = Op.getValueType();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006038 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006039
Chris Lattner19e90552006-04-14 05:19:18 +00006040 // Cases that are handled by instructions that take permute immediates
6041 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
6042 // selected by the instruction selector.
6043 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006044 if (PPC::isSplatShuffleMask(SVOp, 1) ||
6045 PPC::isSplatShuffleMask(SVOp, 2) ||
6046 PPC::isSplatShuffleMask(SVOp, 4) ||
Bill Schmidtf910a062014-06-10 14:35:01 +00006047 PPC::isVPKUWUMShuffleMask(SVOp, true, DAG) ||
6048 PPC::isVPKUHUMShuffleMask(SVOp, true, DAG) ||
6049 PPC::isVSLDOIShuffleMask(SVOp, true, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00006050 PPC::isVMRGLShuffleMask(SVOp, 1, 1, DAG) ||
6051 PPC::isVMRGLShuffleMask(SVOp, 2, 1, DAG) ||
6052 PPC::isVMRGLShuffleMask(SVOp, 4, 1, DAG) ||
6053 PPC::isVMRGHShuffleMask(SVOp, 1, 1, DAG) ||
6054 PPC::isVMRGHShuffleMask(SVOp, 2, 1, DAG) ||
6055 PPC::isVMRGHShuffleMask(SVOp, 4, 1, DAG)) {
Chris Lattner19e90552006-04-14 05:19:18 +00006056 return Op;
6057 }
6058 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006059
Chris Lattner19e90552006-04-14 05:19:18 +00006060 // Altivec has a variety of "shuffle immediates" that take two vector inputs
6061 // and produce a fixed permutation. If any of these match, do not lower to
6062 // VPERM.
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00006063 unsigned int ShuffleKind = isLittleEndian ? 2 : 0;
Bill Schmidtf910a062014-06-10 14:35:01 +00006064 if (PPC::isVPKUWUMShuffleMask(SVOp, false, DAG) ||
6065 PPC::isVPKUHUMShuffleMask(SVOp, false, DAG) ||
6066 PPC::isVSLDOIShuffleMask(SVOp, false, DAG) != -1 ||
Bill Schmidtc9fa5dd2014-07-25 01:55:55 +00006067 PPC::isVMRGLShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
6068 PPC::isVMRGLShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
6069 PPC::isVMRGLShuffleMask(SVOp, 4, ShuffleKind, DAG) ||
6070 PPC::isVMRGHShuffleMask(SVOp, 1, ShuffleKind, DAG) ||
6071 PPC::isVMRGHShuffleMask(SVOp, 2, ShuffleKind, DAG) ||
6072 PPC::isVMRGHShuffleMask(SVOp, 4, ShuffleKind, DAG))
Chris Lattner19e90552006-04-14 05:19:18 +00006073 return Op;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006074
Chris Lattner071ad012006-04-17 05:28:54 +00006075 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
6076 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramer339ced42012-01-15 13:16:05 +00006077 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peck527da1b2010-11-23 03:31:01 +00006078
Chris Lattner071ad012006-04-17 05:28:54 +00006079 unsigned PFIndexes[4];
6080 bool isFourElementShuffle = true;
6081 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
6082 unsigned EltNo = 8; // Start out undef.
6083 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006084 if (PermMask[i*4+j] < 0)
Chris Lattner071ad012006-04-17 05:28:54 +00006085 continue; // Undef, ignore it.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006086
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006087 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner071ad012006-04-17 05:28:54 +00006088 if ((ByteSource & 3) != j) {
6089 isFourElementShuffle = false;
6090 break;
6091 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006092
Chris Lattner071ad012006-04-17 05:28:54 +00006093 if (EltNo == 8) {
6094 EltNo = ByteSource/4;
6095 } else if (EltNo != ByteSource/4) {
6096 isFourElementShuffle = false;
6097 break;
6098 }
6099 }
6100 PFIndexes[i] = EltNo;
6101 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006102
6103 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner071ad012006-04-17 05:28:54 +00006104 // perfect shuffle vector to determine if it is cost effective to do this as
6105 // discrete instructions, or whether we should use a vperm.
Bill Schmidtf910a062014-06-10 14:35:01 +00006106 // For now, we skip this for little endian until such time as we have a
6107 // little-endian perfect shuffle table.
6108 if (isFourElementShuffle && !isLittleEndian) {
Chris Lattner071ad012006-04-17 05:28:54 +00006109 // Compute the index in the perfect shuffle table.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006110 unsigned PFTableIndex =
Chris Lattner071ad012006-04-17 05:28:54 +00006111 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelcf0da6c2009-02-17 22:15:04 +00006112
Chris Lattner071ad012006-04-17 05:28:54 +00006113 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6114 unsigned Cost = (PFEntry >> 30);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006115
Chris Lattner071ad012006-04-17 05:28:54 +00006116 // Determining when to avoid vperm is tricky. Many things affect the cost
6117 // of vperm, particularly how many times the perm mask needs to be computed.
6118 // For example, if the perm mask can be hoisted out of a loop or is already
6119 // used (perhaps because there are multiple permutes with the same shuffle
6120 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
6121 // the loop requires an extra register.
6122 //
6123 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelcf0da6c2009-02-17 22:15:04 +00006124 // generated in 3 or fewer operations. When we have loop information
Chris Lattner071ad012006-04-17 05:28:54 +00006125 // available, if this block is within a loop, we should avoid using vperm
6126 // for 3-operation perms and use a constant pool load instead.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006127 if (Cost < 3)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006128 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner071ad012006-04-17 05:28:54 +00006129 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006130
Chris Lattner19e90552006-04-14 05:19:18 +00006131 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
6132 // vector that will get spilled to the constant pool.
6133 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006134
Chris Lattner19e90552006-04-14 05:19:18 +00006135 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
6136 // that it is in input element units, not in bytes. Convert now.
Bill Schmidt4aedff82014-06-06 14:06:26 +00006137
6138 // For little endian, the order of the input vectors is reversed, and
6139 // the permutation mask is complemented with respect to 31. This is
6140 // necessary to produce proper semantics with the big-endian-biased vperm
6141 // instruction.
Owen Anderson53aa7a92009-08-10 22:56:29 +00006142 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006143 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006144
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006145 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006146 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
6147 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00006148
Chris Lattner19e90552006-04-14 05:19:18 +00006149 for (unsigned j = 0; j != BytesPerElement; ++j)
Bill Schmidt4aedff82014-06-06 14:06:26 +00006150 if (isLittleEndian)
6151 ResultMask.push_back(DAG.getConstant(31 - (SrcElt*BytesPerElement+j),
6152 MVT::i32));
6153 else
6154 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
6155 MVT::i32));
Chris Lattner19e90552006-04-14 05:19:18 +00006156 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006157
Owen Anderson9f944592009-08-11 20:47:22 +00006158 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Craig Topper48d114b2014-04-26 18:35:24 +00006159 ResultMask);
Bill Schmidt4aedff82014-06-06 14:06:26 +00006160 if (isLittleEndian)
6161 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
6162 V2, V1, VPermMask);
6163 else
6164 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(),
6165 V1, V2, VPermMask);
Chris Lattner19e90552006-04-14 05:19:18 +00006166}
6167
Chris Lattner9754d142006-04-18 17:59:36 +00006168/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
6169/// altivec comparison. If it is, return true and fill in Opc/isDot with
6170/// information about the intrinsic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006171static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner9754d142006-04-18 17:59:36 +00006172 bool &isDot) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00006173 unsigned IntrinsicID =
6174 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00006175 CompareOpc = -1;
6176 isDot = false;
6177 switch (IntrinsicID) {
6178 default: return false;
6179 // Comparison predicates.
Chris Lattner4211ca92006-04-14 06:01:58 +00006180 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
6181 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
6182 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
6183 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
6184 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
6185 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
6186 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
6187 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
6188 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
6189 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
6190 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
6191 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
6192 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006193
Chris Lattner4211ca92006-04-14 06:01:58 +00006194 // Normal Comparisons.
6195 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
6196 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
6197 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
6198 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
6199 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
6200 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
6201 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
6202 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
6203 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
6204 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
6205 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
6206 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
6207 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
6208 }
Chris Lattner9754d142006-04-18 17:59:36 +00006209 return true;
6210}
6211
6212/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
6213/// lower, do it, otherwise return null.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006214SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006215 SelectionDAG &DAG) const {
Chris Lattner9754d142006-04-18 17:59:36 +00006216 // If this is a lowered altivec predicate compare, CompareOpc is set to the
6217 // opcode number of the comparison.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006218 SDLoc dl(Op);
Chris Lattner9754d142006-04-18 17:59:36 +00006219 int CompareOpc;
6220 bool isDot;
6221 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006222 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006223
Chris Lattner9754d142006-04-18 17:59:36 +00006224 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner4211ca92006-04-14 06:01:58 +00006225 if (!isDot) {
Dale Johannesenf80493b2009-02-05 22:07:54 +00006226 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner9fa851b2010-03-14 22:44:11 +00006227 Op.getOperand(1), Op.getOperand(2),
6228 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00006229 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner4211ca92006-04-14 06:01:58 +00006230 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006231
Chris Lattner4211ca92006-04-14 06:01:58 +00006232 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006233 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00006234 Op.getOperand(2), // LHS
6235 Op.getOperand(3), // RHS
Owen Anderson9f944592009-08-11 20:47:22 +00006236 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00006237 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00006238 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00006239 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006240
Chris Lattner4211ca92006-04-14 06:01:58 +00006241 // Now that we have the comparison, emit a copy from the CR to a GPR.
6242 // This is flagged to the above dot comparison.
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00006243 SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32,
Owen Anderson9f944592009-08-11 20:47:22 +00006244 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelcf0da6c2009-02-17 22:15:04 +00006245 CompNode.getValue(1));
6246
Chris Lattner4211ca92006-04-14 06:01:58 +00006247 // Unpack the result based on how the target uses it.
6248 unsigned BitNo; // Bit # of CR6.
6249 bool InvertBit; // Invert result?
Dan Gohmaneffb8942008-09-12 16:56:44 +00006250 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner4211ca92006-04-14 06:01:58 +00006251 default: // Can't happen, don't crash on invalid number though.
6252 case 0: // Return the value of the EQ bit of CR6.
6253 BitNo = 0; InvertBit = false;
6254 break;
6255 case 1: // Return the inverted value of the EQ bit of CR6.
6256 BitNo = 0; InvertBit = true;
6257 break;
6258 case 2: // Return the value of the LT bit of CR6.
6259 BitNo = 2; InvertBit = false;
6260 break;
6261 case 3: // Return the inverted value of the LT bit of CR6.
6262 BitNo = 2; InvertBit = true;
6263 break;
6264 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006265
Chris Lattner4211ca92006-04-14 06:01:58 +00006266 // Shift the bit into the low position.
Owen Anderson9f944592009-08-11 20:47:22 +00006267 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
6268 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00006269 // Isolate the bit.
Owen Anderson9f944592009-08-11 20:47:22 +00006270 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
6271 DAG.getConstant(1, MVT::i32));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006272
Chris Lattner4211ca92006-04-14 06:01:58 +00006273 // If we are supposed to, toggle the bit.
6274 if (InvertBit)
Owen Anderson9f944592009-08-11 20:47:22 +00006275 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
6276 DAG.getConstant(1, MVT::i32));
Chris Lattner4211ca92006-04-14 06:01:58 +00006277 return Flags;
6278}
6279
Hal Finkel5c0d1452014-03-30 13:22:59 +00006280SDValue PPCTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
6281 SelectionDAG &DAG) const {
6282 SDLoc dl(Op);
6283 // For v2i64 (VSX), we can pattern patch the v2i32 case (using fp <-> int
6284 // instructions), but for smaller types, we need to first extend up to v2i32
6285 // before doing going farther.
6286 if (Op.getValueType() == MVT::v2i64) {
6287 EVT ExtVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
6288 if (ExtVT != MVT::v2i32) {
6289 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op.getOperand(0));
6290 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, Op,
6291 DAG.getValueType(EVT::getVectorVT(*DAG.getContext(),
6292 ExtVT.getVectorElementType(), 4)));
6293 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Op);
6294 Op = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v2i64, Op,
6295 DAG.getValueType(MVT::v2i32));
6296 }
6297
6298 return Op;
6299 }
6300
6301 return SDValue();
6302}
6303
Scott Michelcf0da6c2009-02-17 22:15:04 +00006304SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006305 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006306 SDLoc dl(Op);
Chris Lattner4211ca92006-04-14 06:01:58 +00006307 // Create a stack slot that is 16-byte aligned.
6308 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00006309 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen81bfca72010-05-03 22:59:34 +00006310 EVT PtrVT = getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006311 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006312
Chris Lattner4211ca92006-04-14 06:01:58 +00006313 // Store the input value into Value#0 of the stack slot.
Dale Johannesen021052a2009-02-04 20:06:27 +00006314 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00006315 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene87a5abe2010-02-15 16:56:53 +00006316 false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00006317 // Load it out.
Chris Lattner7727d052010-09-21 06:44:06 +00006318 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006319 false, false, false, 0);
Chris Lattner4211ca92006-04-14 06:01:58 +00006320}
6321
Dan Gohman21cea8a2010-04-17 15:26:15 +00006322SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006323 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006324 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006325 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006326
Owen Anderson9f944592009-08-11 20:47:22 +00006327 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
6328 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006329
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006330 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006331 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006332
Chris Lattner7e4398742006-04-18 03:43:48 +00006333 // Shrinkify inputs to v8i16.
Wesley Peck527da1b2010-11-23 03:31:01 +00006334 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
6335 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
6336 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006337
Chris Lattner7e4398742006-04-18 03:43:48 +00006338 // Low parts multiplied together, generating 32-bit results (we ignore the
6339 // top parts).
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006340 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson9f944592009-08-11 20:47:22 +00006341 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006342
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006343 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson9f944592009-08-11 20:47:22 +00006344 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner7e4398742006-04-18 03:43:48 +00006345 // Shift the high parts up 16 bits.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006346 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006347 Neg16, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00006348 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
6349 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006350 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006351
Owen Anderson9f944592009-08-11 20:47:22 +00006352 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner7e4398742006-04-18 03:43:48 +00006353
Chris Lattner96d50482006-04-18 04:28:57 +00006354 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00006355 LHS, RHS, Zero, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00006356 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006357 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006358 bool isLittleEndian = Subtarget.isLittleEndian();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006359
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006360 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006361 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson9f944592009-08-11 20:47:22 +00006362 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00006363 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006364
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006365 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006366 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson9f944592009-08-11 20:47:22 +00006367 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peck527da1b2010-11-23 03:31:01 +00006368 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006369
Bill Schmidt42995e82014-06-09 16:06:29 +00006370 // Merge the results together. Because vmuleub and vmuloub are
6371 // instructions with a big-endian bias, we must reverse the
6372 // element numbering and reverse the meaning of "odd" and "even"
6373 // when generating little endian code.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006374 int Ops[16];
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006375 for (unsigned i = 0; i != 8; ++i) {
Bill Schmidt42995e82014-06-09 16:06:29 +00006376 if (isLittleEndian) {
6377 Ops[i*2 ] = 2*i;
6378 Ops[i*2+1] = 2*i+16;
6379 } else {
6380 Ops[i*2 ] = 2*i+1;
6381 Ops[i*2+1] = 2*i+1+16;
6382 }
Chris Lattnerd6d82aa2006-04-18 03:57:35 +00006383 }
Bill Schmidt42995e82014-06-09 16:06:29 +00006384 if (isLittleEndian)
6385 return DAG.getVectorShuffle(MVT::v16i8, dl, OddParts, EvenParts, Ops);
6386 else
6387 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner7e4398742006-04-18 03:43:48 +00006388 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006389 llvm_unreachable("Unknown mul to lower!");
Chris Lattner7e4398742006-04-18 03:43:48 +00006390 }
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006391}
6392
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006393/// LowerOperation - Provide custom lowering hooks for some operations.
6394///
Dan Gohman21cea8a2010-04-17 15:26:15 +00006395SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006396 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006397 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner4211ca92006-04-14 06:01:58 +00006398 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonf84f7102009-11-04 21:31:18 +00006399 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006400 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackye3f15c982012-06-04 17:36:38 +00006401 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00006402 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006403 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sandsa0984362011-09-06 13:37:06 +00006404 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
6405 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006406 case ISD::VASTART:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006407 return LowerVASTART(Op, DAG, Subtarget);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006408
6409 case ISD::VAARG:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006410 return LowerVAARG(Op, DAG, Subtarget);
Nicolas Geoffray23710a72007-04-03 13:59:52 +00006411
Roman Divackyc3825df2013-07-25 21:36:47 +00006412 case ISD::VACOPY:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006413 return LowerVACOPY(Op, DAG, Subtarget);
Roman Divackyc3825df2013-07-25 21:36:47 +00006414
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006415 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, Subtarget);
Chris Lattner43df5b32007-02-25 05:34:32 +00006416 case ISD::DYNAMIC_STACKALLOC:
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006417 return LowerDYNAMIC_STACKALLOC(Op, DAG, Subtarget);
Evan Cheng51096af2008-04-19 01:30:48 +00006418
Hal Finkel756810f2013-03-21 21:37:52 +00006419 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
6420 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
6421
Hal Finkel940ab932014-02-28 00:27:01 +00006422 case ISD::LOAD: return LowerLOAD(Op, DAG);
6423 case ISD::STORE: return LowerSTORE(Op, DAG);
6424 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Chris Lattner4211ca92006-04-14 06:01:58 +00006425 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006426 case ISD::FP_TO_UINT:
6427 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00006428 SDLoc(Op));
Hal Finkelf6d45f22013-04-01 17:52:07 +00006429 case ISD::UINT_TO_FP:
6430 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman9ba4d762008-01-31 00:41:03 +00006431 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006432
Chris Lattner4211ca92006-04-14 06:01:58 +00006433 // Lower 64-bit shifts.
Chris Lattner601b8652006-09-20 03:47:40 +00006434 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
6435 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
6436 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattner4a66d692006-03-22 05:30:33 +00006437
Chris Lattner4211ca92006-04-14 06:01:58 +00006438 // Vector-related lowering.
6439 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6440 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6441 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6442 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Hal Finkel5c0d1452014-03-30 13:22:59 +00006443 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Chris Lattnera2cae1b2006-04-18 03:24:30 +00006444 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006445
Hal Finkel25c19922013-05-15 21:37:41 +00006446 // For counter-based loop handling.
6447 case ISD::INTRINSIC_W_CHAIN: return SDValue();
6448
Chris Lattnerf6a81562007-12-08 06:59:59 +00006449 // Frame & Return address.
6450 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00006451 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnere675a082005-08-31 20:23:54 +00006452 }
Chris Lattnerf3d06c62005-08-26 00:52:45 +00006453}
6454
Duncan Sands6ed40142008-12-01 11:39:25 +00006455void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
6456 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006457 SelectionDAG &DAG) const {
Roman Divacky4394e682011-06-28 15:30:42 +00006458 const TargetMachine &TM = getTargetMachine();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006459 SDLoc dl(N);
Chris Lattner57ee7c62007-11-28 18:44:47 +00006460 switch (N->getOpcode()) {
Duncan Sands4068a7f2008-10-28 15:00:32 +00006461 default:
Craig Toppere55c5562012-02-07 02:50:20 +00006462 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkel25c19922013-05-15 21:37:41 +00006463 case ISD::INTRINSIC_W_CHAIN: {
6464 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
6465 Intrinsic::ppc_is_decremented_ctr_nonzero)
6466 break;
6467
6468 assert(N->getValueType(0) == MVT::i1 &&
6469 "Unexpected result type for CTR decrement intrinsic");
Matt Arsenault758659232013-05-18 00:21:46 +00006470 EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0));
Hal Finkel25c19922013-05-15 21:37:41 +00006471 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
6472 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
6473 N->getOperand(1));
6474
6475 Results.push_back(NewInt);
6476 Results.push_back(NewInt.getValue(1));
6477 break;
6478 }
Roman Divacky4394e682011-06-28 15:30:42 +00006479 case ISD::VAARG: {
6480 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
6481 || TM.getSubtarget<PPCSubtarget>().isPPC64())
6482 return;
6483
6484 EVT VT = N->getValueType(0);
6485
6486 if (VT == MVT::i64) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006487 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, Subtarget);
Roman Divacky4394e682011-06-28 15:30:42 +00006488
6489 Results.push_back(NewNode);
6490 Results.push_back(NewNode.getValue(1));
6491 }
6492 return;
6493 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006494 case ISD::FP_ROUND_INREG: {
Owen Anderson9f944592009-08-11 20:47:22 +00006495 assert(N->getValueType(0) == MVT::ppcf128);
6496 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006497 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006498 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006499 DAG.getIntPtrConstant(0));
Dale Johannesenf80493b2009-02-05 22:07:54 +00006500 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00006501 MVT::f64, N->getOperand(0),
Duncan Sands6ed40142008-12-01 11:39:25 +00006502 DAG.getIntPtrConstant(1));
6503
Ulrich Weigand874fc622013-03-26 10:56:22 +00006504 // Add the two halves of the long double in round-to-zero mode.
6505 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands6ed40142008-12-01 11:39:25 +00006506
6507 // We know the low half is about to be thrown away, so just use something
6508 // convenient.
Owen Anderson9f944592009-08-11 20:47:22 +00006509 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesenf80493b2009-02-05 22:07:54 +00006510 FPreg, FPreg));
Duncan Sands6ed40142008-12-01 11:39:25 +00006511 return;
Duncan Sands2a287912008-07-19 16:26:02 +00006512 }
Duncan Sands6ed40142008-12-01 11:39:25 +00006513 case ISD::FP_TO_SINT:
Bill Schmidt41221692013-07-09 18:50:20 +00006514 // LowerFP_TO_INT() can only handle f32 and f64.
6515 if (N->getOperand(0).getValueType() == MVT::ppcf128)
6516 return;
Dale Johannesen37bc85f2009-06-04 20:53:52 +00006517 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands6ed40142008-12-01 11:39:25 +00006518 return;
Chris Lattner57ee7c62007-11-28 18:44:47 +00006519 }
6520}
6521
6522
Chris Lattner4211ca92006-04-14 06:01:58 +00006523//===----------------------------------------------------------------------===//
6524// Other Lowering Code
6525//===----------------------------------------------------------------------===//
6526
Chris Lattner9b577f12005-08-26 21:23:58 +00006527MachineBasicBlock *
Dale Johannesend4eb0522008-08-25 22:34:37 +00006528PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman747e55b2009-02-07 16:15:20 +00006529 bool is64bit, unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006530 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesend4eb0522008-08-25 22:34:37 +00006531 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6532
6533 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6534 MachineFunction *F = BB->getParent();
6535 MachineFunction::iterator It = BB;
6536 ++It;
6537
6538 unsigned dest = MI->getOperand(0).getReg();
6539 unsigned ptrA = MI->getOperand(1).getReg();
6540 unsigned ptrB = MI->getOperand(2).getReg();
6541 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006542 DebugLoc dl = MI->getDebugLoc();
Dale Johannesend4eb0522008-08-25 22:34:37 +00006543
6544 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6545 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6546 F->insert(It, loopMBB);
6547 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006548 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006549 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006550 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006551
6552 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006553 unsigned TmpReg = (!BinOpcode) ? incr :
6554 RegInfo.createVirtualRegister(
Dale Johannesenbc698292008-09-02 20:30:23 +00006555 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6556 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006557
6558 // thisMBB:
6559 // ...
6560 // fallthrough --> loopMBB
6561 BB->addSuccessor(loopMBB);
6562
6563 // loopMBB:
6564 // l[wd]arx dest, ptr
6565 // add r0, dest, incr
6566 // st[wd]cx. r0, ptr
6567 // bne- loopMBB
6568 // fallthrough --> exitMBB
6569 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006570 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesend4eb0522008-08-25 22:34:37 +00006571 .addReg(ptrA).addReg(ptrB);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006572 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006573 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
6574 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesend4eb0522008-08-25 22:34:37 +00006575 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006576 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006577 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesend4eb0522008-08-25 22:34:37 +00006578 BB->addSuccessor(loopMBB);
6579 BB->addSuccessor(exitMBB);
6580
6581 // exitMBB:
6582 // ...
6583 BB = exitMBB;
6584 return BB;
6585}
6586
6587MachineBasicBlock *
Scott Michelcf0da6c2009-02-17 22:15:04 +00006588PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesena32affb2008-08-28 17:53:09 +00006589 MachineBasicBlock *BB,
6590 bool is8bit, // operation
Dan Gohman747e55b2009-02-07 16:15:20 +00006591 unsigned BinOpcode) const {
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006592 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesena32affb2008-08-28 17:53:09 +00006593 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6594 // In 64 bit mode we have to use 64 bits for addresses, even though the
6595 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
6596 // registers without caring whether they're 32 or 64, but here we're
6597 // doing actual arithmetic on the addresses.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006598 bool is64bit = Subtarget.isPPC64();
Hal Finkelf70c41e2013-03-21 23:45:03 +00006599 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesena32affb2008-08-28 17:53:09 +00006600
6601 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6602 MachineFunction *F = BB->getParent();
6603 MachineFunction::iterator It = BB;
6604 ++It;
6605
6606 unsigned dest = MI->getOperand(0).getReg();
6607 unsigned ptrA = MI->getOperand(1).getReg();
6608 unsigned ptrB = MI->getOperand(2).getReg();
6609 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00006610 DebugLoc dl = MI->getDebugLoc();
Dale Johannesena32affb2008-08-28 17:53:09 +00006611
6612 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
6613 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6614 F->insert(It, loopMBB);
6615 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00006616 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006617 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00006618 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006619
6620 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006621 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00006622 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6623 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesena32affb2008-08-28 17:53:09 +00006624 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6625 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6626 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6627 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
6628 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6629 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6630 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6631 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6632 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
6633 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006634 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006635 unsigned Ptr1Reg;
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006636 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesena32affb2008-08-28 17:53:09 +00006637
6638 // thisMBB:
6639 // ...
6640 // fallthrough --> loopMBB
6641 BB->addSuccessor(loopMBB);
6642
6643 // The 4-byte load must be aligned, while a char or short may be
6644 // anywhere in the word. Hence all this nasty bookkeeping code.
6645 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6646 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00006647 // xori shift, shift1, 24 [16]
Dale Johannesena32affb2008-08-28 17:53:09 +00006648 // rlwinm ptr, ptr1, 0, 0, 29
6649 // slw incr2, incr, shift
6650 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6651 // slw mask, mask2, shift
6652 // loopMBB:
Dale Johannesen340d2642008-08-30 00:08:53 +00006653 // lwarx tmpDest, ptr
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006654 // add tmp, tmpDest, incr2
6655 // andc tmp2, tmpDest, mask
Dale Johannesena32affb2008-08-28 17:53:09 +00006656 // and tmp3, tmp, mask
6657 // or tmp4, tmp3, tmp2
Dale Johannesen340d2642008-08-30 00:08:53 +00006658 // stwcx. tmp4, ptr
Dale Johannesena32affb2008-08-28 17:53:09 +00006659 // bne- loopMBB
6660 // fallthrough --> exitMBB
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006661 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006662 if (ptrA != ZeroReg) {
Dale Johannesena32affb2008-08-28 17:53:09 +00006663 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006664 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006665 .addReg(ptrA).addReg(ptrB);
6666 } else {
6667 Ptr1Reg = ptrB;
6668 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006669 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006670 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006671 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006672 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6673 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006674 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006675 .addReg(Ptr1Reg).addImm(0).addImm(61);
6676 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00006677 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006678 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006679 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006680 .addReg(incr).addReg(ShiftReg);
6681 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006682 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesena32affb2008-08-28 17:53:09 +00006683 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00006684 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6685 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesena32affb2008-08-28 17:53:09 +00006686 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00006687 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006688 .addReg(Mask2Reg).addReg(ShiftReg);
6689
6690 BB = loopMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00006691 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006692 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006693 if (BinOpcode)
Dale Johannesene9f623e2009-02-13 02:27:39 +00006694 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006695 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006696 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesenf0a88d62008-08-29 18:29:46 +00006697 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006698 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006699 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006700 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesena32affb2008-08-28 17:53:09 +00006701 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidt3581cd42013-04-02 18:37:08 +00006702 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00006703 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00006704 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelcf0da6c2009-02-17 22:15:04 +00006705 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesena32affb2008-08-28 17:53:09 +00006706 BB->addSuccessor(loopMBB);
6707 BB->addSuccessor(exitMBB);
6708
6709 // exitMBB:
6710 // ...
6711 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00006712 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6713 .addReg(ShiftReg);
Dale Johannesena32affb2008-08-28 17:53:09 +00006714 return BB;
6715}
6716
Hal Finkel756810f2013-03-21 21:37:52 +00006717llvm::MachineBasicBlock*
6718PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6719 MachineBasicBlock *MBB) const {
6720 DebugLoc DL = MI->getDebugLoc();
6721 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6722
6723 MachineFunction *MF = MBB->getParent();
6724 MachineRegisterInfo &MRI = MF->getRegInfo();
6725
6726 const BasicBlock *BB = MBB->getBasicBlock();
6727 MachineFunction::iterator I = MBB;
6728 ++I;
6729
6730 // Memory Reference
6731 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6732 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6733
6734 unsigned DstReg = MI->getOperand(0).getReg();
6735 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6736 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6737 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6738 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6739
6740 MVT PVT = getPointerTy();
6741 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6742 "Invalid Pointer Size!");
6743 // For v = setjmp(buf), we generate
6744 //
6745 // thisMBB:
6746 // SjLjSetup mainMBB
6747 // bl mainMBB
6748 // v_restore = 1
6749 // b sinkMBB
6750 //
6751 // mainMBB:
6752 // buf[LabelOffset] = LR
6753 // v_main = 0
6754 //
6755 // sinkMBB:
6756 // v = phi(main, restore)
6757 //
6758
6759 MachineBasicBlock *thisMBB = MBB;
6760 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6761 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6762 MF->insert(I, mainMBB);
6763 MF->insert(I, sinkMBB);
6764
6765 MachineInstrBuilder MIB;
6766
6767 // Transfer the remainder of BB and its successor edges to sinkMBB.
6768 sinkMBB->splice(sinkMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006769 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Hal Finkel756810f2013-03-21 21:37:52 +00006770 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6771
6772 // Note that the structure of the jmp_buf used here is not compatible
6773 // with that used by libc, and is not designed to be. Specifically, it
6774 // stores only those 'reserved' registers that LLVM does not otherwise
6775 // understand how to spill. Also, by convention, by the time this
6776 // intrinsic is called, Clang has already stored the frame address in the
6777 // first slot of the buffer and stack address in the third. Following the
6778 // X86 target code, we'll store the jump address in the second slot. We also
6779 // need to save the TOC pointer (R2) to handle jumps between shared
6780 // libraries, and that will be stored in the fourth slot. The thread
6781 // identifier (R13) is not affected.
6782
6783 // thisMBB:
6784 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6785 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006786 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006787
6788 // Prepare IP either in reg.
6789 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6790 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6791 unsigned BufReg = MI->getOperand(1).getReg();
6792
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006793 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI()) {
Hal Finkel756810f2013-03-21 21:37:52 +00006794 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6795 .addReg(PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006796 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006797 .addReg(BufReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006798 MIB.setMemRefs(MMOBegin, MMOEnd);
6799 }
6800
Hal Finkelf05d6c72013-07-17 23:50:51 +00006801 // Naked functions never have a base pointer, and so we use r1. For all
6802 // other functions, this decision must be delayed until during PEI.
6803 unsigned BaseReg;
6804 if (MF->getFunction()->getAttributes().hasAttribute(
6805 AttributeSet::FunctionIndex, Attribute::Naked))
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006806 BaseReg = Subtarget.isPPC64() ? PPC::X1 : PPC::R1;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006807 else
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006808 BaseReg = Subtarget.isPPC64() ? PPC::BP8 : PPC::BP;
Hal Finkelf05d6c72013-07-17 23:50:51 +00006809
6810 MIB = BuildMI(*thisMBB, MI, DL,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006811 TII->get(Subtarget.isPPC64() ? PPC::STD : PPC::STW))
Hal Finkelf05d6c72013-07-17 23:50:51 +00006812 .addReg(BaseReg)
6813 .addImm(BPOffset)
6814 .addReg(BufReg);
6815 MIB.setMemRefs(MMOBegin, MMOEnd);
6816
Hal Finkel756810f2013-03-21 21:37:52 +00006817 // Setup
Hal Finkele5680b32013-04-04 22:55:54 +00006818 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Bill Wendling5e7656b2013-06-07 07:55:53 +00006819 const PPCRegisterInfo *TRI =
6820 static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo());
6821 MIB.addRegMask(TRI->getNoPreservedMask());
Hal Finkel756810f2013-03-21 21:37:52 +00006822
6823 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6824
6825 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6826 .addMBB(mainMBB);
6827 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6828
6829 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6830 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6831
6832 // mainMBB:
6833 // mainDstReg = 0
6834 MIB = BuildMI(mainMBB, DL,
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006835 TII->get(Subtarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
Hal Finkel756810f2013-03-21 21:37:52 +00006836
6837 // Store IP
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006838 if (Subtarget.isPPC64()) {
Hal Finkel756810f2013-03-21 21:37:52 +00006839 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6840 .addReg(LabelReg)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006841 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006842 .addReg(BufReg);
6843 } else {
6844 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6845 .addReg(LabelReg)
6846 .addImm(LabelOffset)
6847 .addReg(BufReg);
6848 }
6849
6850 MIB.setMemRefs(MMOBegin, MMOEnd);
6851
6852 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6853 mainMBB->addSuccessor(sinkMBB);
6854
6855 // sinkMBB:
6856 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6857 TII->get(PPC::PHI), DstReg)
6858 .addReg(mainDstReg).addMBB(mainMBB)
6859 .addReg(restoreDstReg).addMBB(thisMBB);
6860
6861 MI->eraseFromParent();
6862 return sinkMBB;
6863}
6864
6865MachineBasicBlock *
6866PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6867 MachineBasicBlock *MBB) const {
6868 DebugLoc DL = MI->getDebugLoc();
6869 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6870
6871 MachineFunction *MF = MBB->getParent();
6872 MachineRegisterInfo &MRI = MF->getRegInfo();
6873
6874 // Memory Reference
6875 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6876 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6877
6878 MVT PVT = getPointerTy();
6879 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6880 "Invalid Pointer Size!");
6881
6882 const TargetRegisterClass *RC =
6883 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6884 unsigned Tmp = MRI.createVirtualRegister(RC);
6885 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6886 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6887 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +00006888 unsigned BP = (PVT == MVT::i64) ? PPC::X30 :
6889 (Subtarget.isSVR4ABI() &&
6890 MF->getTarget().getRelocationModel() == Reloc::PIC_ ?
6891 PPC::R29 : PPC::R30);
Hal Finkel756810f2013-03-21 21:37:52 +00006892
6893 MachineInstrBuilder MIB;
6894
6895 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6896 const int64_t SPOffset = 2 * PVT.getStoreSize();
6897 const int64_t TOCOffset = 3 * PVT.getStoreSize();
Hal Finkelf05d6c72013-07-17 23:50:51 +00006898 const int64_t BPOffset = 4 * PVT.getStoreSize();
Hal Finkel756810f2013-03-21 21:37:52 +00006899
6900 unsigned BufReg = MI->getOperand(0).getReg();
6901
6902 // Reload FP (the jumped-to function may not have had a
6903 // frame pointer, and if so, then its r31 will be restored
6904 // as necessary).
6905 if (PVT == MVT::i64) {
6906 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6907 .addImm(0)
6908 .addReg(BufReg);
6909 } else {
6910 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6911 .addImm(0)
6912 .addReg(BufReg);
6913 }
6914 MIB.setMemRefs(MMOBegin, MMOEnd);
6915
6916 // Reload IP
6917 if (PVT == MVT::i64) {
6918 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006919 .addImm(LabelOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006920 .addReg(BufReg);
6921 } else {
6922 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6923 .addImm(LabelOffset)
6924 .addReg(BufReg);
6925 }
6926 MIB.setMemRefs(MMOBegin, MMOEnd);
6927
6928 // Reload SP
6929 if (PVT == MVT::i64) {
6930 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006931 .addImm(SPOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006932 .addReg(BufReg);
6933 } else {
6934 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6935 .addImm(SPOffset)
6936 .addReg(BufReg);
6937 }
6938 MIB.setMemRefs(MMOBegin, MMOEnd);
6939
Hal Finkelf05d6c72013-07-17 23:50:51 +00006940 // Reload BP
6941 if (PVT == MVT::i64) {
6942 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP)
6943 .addImm(BPOffset)
6944 .addReg(BufReg);
6945 } else {
6946 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP)
6947 .addImm(BPOffset)
6948 .addReg(BufReg);
6949 }
6950 MIB.setMemRefs(MMOBegin, MMOEnd);
Hal Finkel756810f2013-03-21 21:37:52 +00006951
6952 // Reload TOC
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006953 if (PVT == MVT::i64 && Subtarget.isSVR4ABI()) {
Hal Finkel756810f2013-03-21 21:37:52 +00006954 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
Ulrich Weigand9d980cb2013-05-16 17:58:02 +00006955 .addImm(TOCOffset)
Hal Finkel756810f2013-03-21 21:37:52 +00006956 .addReg(BufReg);
6957
6958 MIB.setMemRefs(MMOBegin, MMOEnd);
6959 }
6960
6961 // Jump
6962 BuildMI(*MBB, MI, DL,
6963 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6964 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6965
6966 MI->eraseFromParent();
6967 return MBB;
6968}
6969
Dale Johannesena32affb2008-08-28 17:53:09 +00006970MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00006971PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00006972 MachineBasicBlock *BB) const {
Hal Finkel756810f2013-03-21 21:37:52 +00006973 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6974 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6975 return emitEHSjLjSetJmp(MI, BB);
6976 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6977 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6978 return emitEHSjLjLongJmp(MI, BB);
6979 }
6980
Evan Cheng20350c42006-11-27 23:37:22 +00006981 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng32e376f2008-07-12 02:23:19 +00006982
6983 // To "insert" these instructions we actually have to insert their
6984 // control-flow patterns.
Chris Lattner9b577f12005-08-26 21:23:58 +00006985 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00006986 MachineFunction::iterator It = BB;
Chris Lattner9b577f12005-08-26 21:23:58 +00006987 ++It;
Evan Cheng32e376f2008-07-12 02:23:19 +00006988
Dan Gohman3b460302008-07-07 23:14:23 +00006989 MachineFunction *F = BB->getParent();
Evan Cheng32e376f2008-07-12 02:23:19 +00006990
Eric Christopherb1aaebe2014-06-12 22:38:18 +00006991 if (Subtarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
Hal Finkel940ab932014-02-28 00:27:01 +00006992 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6993 MI->getOpcode() == PPC::SELECT_I4 ||
6994 MI->getOpcode() == PPC::SELECT_I8)) {
Hal Finkeled6a2852013-04-05 23:29:01 +00006995 SmallVector<MachineOperand, 2> Cond;
Hal Finkel940ab932014-02-28 00:27:01 +00006996 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6997 MI->getOpcode() == PPC::SELECT_CC_I8)
6998 Cond.push_back(MI->getOperand(4));
6999 else
7000 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Hal Finkeled6a2852013-04-05 23:29:01 +00007001 Cond.push_back(MI->getOperand(1));
7002
Hal Finkel460e94d2012-06-22 23:10:08 +00007003 DebugLoc dl = MI->getDebugLoc();
Bill Wendling5e7656b2013-06-07 07:55:53 +00007004 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7005 TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(),
7006 Cond, MI->getOperand(2).getReg(),
7007 MI->getOperand(3).getReg());
Hal Finkel460e94d2012-06-22 23:10:08 +00007008 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
7009 MI->getOpcode() == PPC::SELECT_CC_I8 ||
7010 MI->getOpcode() == PPC::SELECT_CC_F4 ||
7011 MI->getOpcode() == PPC::SELECT_CC_F8 ||
Hal Finkel940ab932014-02-28 00:27:01 +00007012 MI->getOpcode() == PPC::SELECT_CC_VRRC ||
7013 MI->getOpcode() == PPC::SELECT_I4 ||
7014 MI->getOpcode() == PPC::SELECT_I8 ||
7015 MI->getOpcode() == PPC::SELECT_F4 ||
7016 MI->getOpcode() == PPC::SELECT_F8 ||
7017 MI->getOpcode() == PPC::SELECT_VRRC) {
Evan Cheng32e376f2008-07-12 02:23:19 +00007018 // The incoming instruction knows the destination vreg to set, the
7019 // condition code register to branch on, the true/false values to
7020 // select between, and a branch opcode to use.
7021
7022 // thisMBB:
7023 // ...
7024 // TrueVal = ...
7025 // cmpTY ccX, r1, r2
7026 // bCC copy1MBB
7027 // fallthrough --> copy0MBB
7028 MachineBasicBlock *thisMBB = BB;
7029 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7030 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007031 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00007032 F->insert(It, copy0MBB);
7033 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007034
7035 // Transfer the remainder of BB and its successor edges to sinkMBB.
7036 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007037 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007038 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7039
Evan Cheng32e376f2008-07-12 02:23:19 +00007040 // Next, add the true and fallthrough blocks as its successors.
7041 BB->addSuccessor(copy0MBB);
7042 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007043
Hal Finkel940ab932014-02-28 00:27:01 +00007044 if (MI->getOpcode() == PPC::SELECT_I4 ||
7045 MI->getOpcode() == PPC::SELECT_I8 ||
7046 MI->getOpcode() == PPC::SELECT_F4 ||
7047 MI->getOpcode() == PPC::SELECT_F8 ||
7048 MI->getOpcode() == PPC::SELECT_VRRC) {
7049 BuildMI(BB, dl, TII->get(PPC::BC))
7050 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
7051 } else {
7052 unsigned SelectPred = MI->getOperand(4).getImm();
7053 BuildMI(BB, dl, TII->get(PPC::BCC))
7054 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
7055 }
Dan Gohman34396292010-07-06 20:24:04 +00007056
Evan Cheng32e376f2008-07-12 02:23:19 +00007057 // copy0MBB:
7058 // %FalseValue = ...
7059 // # fallthrough to sinkMBB
7060 BB = copy0MBB;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007061
Evan Cheng32e376f2008-07-12 02:23:19 +00007062 // Update machine-CFG edges
7063 BB->addSuccessor(sinkMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007064
Evan Cheng32e376f2008-07-12 02:23:19 +00007065 // sinkMBB:
7066 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7067 // ...
7068 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007069 BuildMI(*BB, BB->begin(), dl,
7070 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng32e376f2008-07-12 02:23:19 +00007071 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
7072 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7073 }
Dale Johannesena32affb2008-08-28 17:53:09 +00007074 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
7075 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
7076 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
7077 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007078 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
7079 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
7080 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
7081 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007082
7083 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
7084 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
7085 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
7086 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007087 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
7088 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
7089 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
7090 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007091
7092 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
7093 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
7094 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
7095 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007096 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
7097 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
7098 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
7099 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007100
7101 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
7102 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
7103 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
7104 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007105 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
7106 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
7107 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
7108 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007109
7110 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00007111 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::NAND);
Dale Johannesena32affb2008-08-28 17:53:09 +00007112 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00007113 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007114 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00007115 BB = EmitAtomicBinary(MI, BB, false, PPC::NAND);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007116 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Ulrich Weigand862d8b82014-07-08 16:16:02 +00007117 BB = EmitAtomicBinary(MI, BB, true, PPC::NAND8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007118
7119 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
7120 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
7121 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
7122 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesend4eb0522008-08-25 22:34:37 +00007123 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
7124 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
7125 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
7126 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesena32affb2008-08-28 17:53:09 +00007127
Dale Johannesenf0a88d62008-08-29 18:29:46 +00007128 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
7129 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
7130 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
7131 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
7132 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
7133 BB = EmitAtomicBinary(MI, BB, false, 0);
7134 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
7135 BB = EmitAtomicBinary(MI, BB, true, 0);
7136
Evan Cheng32e376f2008-07-12 02:23:19 +00007137 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
7138 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
7139 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
7140
7141 unsigned dest = MI->getOperand(0).getReg();
7142 unsigned ptrA = MI->getOperand(1).getReg();
7143 unsigned ptrB = MI->getOperand(2).getReg();
7144 unsigned oldval = MI->getOperand(3).getReg();
7145 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00007146 DebugLoc dl = MI->getDebugLoc();
Evan Cheng32e376f2008-07-12 02:23:19 +00007147
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007148 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
7149 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
7150 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007151 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007152 F->insert(It, loop1MBB);
7153 F->insert(It, loop2MBB);
7154 F->insert(It, midMBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007155 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007156 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007157 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007158 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007159
7160 // thisMBB:
7161 // ...
7162 // fallthrough --> loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007163 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007164
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007165 // loop1MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00007166 // l[wd]arx dest, ptr
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007167 // cmp[wd] dest, oldval
7168 // bne- midMBB
7169 // loop2MBB:
Evan Cheng32e376f2008-07-12 02:23:19 +00007170 // st[wd]cx. newval, ptr
7171 // bne- loopMBB
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007172 // b exitBB
7173 // midMBB:
7174 // st[wd]cx. dest, ptr
7175 // exitBB:
7176 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007177 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng32e376f2008-07-12 02:23:19 +00007178 .addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007179 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng32e376f2008-07-12 02:23:19 +00007180 .addReg(oldval).addReg(dest);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007181 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007182 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
7183 BB->addSuccessor(loop2MBB);
7184 BB->addSuccessor(midMBB);
7185
7186 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007187 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng32e376f2008-07-12 02:23:19 +00007188 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007189 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007190 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007191 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007192 BB->addSuccessor(loop1MBB);
Evan Cheng32e376f2008-07-12 02:23:19 +00007193 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007194
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007195 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007196 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen166d6cb2008-08-25 18:53:26 +00007197 .addReg(dest).addReg(ptrA).addReg(ptrB);
7198 BB->addSuccessor(exitMBB);
7199
Evan Cheng32e376f2008-07-12 02:23:19 +00007200 // exitMBB:
7201 // ...
7202 BB = exitMBB;
Dale Johannesen340d2642008-08-30 00:08:53 +00007203 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
7204 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
7205 // We must use 64-bit registers for addresses when targeting 64-bit,
7206 // since we're actually doing arithmetic on them. Other registers
7207 // can be 32-bit.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007208 bool is64bit = Subtarget.isPPC64();
Dale Johannesen340d2642008-08-30 00:08:53 +00007209 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
7210
7211 unsigned dest = MI->getOperand(0).getReg();
7212 unsigned ptrA = MI->getOperand(1).getReg();
7213 unsigned ptrB = MI->getOperand(2).getReg();
7214 unsigned oldval = MI->getOperand(3).getReg();
7215 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesene9f623e2009-02-13 02:27:39 +00007216 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen340d2642008-08-30 00:08:53 +00007217
7218 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
7219 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
7220 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
7221 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
7222 F->insert(It, loop1MBB);
7223 F->insert(It, loop2MBB);
7224 F->insert(It, midMBB);
7225 F->insert(It, exitMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007226 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007227 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007228 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen340d2642008-08-30 00:08:53 +00007229
7230 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelcf0da6c2009-02-17 22:15:04 +00007231 const TargetRegisterClass *RC =
Dale Johannesenbc698292008-09-02 20:30:23 +00007232 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
7233 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen340d2642008-08-30 00:08:53 +00007234 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
7235 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
7236 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
7237 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
7238 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
7239 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
7240 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
7241 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
7242 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
7243 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
7244 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
7245 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
7246 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
7247 unsigned Ptr1Reg;
7248 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkelf70c41e2013-03-21 23:45:03 +00007249 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen340d2642008-08-30 00:08:53 +00007250 // thisMBB:
7251 // ...
7252 // fallthrough --> loopMBB
7253 BB->addSuccessor(loop1MBB);
7254
7255 // The 4-byte load must be aligned, while a char or short may be
7256 // anywhere in the word. Hence all this nasty bookkeeping code.
7257 // add ptr1, ptrA, ptrB [copy if ptrA==0]
7258 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesenbc698292008-09-02 20:30:23 +00007259 // xori shift, shift1, 24 [16]
Dale Johannesen340d2642008-08-30 00:08:53 +00007260 // rlwinm ptr, ptr1, 0, 0, 29
7261 // slw newval2, newval, shift
7262 // slw oldval2, oldval,shift
7263 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
7264 // slw mask, mask2, shift
7265 // and newval3, newval2, mask
7266 // and oldval3, oldval2, mask
7267 // loop1MBB:
7268 // lwarx tmpDest, ptr
7269 // and tmp, tmpDest, mask
7270 // cmpw tmp, oldval3
7271 // bne- midMBB
7272 // loop2MBB:
7273 // andc tmp2, tmpDest, mask
7274 // or tmp4, tmp2, newval3
7275 // stwcx. tmp4, ptr
7276 // bne- loop1MBB
7277 // b exitBB
7278 // midMBB:
7279 // stwcx. tmpDest, ptr
7280 // exitBB:
7281 // srw dest, tmpDest, shift
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007282 if (ptrA != ZeroReg) {
Dale Johannesen340d2642008-08-30 00:08:53 +00007283 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007284 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007285 .addReg(ptrA).addReg(ptrB);
7286 } else {
7287 Ptr1Reg = ptrB;
7288 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00007289 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007290 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007291 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007292 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
7293 if (is64bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00007294 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007295 .addReg(Ptr1Reg).addImm(0).addImm(61);
7296 else
Dale Johannesene9f623e2009-02-13 02:27:39 +00007297 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007298 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007299 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007300 .addReg(newval).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007301 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007302 .addReg(oldval).addReg(ShiftReg);
7303 if (is8bit)
Dale Johannesene9f623e2009-02-13 02:27:39 +00007304 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen340d2642008-08-30 00:08:53 +00007305 else {
Dale Johannesene9f623e2009-02-13 02:27:39 +00007306 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
7307 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
7308 .addReg(Mask3Reg).addImm(65535);
Dale Johannesen340d2642008-08-30 00:08:53 +00007309 }
Dale Johannesene9f623e2009-02-13 02:27:39 +00007310 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007311 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007312 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007313 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007314 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesen340d2642008-08-30 00:08:53 +00007315 .addReg(OldVal2Reg).addReg(MaskReg);
7316
7317 BB = loop1MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007318 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007319 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007320 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
7321 .addReg(TmpDestReg).addReg(MaskReg);
7322 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesen340d2642008-08-30 00:08:53 +00007323 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007324 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00007325 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
7326 BB->addSuccessor(loop2MBB);
7327 BB->addSuccessor(midMBB);
7328
7329 BB = loop2MBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007330 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
7331 .addReg(TmpDestReg).addReg(MaskReg);
7332 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
7333 .addReg(Tmp2Reg).addReg(NewVal3Reg);
7334 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007335 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007336 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen340d2642008-08-30 00:08:53 +00007337 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesene9f623e2009-02-13 02:27:39 +00007338 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen340d2642008-08-30 00:08:53 +00007339 BB->addSuccessor(loop1MBB);
7340 BB->addSuccessor(exitMBB);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007341
Dale Johannesen340d2642008-08-30 00:08:53 +00007342 BB = midMBB;
Dale Johannesene9f623e2009-02-13 02:27:39 +00007343 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen7067bff2011-04-04 17:07:06 +00007344 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen340d2642008-08-30 00:08:53 +00007345 BB->addSuccessor(exitMBB);
7346
7347 // exitMBB:
7348 // ...
7349 BB = exitMBB;
Jakob Stoklund Olesen13ce2362011-04-04 17:57:29 +00007350 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
7351 .addReg(ShiftReg);
Ulrich Weigand874fc622013-03-26 10:56:22 +00007352 } else if (MI->getOpcode() == PPC::FADDrtz) {
7353 // This pseudo performs an FADD with rounding mode temporarily forced
7354 // to round-to-zero. We emit this via custom inserter since the FPSCR
7355 // is not modeled at the SelectionDAG level.
7356 unsigned Dest = MI->getOperand(0).getReg();
7357 unsigned Src1 = MI->getOperand(1).getReg();
7358 unsigned Src2 = MI->getOperand(2).getReg();
7359 DebugLoc dl = MI->getDebugLoc();
7360
7361 MachineRegisterInfo &RegInfo = F->getRegInfo();
7362 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
7363
7364 // Save FPSCR value.
7365 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
7366
7367 // Set rounding mode to round-to-zero.
7368 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
7369 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
7370
7371 // Perform addition.
7372 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
7373
7374 // Restore FPSCR value.
7375 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkel940ab932014-02-28 00:27:01 +00007376 } else if (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
7377 MI->getOpcode() == PPC::ANDIo_1_GT_BIT ||
7378 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
7379 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) {
7380 unsigned Opcode = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8 ||
7381 MI->getOpcode() == PPC::ANDIo_1_GT_BIT8) ?
7382 PPC::ANDIo8 : PPC::ANDIo;
7383 bool isEQ = (MI->getOpcode() == PPC::ANDIo_1_EQ_BIT ||
7384 MI->getOpcode() == PPC::ANDIo_1_EQ_BIT8);
7385
7386 MachineRegisterInfo &RegInfo = F->getRegInfo();
7387 unsigned Dest = RegInfo.createVirtualRegister(Opcode == PPC::ANDIo ?
7388 &PPC::GPRCRegClass :
7389 &PPC::G8RCRegClass);
7390
7391 DebugLoc dl = MI->getDebugLoc();
7392 BuildMI(*BB, MI, dl, TII->get(Opcode), Dest)
7393 .addReg(MI->getOperand(1).getReg()).addImm(1);
7394 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY),
7395 MI->getOperand(0).getReg())
7396 .addReg(isEQ ? PPC::CR0EQ : PPC::CR0GT);
Dale Johannesen340d2642008-08-30 00:08:53 +00007397 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007398 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng32e376f2008-07-12 02:23:19 +00007399 }
Chris Lattner9b577f12005-08-26 21:23:58 +00007400
Dan Gohman34396292010-07-06 20:24:04 +00007401 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner9b577f12005-08-26 21:23:58 +00007402 return BB;
7403}
7404
Chris Lattner4211ca92006-04-14 06:01:58 +00007405//===----------------------------------------------------------------------===//
7406// Target Optimization Hooks
7407//===----------------------------------------------------------------------===//
7408
Hal Finkelb0c810f2013-04-03 17:44:56 +00007409SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
7410 DAGCombinerInfo &DCI) const {
Hal Finkel2e103312013-04-03 04:01:11 +00007411 if (DCI.isAfterLegalizeVectorOps())
7412 return SDValue();
7413
Hal Finkelb0c810f2013-04-03 17:44:56 +00007414 EVT VT = Op.getValueType();
7415
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007416 if ((VT == MVT::f32 && Subtarget.hasFRES()) ||
7417 (VT == MVT::f64 && Subtarget.hasFRE()) ||
7418 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
7419 (VT == MVT::v2f64 && Subtarget.hasVSX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00007420
7421 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
7422 // For the reciprocal, we need to find the zero of the function:
7423 // F(X) = A X - 1 [which has a zero at X = 1/A]
7424 // =>
7425 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
7426 // does not require additional intermediate precision]
7427
7428 // Convergence is quadratic, so we essentially double the number of digits
7429 // correct after every iteration. The minimum architected relative
7430 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
7431 // 23 digits and double has 52 digits.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007432 int Iterations = Subtarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00007433 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00007434 ++Iterations;
7435
7436 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007437 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007438
7439 SDValue FPOne =
Hal Finkelb0c810f2013-04-03 17:44:56 +00007440 DAG.getConstantFP(1.0, VT.getScalarType());
7441 if (VT.isVector()) {
7442 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00007443 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00007444 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel2e103312013-04-03 04:01:11 +00007445 FPOne, FPOne, FPOne, FPOne);
7446 }
7447
Hal Finkelb0c810f2013-04-03 17:44:56 +00007448 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007449 DCI.AddToWorklist(Est.getNode());
7450
7451 // Newton iterations: Est = Est + Est (1 - Arg * Est)
7452 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007453 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00007454 DCI.AddToWorklist(NewEst.getNode());
7455
Hal Finkelb0c810f2013-04-03 17:44:56 +00007456 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007457 DCI.AddToWorklist(NewEst.getNode());
7458
Hal Finkelb0c810f2013-04-03 17:44:56 +00007459 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007460 DCI.AddToWorklist(NewEst.getNode());
7461
Hal Finkelb0c810f2013-04-03 17:44:56 +00007462 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007463 DCI.AddToWorklist(Est.getNode());
7464 }
7465
7466 return Est;
7467 }
7468
7469 return SDValue();
7470}
7471
Hal Finkelb0c810f2013-04-03 17:44:56 +00007472SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel2e103312013-04-03 04:01:11 +00007473 DAGCombinerInfo &DCI) const {
7474 if (DCI.isAfterLegalizeVectorOps())
7475 return SDValue();
7476
Hal Finkelb0c810f2013-04-03 17:44:56 +00007477 EVT VT = Op.getValueType();
7478
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007479 if ((VT == MVT::f32 && Subtarget.hasFRSQRTES()) ||
7480 (VT == MVT::f64 && Subtarget.hasFRSQRTE()) ||
7481 (VT == MVT::v4f32 && Subtarget.hasAltivec()) ||
7482 (VT == MVT::v2f64 && Subtarget.hasVSX())) {
Hal Finkel2e103312013-04-03 04:01:11 +00007483
7484 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
7485 // For the reciprocal sqrt, we need to find the zero of the function:
7486 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
7487 // =>
7488 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
7489 // As a result, we precompute A/2 prior to the iteration loop.
7490
7491 // Convergence is quadratic, so we essentially double the number of digits
7492 // correct after every iteration. The minimum architected relative
7493 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
7494 // 23 digits and double has 52 digits.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007495 int Iterations = Subtarget.hasRecipPrec() ? 1 : 3;
Hal Finkelb0c810f2013-04-03 17:44:56 +00007496 if (VT.getScalarType() == MVT::f64)
Hal Finkel2e103312013-04-03 04:01:11 +00007497 ++Iterations;
7498
7499 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007500 SDLoc dl(Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007501
Hal Finkelb0c810f2013-04-03 17:44:56 +00007502 SDValue FPThreeHalves =
7503 DAG.getConstantFP(1.5, VT.getScalarType());
7504 if (VT.isVector()) {
7505 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel2e103312013-04-03 04:01:11 +00007506 "Unknown vector type");
Hal Finkelb0c810f2013-04-03 17:44:56 +00007507 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
7508 FPThreeHalves, FPThreeHalves,
7509 FPThreeHalves, FPThreeHalves);
Hal Finkel2e103312013-04-03 04:01:11 +00007510 }
7511
Hal Finkelb0c810f2013-04-03 17:44:56 +00007512 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007513 DCI.AddToWorklist(Est.getNode());
7514
7515 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
7516 // this entire sequence requires only one FP constant.
Hal Finkelb0c810f2013-04-03 17:44:56 +00007517 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007518 DCI.AddToWorklist(HalfArg.getNode());
7519
Hal Finkelb0c810f2013-04-03 17:44:56 +00007520 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel2e103312013-04-03 04:01:11 +00007521 DCI.AddToWorklist(HalfArg.getNode());
7522
7523 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
7524 for (int i = 0; i < Iterations; ++i) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00007525 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel2e103312013-04-03 04:01:11 +00007526 DCI.AddToWorklist(NewEst.getNode());
7527
Hal Finkelb0c810f2013-04-03 17:44:56 +00007528 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007529 DCI.AddToWorklist(NewEst.getNode());
7530
Hal Finkelb0c810f2013-04-03 17:44:56 +00007531 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007532 DCI.AddToWorklist(NewEst.getNode());
7533
Hal Finkelb0c810f2013-04-03 17:44:56 +00007534 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel2e103312013-04-03 04:01:11 +00007535 DCI.AddToWorklist(Est.getNode());
7536 }
7537
7538 return Est;
7539 }
7540
7541 return SDValue();
7542}
7543
Hal Finkel3604bf72014-08-01 01:02:01 +00007544static bool isConsecutiveLSLoc(SDValue Loc, EVT VT, LSBaseSDNode *Base,
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007545 unsigned Bytes, int Dist,
7546 SelectionDAG &DAG) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007547 if (VT.getSizeInBits() / 8 != Bytes)
7548 return false;
7549
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007550 SDValue BaseLoc = Base->getBasePtr();
7551 if (Loc.getOpcode() == ISD::FrameIndex) {
7552 if (BaseLoc.getOpcode() != ISD::FrameIndex)
7553 return false;
7554 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7555 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
7556 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
7557 int FS = MFI->getObjectSize(FI);
7558 int BFS = MFI->getObjectSize(BFI);
7559 if (FS != BFS || FS != (int)Bytes) return false;
7560 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
7561 }
7562
7563 // Handle X+C
7564 if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
7565 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
7566 return true;
7567
7568 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Craig Topper062a2ba2014-04-25 05:30:21 +00007569 const GlobalValue *GV1 = nullptr;
7570 const GlobalValue *GV2 = nullptr;
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007571 int64_t Offset1 = 0;
7572 int64_t Offset2 = 0;
7573 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
7574 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
7575 if (isGA1 && isGA2 && GV1 == GV2)
7576 return Offset1 == (Offset2 + Dist*Bytes);
7577 return false;
7578}
7579
Hal Finkel3604bf72014-08-01 01:02:01 +00007580// Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does
7581// not enforce equality of the chain operands.
7582static bool isConsecutiveLS(SDNode *N, LSBaseSDNode *Base,
7583 unsigned Bytes, int Dist,
7584 SelectionDAG &DAG) {
7585 if (LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(N)) {
7586 EVT VT = LS->getMemoryVT();
7587 SDValue Loc = LS->getBasePtr();
7588 return isConsecutiveLSLoc(Loc, VT, Base, Bytes, Dist, DAG);
7589 }
7590
7591 if (N->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
7592 EVT VT;
7593 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
7594 default: return false;
7595 case Intrinsic::ppc_altivec_lvx:
7596 case Intrinsic::ppc_altivec_lvxl:
7597 VT = MVT::v4i32;
7598 break;
7599 case Intrinsic::ppc_altivec_lvebx:
7600 VT = MVT::i8;
7601 break;
7602 case Intrinsic::ppc_altivec_lvehx:
7603 VT = MVT::i16;
7604 break;
7605 case Intrinsic::ppc_altivec_lvewx:
7606 VT = MVT::i32;
7607 break;
7608 }
7609
7610 return isConsecutiveLSLoc(N->getOperand(2), VT, Base, Bytes, Dist, DAG);
7611 }
7612
7613 if (N->getOpcode() == ISD::INTRINSIC_VOID) {
7614 EVT VT;
7615 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
7616 default: return false;
7617 case Intrinsic::ppc_altivec_stvx:
7618 case Intrinsic::ppc_altivec_stvxl:
7619 VT = MVT::v4i32;
7620 break;
7621 case Intrinsic::ppc_altivec_stvebx:
7622 VT = MVT::i8;
7623 break;
7624 case Intrinsic::ppc_altivec_stvehx:
7625 VT = MVT::i16;
7626 break;
7627 case Intrinsic::ppc_altivec_stvewx:
7628 VT = MVT::i32;
7629 break;
7630 }
7631
7632 return isConsecutiveLSLoc(N->getOperand(3), VT, Base, Bytes, Dist, DAG);
7633 }
7634
7635 return false;
7636}
7637
Hal Finkel7d8a6912013-05-26 18:08:30 +00007638// Return true is there is a nearyby consecutive load to the one provided
7639// (regardless of alignment). We search up and down the chain, looking though
Matt Arsenault57e74d22014-07-29 00:02:40 +00007640// token factors and other loads (but nothing else). As a result, a true result
7641// indicates that it is safe to create a new consecutive load adjacent to the
7642// load provided.
Hal Finkel7d8a6912013-05-26 18:08:30 +00007643static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) {
7644 SDValue Chain = LD->getChain();
7645 EVT VT = LD->getMemoryVT();
7646
7647 SmallSet<SDNode *, 16> LoadRoots;
7648 SmallVector<SDNode *, 8> Queue(1, Chain.getNode());
7649 SmallSet<SDNode *, 16> Visited;
7650
7651 // First, search up the chain, branching to follow all token-factor operands.
7652 // If we find a consecutive load, then we're done, otherwise, record all
7653 // nodes just above the top-level loads and token factors.
7654 while (!Queue.empty()) {
7655 SDNode *ChainNext = Queue.pop_back_val();
7656 if (!Visited.insert(ChainNext))
7657 continue;
7658
Hal Finkel3604bf72014-08-01 01:02:01 +00007659 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(ChainNext)) {
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007660 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007661 return true;
7662
7663 if (!Visited.count(ChainLD->getChain().getNode()))
7664 Queue.push_back(ChainLD->getChain().getNode());
7665 } else if (ChainNext->getOpcode() == ISD::TokenFactor) {
Craig Topper66e588b2014-06-29 00:40:57 +00007666 for (const SDUse &O : ChainNext->ops())
7667 if (!Visited.count(O.getNode()))
7668 Queue.push_back(O.getNode());
Hal Finkel7d8a6912013-05-26 18:08:30 +00007669 } else
7670 LoadRoots.insert(ChainNext);
7671 }
7672
7673 // Second, search down the chain, starting from the top-level nodes recorded
7674 // in the first phase. These top-level nodes are the nodes just above all
7675 // loads and token factors. Starting with their uses, recursively look though
7676 // all loads (just the chain uses) and token factors to find a consecutive
7677 // load.
7678 Visited.clear();
7679 Queue.clear();
7680
7681 for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(),
7682 IE = LoadRoots.end(); I != IE; ++I) {
7683 Queue.push_back(*I);
7684
7685 while (!Queue.empty()) {
7686 SDNode *LoadRoot = Queue.pop_back_val();
7687 if (!Visited.insert(LoadRoot))
7688 continue;
7689
Hal Finkel3604bf72014-08-01 01:02:01 +00007690 if (MemSDNode *ChainLD = dyn_cast<MemSDNode>(LoadRoot))
Hal Finkel8ebfe6c2013-05-27 02:06:39 +00007691 if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG))
Hal Finkel7d8a6912013-05-26 18:08:30 +00007692 return true;
7693
7694 for (SDNode::use_iterator UI = LoadRoot->use_begin(),
7695 UE = LoadRoot->use_end(); UI != UE; ++UI)
Hal Finkel3604bf72014-08-01 01:02:01 +00007696 if (((isa<MemSDNode>(*UI) &&
7697 cast<MemSDNode>(*UI)->getChain().getNode() == LoadRoot) ||
Hal Finkel7d8a6912013-05-26 18:08:30 +00007698 UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI))
7699 Queue.push_back(*UI);
7700 }
7701 }
7702
7703 return false;
7704}
7705
Hal Finkel940ab932014-02-28 00:27:01 +00007706SDValue PPCTargetLowering::DAGCombineTruncBoolExt(SDNode *N,
7707 DAGCombinerInfo &DCI) const {
7708 SelectionDAG &DAG = DCI.DAG;
7709 SDLoc dl(N);
7710
Eric Christopherb1aaebe2014-06-12 22:38:18 +00007711 assert(Subtarget.useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00007712 "Expecting to be tracking CR bits");
7713 // If we're tracking CR bits, we need to be careful that we don't have:
7714 // trunc(binary-ops(zext(x), zext(y)))
7715 // or
7716 // trunc(binary-ops(binary-ops(zext(x), zext(y)), ...)
7717 // such that we're unnecessarily moving things into GPRs when it would be
7718 // better to keep them in CR bits.
7719
7720 // Note that trunc here can be an actual i1 trunc, or can be the effective
7721 // truncation that comes from a setcc or select_cc.
7722 if (N->getOpcode() == ISD::TRUNCATE &&
7723 N->getValueType(0) != MVT::i1)
7724 return SDValue();
7725
7726 if (N->getOperand(0).getValueType() != MVT::i32 &&
7727 N->getOperand(0).getValueType() != MVT::i64)
7728 return SDValue();
7729
7730 if (N->getOpcode() == ISD::SETCC ||
7731 N->getOpcode() == ISD::SELECT_CC) {
7732 // If we're looking at a comparison, then we need to make sure that the
7733 // high bits (all except for the first) don't matter the result.
7734 ISD::CondCode CC =
7735 cast<CondCodeSDNode>(N->getOperand(
7736 N->getOpcode() == ISD::SETCC ? 2 : 4))->get();
7737 unsigned OpBits = N->getOperand(0).getValueSizeInBits();
7738
7739 if (ISD::isSignedIntSetCC(CC)) {
7740 if (DAG.ComputeNumSignBits(N->getOperand(0)) != OpBits ||
7741 DAG.ComputeNumSignBits(N->getOperand(1)) != OpBits)
7742 return SDValue();
7743 } else if (ISD::isUnsignedIntSetCC(CC)) {
7744 if (!DAG.MaskedValueIsZero(N->getOperand(0),
7745 APInt::getHighBitsSet(OpBits, OpBits-1)) ||
7746 !DAG.MaskedValueIsZero(N->getOperand(1),
7747 APInt::getHighBitsSet(OpBits, OpBits-1)))
7748 return SDValue();
7749 } else {
7750 // This is neither a signed nor an unsigned comparison, just make sure
7751 // that the high bits are equal.
7752 APInt Op1Zero, Op1One;
7753 APInt Op2Zero, Op2One;
Jay Foada0653a32014-05-14 21:14:37 +00007754 DAG.computeKnownBits(N->getOperand(0), Op1Zero, Op1One);
7755 DAG.computeKnownBits(N->getOperand(1), Op2Zero, Op2One);
Hal Finkel940ab932014-02-28 00:27:01 +00007756
7757 // We don't really care about what is known about the first bit (if
7758 // anything), so clear it in all masks prior to comparing them.
7759 Op1Zero.clearBit(0); Op1One.clearBit(0);
7760 Op2Zero.clearBit(0); Op2One.clearBit(0);
7761
7762 if (Op1Zero != Op2Zero || Op1One != Op2One)
7763 return SDValue();
7764 }
7765 }
7766
7767 // We now know that the higher-order bits are irrelevant, we just need to
7768 // make sure that all of the intermediate operations are bit operations, and
7769 // all inputs are extensions.
7770 if (N->getOperand(0).getOpcode() != ISD::AND &&
7771 N->getOperand(0).getOpcode() != ISD::OR &&
7772 N->getOperand(0).getOpcode() != ISD::XOR &&
7773 N->getOperand(0).getOpcode() != ISD::SELECT &&
7774 N->getOperand(0).getOpcode() != ISD::SELECT_CC &&
7775 N->getOperand(0).getOpcode() != ISD::TRUNCATE &&
7776 N->getOperand(0).getOpcode() != ISD::SIGN_EXTEND &&
7777 N->getOperand(0).getOpcode() != ISD::ZERO_EXTEND &&
7778 N->getOperand(0).getOpcode() != ISD::ANY_EXTEND)
7779 return SDValue();
7780
7781 if ((N->getOpcode() == ISD::SETCC || N->getOpcode() == ISD::SELECT_CC) &&
7782 N->getOperand(1).getOpcode() != ISD::AND &&
7783 N->getOperand(1).getOpcode() != ISD::OR &&
7784 N->getOperand(1).getOpcode() != ISD::XOR &&
7785 N->getOperand(1).getOpcode() != ISD::SELECT &&
7786 N->getOperand(1).getOpcode() != ISD::SELECT_CC &&
7787 N->getOperand(1).getOpcode() != ISD::TRUNCATE &&
7788 N->getOperand(1).getOpcode() != ISD::SIGN_EXTEND &&
7789 N->getOperand(1).getOpcode() != ISD::ZERO_EXTEND &&
7790 N->getOperand(1).getOpcode() != ISD::ANY_EXTEND)
7791 return SDValue();
7792
7793 SmallVector<SDValue, 4> Inputs;
7794 SmallVector<SDValue, 8> BinOps, PromOps;
7795 SmallPtrSet<SDNode *, 16> Visited;
7796
7797 for (unsigned i = 0; i < 2; ++i) {
7798 if (((N->getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7799 N->getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7800 N->getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7801 N->getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7802 isa<ConstantSDNode>(N->getOperand(i)))
7803 Inputs.push_back(N->getOperand(i));
7804 else
7805 BinOps.push_back(N->getOperand(i));
7806
7807 if (N->getOpcode() == ISD::TRUNCATE)
7808 break;
7809 }
7810
7811 // Visit all inputs, collect all binary operations (and, or, xor and
7812 // select) that are all fed by extensions.
7813 while (!BinOps.empty()) {
7814 SDValue BinOp = BinOps.back();
7815 BinOps.pop_back();
7816
7817 if (!Visited.insert(BinOp.getNode()))
7818 continue;
7819
7820 PromOps.push_back(BinOp);
7821
7822 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
7823 // The condition of the select is not promoted.
7824 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
7825 continue;
7826 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
7827 continue;
7828
7829 if (((BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7830 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7831 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) &&
7832 BinOp.getOperand(i).getOperand(0).getValueType() == MVT::i1) ||
7833 isa<ConstantSDNode>(BinOp.getOperand(i))) {
7834 Inputs.push_back(BinOp.getOperand(i));
7835 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
7836 BinOp.getOperand(i).getOpcode() == ISD::OR ||
7837 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
7838 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
7839 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC ||
7840 BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
7841 BinOp.getOperand(i).getOpcode() == ISD::SIGN_EXTEND ||
7842 BinOp.getOperand(i).getOpcode() == ISD::ZERO_EXTEND ||
7843 BinOp.getOperand(i).getOpcode() == ISD::ANY_EXTEND) {
7844 BinOps.push_back(BinOp.getOperand(i));
7845 } else {
7846 // We have an input that is not an extension or another binary
7847 // operation; we'll abort this transformation.
7848 return SDValue();
7849 }
7850 }
7851 }
7852
7853 // Make sure that this is a self-contained cluster of operations (which
7854 // is not quite the same thing as saying that everything has only one
7855 // use).
7856 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7857 if (isa<ConstantSDNode>(Inputs[i]))
7858 continue;
7859
7860 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
7861 UE = Inputs[i].getNode()->use_end();
7862 UI != UE; ++UI) {
7863 SDNode *User = *UI;
7864 if (User != N && !Visited.count(User))
7865 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007866
7867 // Make sure that we're not going to promote the non-output-value
7868 // operand(s) or SELECT or SELECT_CC.
7869 // FIXME: Although we could sometimes handle this, and it does occur in
7870 // practice that one of the condition inputs to the select is also one of
7871 // the outputs, we currently can't deal with this.
7872 if (User->getOpcode() == ISD::SELECT) {
7873 if (User->getOperand(0) == Inputs[i])
7874 return SDValue();
7875 } else if (User->getOpcode() == ISD::SELECT_CC) {
7876 if (User->getOperand(0) == Inputs[i] ||
7877 User->getOperand(1) == Inputs[i])
7878 return SDValue();
7879 }
Hal Finkel940ab932014-02-28 00:27:01 +00007880 }
7881 }
7882
7883 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
7884 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
7885 UE = PromOps[i].getNode()->use_end();
7886 UI != UE; ++UI) {
7887 SDNode *User = *UI;
7888 if (User != N && !Visited.count(User))
7889 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00007890
7891 // Make sure that we're not going to promote the non-output-value
7892 // operand(s) or SELECT or SELECT_CC.
7893 // FIXME: Although we could sometimes handle this, and it does occur in
7894 // practice that one of the condition inputs to the select is also one of
7895 // the outputs, we currently can't deal with this.
7896 if (User->getOpcode() == ISD::SELECT) {
7897 if (User->getOperand(0) == PromOps[i])
7898 return SDValue();
7899 } else if (User->getOpcode() == ISD::SELECT_CC) {
7900 if (User->getOperand(0) == PromOps[i] ||
7901 User->getOperand(1) == PromOps[i])
7902 return SDValue();
7903 }
Hal Finkel940ab932014-02-28 00:27:01 +00007904 }
7905 }
7906
7907 // Replace all inputs with the extension operand.
7908 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
7909 // Constants may have users outside the cluster of to-be-promoted nodes,
7910 // and so we need to replace those as we do the promotions.
7911 if (isa<ConstantSDNode>(Inputs[i]))
7912 continue;
7913 else
7914 DAG.ReplaceAllUsesOfValueWith(Inputs[i], Inputs[i].getOperand(0));
7915 }
7916
7917 // Replace all operations (these are all the same, but have a different
7918 // (i1) return type). DAG.getNode will validate that the types of
7919 // a binary operator match, so go through the list in reverse so that
7920 // we've likely promoted both operands first. Any intermediate truncations or
7921 // extensions disappear.
7922 while (!PromOps.empty()) {
7923 SDValue PromOp = PromOps.back();
7924 PromOps.pop_back();
7925
7926 if (PromOp.getOpcode() == ISD::TRUNCATE ||
7927 PromOp.getOpcode() == ISD::SIGN_EXTEND ||
7928 PromOp.getOpcode() == ISD::ZERO_EXTEND ||
7929 PromOp.getOpcode() == ISD::ANY_EXTEND) {
7930 if (!isa<ConstantSDNode>(PromOp.getOperand(0)) &&
7931 PromOp.getOperand(0).getValueType() != MVT::i1) {
7932 // The operand is not yet ready (see comment below).
7933 PromOps.insert(PromOps.begin(), PromOp);
7934 continue;
7935 }
7936
7937 SDValue RepValue = PromOp.getOperand(0);
7938 if (isa<ConstantSDNode>(RepValue))
7939 RepValue = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, RepValue);
7940
7941 DAG.ReplaceAllUsesOfValueWith(PromOp, RepValue);
7942 continue;
7943 }
7944
7945 unsigned C;
7946 switch (PromOp.getOpcode()) {
7947 default: C = 0; break;
7948 case ISD::SELECT: C = 1; break;
7949 case ISD::SELECT_CC: C = 2; break;
7950 }
7951
7952 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
7953 PromOp.getOperand(C).getValueType() != MVT::i1) ||
7954 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
7955 PromOp.getOperand(C+1).getValueType() != MVT::i1)) {
7956 // The to-be-promoted operands of this node have not yet been
7957 // promoted (this should be rare because we're going through the
7958 // list backward, but if one of the operands has several users in
7959 // this cluster of to-be-promoted nodes, it is possible).
7960 PromOps.insert(PromOps.begin(), PromOp);
7961 continue;
7962 }
7963
7964 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
7965 PromOp.getNode()->op_end());
7966
7967 // If there are any constant inputs, make sure they're replaced now.
7968 for (unsigned i = 0; i < 2; ++i)
7969 if (isa<ConstantSDNode>(Ops[C+i]))
7970 Ops[C+i] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ops[C+i]);
7971
7972 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00007973 DAG.getNode(PromOp.getOpcode(), dl, MVT::i1, Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00007974 }
7975
7976 // Now we're left with the initial truncation itself.
7977 if (N->getOpcode() == ISD::TRUNCATE)
7978 return N->getOperand(0);
7979
7980 // Otherwise, this is a comparison. The operands to be compared have just
7981 // changed type (to i1), but everything else is the same.
7982 return SDValue(N, 0);
7983}
7984
7985SDValue PPCTargetLowering::DAGCombineExtBoolTrunc(SDNode *N,
7986 DAGCombinerInfo &DCI) const {
7987 SelectionDAG &DAG = DCI.DAG;
7988 SDLoc dl(N);
7989
Hal Finkel940ab932014-02-28 00:27:01 +00007990 // If we're tracking CR bits, we need to be careful that we don't have:
7991 // zext(binary-ops(trunc(x), trunc(y)))
7992 // or
7993 // zext(binary-ops(binary-ops(trunc(x), trunc(y)), ...)
7994 // such that we're unnecessarily moving things into CR bits that can more
7995 // efficiently stay in GPRs. Note that if we're not certain that the high
7996 // bits are set as required by the final extension, we still may need to do
7997 // some masking to get the proper behavior.
7998
Hal Finkel46043ed2014-03-01 21:36:57 +00007999 // This same functionality is important on PPC64 when dealing with
8000 // 32-to-64-bit extensions; these occur often when 32-bit values are used as
8001 // the return values of functions. Because it is so similar, it is handled
8002 // here as well.
8003
Hal Finkel940ab932014-02-28 00:27:01 +00008004 if (N->getValueType(0) != MVT::i32 &&
8005 N->getValueType(0) != MVT::i64)
8006 return SDValue();
8007
Hal Finkel46043ed2014-03-01 21:36:57 +00008008 if (!((N->getOperand(0).getValueType() == MVT::i1 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008009 Subtarget.useCRBits()) ||
Hal Finkel46043ed2014-03-01 21:36:57 +00008010 (N->getOperand(0).getValueType() == MVT::i32 &&
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008011 Subtarget.isPPC64())))
Hal Finkel940ab932014-02-28 00:27:01 +00008012 return SDValue();
8013
8014 if (N->getOperand(0).getOpcode() != ISD::AND &&
8015 N->getOperand(0).getOpcode() != ISD::OR &&
8016 N->getOperand(0).getOpcode() != ISD::XOR &&
8017 N->getOperand(0).getOpcode() != ISD::SELECT &&
8018 N->getOperand(0).getOpcode() != ISD::SELECT_CC)
8019 return SDValue();
8020
8021 SmallVector<SDValue, 4> Inputs;
8022 SmallVector<SDValue, 8> BinOps(1, N->getOperand(0)), PromOps;
8023 SmallPtrSet<SDNode *, 16> Visited;
8024
8025 // Visit all inputs, collect all binary operations (and, or, xor and
8026 // select) that are all fed by truncations.
8027 while (!BinOps.empty()) {
8028 SDValue BinOp = BinOps.back();
8029 BinOps.pop_back();
8030
8031 if (!Visited.insert(BinOp.getNode()))
8032 continue;
8033
8034 PromOps.push_back(BinOp);
8035
8036 for (unsigned i = 0, ie = BinOp.getNumOperands(); i != ie; ++i) {
8037 // The condition of the select is not promoted.
8038 if (BinOp.getOpcode() == ISD::SELECT && i == 0)
8039 continue;
8040 if (BinOp.getOpcode() == ISD::SELECT_CC && i != 2 && i != 3)
8041 continue;
8042
8043 if (BinOp.getOperand(i).getOpcode() == ISD::TRUNCATE ||
8044 isa<ConstantSDNode>(BinOp.getOperand(i))) {
8045 Inputs.push_back(BinOp.getOperand(i));
8046 } else if (BinOp.getOperand(i).getOpcode() == ISD::AND ||
8047 BinOp.getOperand(i).getOpcode() == ISD::OR ||
8048 BinOp.getOperand(i).getOpcode() == ISD::XOR ||
8049 BinOp.getOperand(i).getOpcode() == ISD::SELECT ||
8050 BinOp.getOperand(i).getOpcode() == ISD::SELECT_CC) {
8051 BinOps.push_back(BinOp.getOperand(i));
8052 } else {
8053 // We have an input that is not a truncation or another binary
8054 // operation; we'll abort this transformation.
8055 return SDValue();
8056 }
8057 }
8058 }
8059
8060 // Make sure that this is a self-contained cluster of operations (which
8061 // is not quite the same thing as saying that everything has only one
8062 // use).
8063 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
8064 if (isa<ConstantSDNode>(Inputs[i]))
8065 continue;
8066
8067 for (SDNode::use_iterator UI = Inputs[i].getNode()->use_begin(),
8068 UE = Inputs[i].getNode()->use_end();
8069 UI != UE; ++UI) {
8070 SDNode *User = *UI;
8071 if (User != N && !Visited.count(User))
8072 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00008073
8074 // Make sure that we're not going to promote the non-output-value
8075 // operand(s) or SELECT or SELECT_CC.
8076 // FIXME: Although we could sometimes handle this, and it does occur in
8077 // practice that one of the condition inputs to the select is also one of
8078 // the outputs, we currently can't deal with this.
8079 if (User->getOpcode() == ISD::SELECT) {
8080 if (User->getOperand(0) == Inputs[i])
8081 return SDValue();
8082 } else if (User->getOpcode() == ISD::SELECT_CC) {
8083 if (User->getOperand(0) == Inputs[i] ||
8084 User->getOperand(1) == Inputs[i])
8085 return SDValue();
8086 }
Hal Finkel940ab932014-02-28 00:27:01 +00008087 }
8088 }
8089
8090 for (unsigned i = 0, ie = PromOps.size(); i != ie; ++i) {
8091 for (SDNode::use_iterator UI = PromOps[i].getNode()->use_begin(),
8092 UE = PromOps[i].getNode()->use_end();
8093 UI != UE; ++UI) {
8094 SDNode *User = *UI;
8095 if (User != N && !Visited.count(User))
8096 return SDValue();
Hal Finkel46043ed2014-03-01 21:36:57 +00008097
8098 // Make sure that we're not going to promote the non-output-value
8099 // operand(s) or SELECT or SELECT_CC.
8100 // FIXME: Although we could sometimes handle this, and it does occur in
8101 // practice that one of the condition inputs to the select is also one of
8102 // the outputs, we currently can't deal with this.
8103 if (User->getOpcode() == ISD::SELECT) {
8104 if (User->getOperand(0) == PromOps[i])
8105 return SDValue();
8106 } else if (User->getOpcode() == ISD::SELECT_CC) {
8107 if (User->getOperand(0) == PromOps[i] ||
8108 User->getOperand(1) == PromOps[i])
8109 return SDValue();
8110 }
Hal Finkel940ab932014-02-28 00:27:01 +00008111 }
8112 }
8113
Hal Finkel46043ed2014-03-01 21:36:57 +00008114 unsigned PromBits = N->getOperand(0).getValueSizeInBits();
Hal Finkel940ab932014-02-28 00:27:01 +00008115 bool ReallyNeedsExt = false;
8116 if (N->getOpcode() != ISD::ANY_EXTEND) {
8117 // If all of the inputs are not already sign/zero extended, then
8118 // we'll still need to do that at the end.
8119 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
8120 if (isa<ConstantSDNode>(Inputs[i]))
8121 continue;
8122
8123 unsigned OpBits =
8124 Inputs[i].getOperand(0).getValueSizeInBits();
Hal Finkel46043ed2014-03-01 21:36:57 +00008125 assert(PromBits < OpBits && "Truncation not to a smaller bit count?");
8126
Hal Finkel940ab932014-02-28 00:27:01 +00008127 if ((N->getOpcode() == ISD::ZERO_EXTEND &&
8128 !DAG.MaskedValueIsZero(Inputs[i].getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00008129 APInt::getHighBitsSet(OpBits,
8130 OpBits-PromBits))) ||
Hal Finkel940ab932014-02-28 00:27:01 +00008131 (N->getOpcode() == ISD::SIGN_EXTEND &&
Hal Finkel46043ed2014-03-01 21:36:57 +00008132 DAG.ComputeNumSignBits(Inputs[i].getOperand(0)) <
8133 (OpBits-(PromBits-1)))) {
Hal Finkel940ab932014-02-28 00:27:01 +00008134 ReallyNeedsExt = true;
8135 break;
8136 }
8137 }
8138 }
8139
8140 // Replace all inputs, either with the truncation operand, or a
8141 // truncation or extension to the final output type.
8142 for (unsigned i = 0, ie = Inputs.size(); i != ie; ++i) {
8143 // Constant inputs need to be replaced with the to-be-promoted nodes that
8144 // use them because they might have users outside of the cluster of
8145 // promoted nodes.
8146 if (isa<ConstantSDNode>(Inputs[i]))
8147 continue;
8148
8149 SDValue InSrc = Inputs[i].getOperand(0);
8150 if (Inputs[i].getValueType() == N->getValueType(0))
8151 DAG.ReplaceAllUsesOfValueWith(Inputs[i], InSrc);
8152 else if (N->getOpcode() == ISD::SIGN_EXTEND)
8153 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
8154 DAG.getSExtOrTrunc(InSrc, dl, N->getValueType(0)));
8155 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8156 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
8157 DAG.getZExtOrTrunc(InSrc, dl, N->getValueType(0)));
8158 else
8159 DAG.ReplaceAllUsesOfValueWith(Inputs[i],
8160 DAG.getAnyExtOrTrunc(InSrc, dl, N->getValueType(0)));
8161 }
8162
8163 // Replace all operations (these are all the same, but have a different
8164 // (promoted) return type). DAG.getNode will validate that the types of
8165 // a binary operator match, so go through the list in reverse so that
8166 // we've likely promoted both operands first.
8167 while (!PromOps.empty()) {
8168 SDValue PromOp = PromOps.back();
8169 PromOps.pop_back();
8170
8171 unsigned C;
8172 switch (PromOp.getOpcode()) {
8173 default: C = 0; break;
8174 case ISD::SELECT: C = 1; break;
8175 case ISD::SELECT_CC: C = 2; break;
8176 }
8177
8178 if ((!isa<ConstantSDNode>(PromOp.getOperand(C)) &&
8179 PromOp.getOperand(C).getValueType() != N->getValueType(0)) ||
8180 (!isa<ConstantSDNode>(PromOp.getOperand(C+1)) &&
8181 PromOp.getOperand(C+1).getValueType() != N->getValueType(0))) {
8182 // The to-be-promoted operands of this node have not yet been
8183 // promoted (this should be rare because we're going through the
8184 // list backward, but if one of the operands has several users in
8185 // this cluster of to-be-promoted nodes, it is possible).
8186 PromOps.insert(PromOps.begin(), PromOp);
8187 continue;
8188 }
8189
8190 SmallVector<SDValue, 3> Ops(PromOp.getNode()->op_begin(),
8191 PromOp.getNode()->op_end());
8192
8193 // If this node has constant inputs, then they'll need to be promoted here.
8194 for (unsigned i = 0; i < 2; ++i) {
8195 if (!isa<ConstantSDNode>(Ops[C+i]))
8196 continue;
8197 if (Ops[C+i].getValueType() == N->getValueType(0))
8198 continue;
8199
8200 if (N->getOpcode() == ISD::SIGN_EXTEND)
8201 Ops[C+i] = DAG.getSExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
8202 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8203 Ops[C+i] = DAG.getZExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
8204 else
8205 Ops[C+i] = DAG.getAnyExtOrTrunc(Ops[C+i], dl, N->getValueType(0));
8206 }
8207
8208 DAG.ReplaceAllUsesOfValueWith(PromOp,
Craig Topper48d114b2014-04-26 18:35:24 +00008209 DAG.getNode(PromOp.getOpcode(), dl, N->getValueType(0), Ops));
Hal Finkel940ab932014-02-28 00:27:01 +00008210 }
8211
8212 // Now we're left with the initial extension itself.
8213 if (!ReallyNeedsExt)
8214 return N->getOperand(0);
8215
Hal Finkel46043ed2014-03-01 21:36:57 +00008216 // To zero extend, just mask off everything except for the first bit (in the
8217 // i1 case).
Hal Finkel940ab932014-02-28 00:27:01 +00008218 if (N->getOpcode() == ISD::ZERO_EXTEND)
8219 return DAG.getNode(ISD::AND, dl, N->getValueType(0), N->getOperand(0),
Hal Finkel46043ed2014-03-01 21:36:57 +00008220 DAG.getConstant(APInt::getLowBitsSet(
8221 N->getValueSizeInBits(0), PromBits),
8222 N->getValueType(0)));
Hal Finkel940ab932014-02-28 00:27:01 +00008223
8224 assert(N->getOpcode() == ISD::SIGN_EXTEND &&
8225 "Invalid extension type");
8226 EVT ShiftAmountTy = getShiftAmountTy(N->getValueType(0));
8227 SDValue ShiftCst =
Hal Finkel46043ed2014-03-01 21:36:57 +00008228 DAG.getConstant(N->getValueSizeInBits(0)-PromBits, ShiftAmountTy);
Hal Finkel940ab932014-02-28 00:27:01 +00008229 return DAG.getNode(ISD::SRA, dl, N->getValueType(0),
8230 DAG.getNode(ISD::SHL, dl, N->getValueType(0),
8231 N->getOperand(0), ShiftCst), ShiftCst);
8232}
8233
Duncan Sandsdc2dac12008-11-24 14:53:14 +00008234SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
8235 DAGCombinerInfo &DCI) const {
Dan Gohman57c732b2010-04-21 01:34:56 +00008236 const TargetMachine &TM = getTargetMachine();
Chris Lattnerf4184352006-03-01 04:57:39 +00008237 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008238 SDLoc dl(N);
Chris Lattnerf4184352006-03-01 04:57:39 +00008239 switch (N->getOpcode()) {
8240 default: break;
Chris Lattner3c48ea52006-09-19 05:22:59 +00008241 case PPCISD::SHL:
8242 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00008243 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00008244 return N->getOperand(0);
8245 }
8246 break;
8247 case PPCISD::SRL:
8248 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00008249 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00008250 return N->getOperand(0);
8251 }
8252 break;
8253 case PPCISD::SRA:
8254 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf1d83042010-06-18 14:22:04 +00008255 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattner3c48ea52006-09-19 05:22:59 +00008256 C->isAllOnesValue()) // -1 >>s V -> -1.
8257 return N->getOperand(0);
8258 }
8259 break;
Hal Finkel940ab932014-02-28 00:27:01 +00008260 case ISD::SIGN_EXTEND:
8261 case ISD::ZERO_EXTEND:
8262 case ISD::ANY_EXTEND:
8263 return DAGCombineExtBoolTrunc(N, DCI);
8264 case ISD::TRUNCATE:
8265 case ISD::SETCC:
8266 case ISD::SELECT_CC:
8267 return DAGCombineTruncBoolExt(N, DCI);
Hal Finkel2e103312013-04-03 04:01:11 +00008268 case ISD::FDIV: {
8269 assert(TM.Options.UnsafeFPMath &&
8270 "Reciprocal estimates require UnsafeFPMath");
Scott Michelcf0da6c2009-02-17 22:15:04 +00008271
Hal Finkel2e103312013-04-03 04:01:11 +00008272 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkelb0c810f2013-04-03 17:44:56 +00008273 SDValue RV =
8274 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00008275 if (RV.getNode()) {
Hal Finkel2e103312013-04-03 04:01:11 +00008276 DCI.AddToWorklist(RV.getNode());
8277 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
8278 N->getOperand(0), RV);
8279 }
Hal Finkelf96c18e2013-04-04 22:44:12 +00008280 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
8281 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
8282 SDValue RV =
8283 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
8284 DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00008285 if (RV.getNode()) {
Hal Finkelf96c18e2013-04-04 22:44:12 +00008286 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00008287 RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00008288 N->getValueType(0), RV);
8289 DCI.AddToWorklist(RV.getNode());
8290 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
8291 N->getOperand(0), RV);
8292 }
8293 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
8294 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
8295 SDValue RV =
8296 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
8297 DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00008298 if (RV.getNode()) {
Hal Finkelf96c18e2013-04-04 22:44:12 +00008299 DCI.AddToWorklist(RV.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00008300 RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)),
Hal Finkelf96c18e2013-04-04 22:44:12 +00008301 N->getValueType(0), RV,
8302 N->getOperand(1).getOperand(1));
8303 DCI.AddToWorklist(RV.getNode());
8304 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
8305 N->getOperand(0), RV);
8306 }
Hal Finkel2e103312013-04-03 04:01:11 +00008307 }
8308
Hal Finkelb0c810f2013-04-03 17:44:56 +00008309 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00008310 if (RV.getNode()) {
Hal Finkel2e103312013-04-03 04:01:11 +00008311 DCI.AddToWorklist(RV.getNode());
8312 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
8313 N->getOperand(0), RV);
8314 }
8315
8316 }
8317 break;
8318 case ISD::FSQRT: {
8319 assert(TM.Options.UnsafeFPMath &&
8320 "Reciprocal estimates require UnsafeFPMath");
8321
8322 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
8323 // reciprocal sqrt.
Hal Finkelb0c810f2013-04-03 17:44:56 +00008324 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00008325 if (RV.getNode()) {
Hal Finkel2e103312013-04-03 04:01:11 +00008326 DCI.AddToWorklist(RV.getNode());
Hal Finkelb0c810f2013-04-03 17:44:56 +00008327 RV = DAGCombineFastRecip(RV, DCI);
Craig Topper062a2ba2014-04-25 05:30:21 +00008328 if (RV.getNode()) {
Eric Christopher174c6622014-05-30 22:47:48 +00008329 // Unfortunately, RV is now NaN if the input was exactly 0. Select out
8330 // this case and force the answer to 0.
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00008331
8332 EVT VT = RV.getValueType();
8333
8334 SDValue Zero = DAG.getConstantFP(0.0, VT.getScalarType());
8335 if (VT.isVector()) {
8336 assert(VT.getVectorNumElements() == 4 && "Unknown vector type");
8337 Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Zero, Zero, Zero, Zero);
8338 }
8339
8340 SDValue ZeroCmp =
8341 DAG.getSetCC(dl, getSetCCResultType(*DAG.getContext(), VT),
8342 N->getOperand(0), Zero, ISD::SETEQ);
8343 DCI.AddToWorklist(ZeroCmp.getNode());
8344 DCI.AddToWorklist(RV.getNode());
8345
8346 RV = DAG.getNode(VT.isVector() ? ISD::VSELECT : ISD::SELECT, dl, VT,
8347 ZeroCmp, Zero, RV);
Hal Finkel2e103312013-04-03 04:01:11 +00008348 return RV;
Hal Finkel1e2e3ea2013-09-12 19:04:12 +00008349 }
Hal Finkel2e103312013-04-03 04:01:11 +00008350 }
8351
8352 }
8353 break;
Chris Lattnerf4184352006-03-01 04:57:39 +00008354 case ISD::SINT_TO_FP:
Chris Lattnera35f3062006-06-16 17:34:12 +00008355 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattner4a66d692006-03-22 05:30:33 +00008356 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
8357 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
8358 // We allow the src/dst to be either f32/f64, but the intermediate
8359 // type must be i64.
Owen Anderson9f944592009-08-11 20:47:22 +00008360 if (N->getOperand(0).getValueType() == MVT::i64 &&
8361 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008362 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00008363 if (Val.getValueType() == MVT::f32) {
8364 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008365 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00008366 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008367
Owen Anderson9f944592009-08-11 20:47:22 +00008368 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008369 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00008370 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008371 DCI.AddToWorklist(Val.getNode());
Owen Anderson9f944592009-08-11 20:47:22 +00008372 if (N->getValueType(0) == MVT::f32) {
8373 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner72733e52008-01-17 07:00:52 +00008374 DAG.getIntPtrConstant(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00008375 DCI.AddToWorklist(Val.getNode());
Chris Lattner4a66d692006-03-22 05:30:33 +00008376 }
8377 return Val;
Owen Anderson9f944592009-08-11 20:47:22 +00008378 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattner4a66d692006-03-22 05:30:33 +00008379 // If the intermediate type is i32, we can avoid the load/store here
8380 // too.
Chris Lattnerf4184352006-03-01 04:57:39 +00008381 }
Chris Lattnerf4184352006-03-01 04:57:39 +00008382 }
8383 }
8384 break;
Chris Lattner27f53452006-03-01 05:50:56 +00008385 case ISD::STORE:
8386 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
8387 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnerf5b46f72008-01-18 16:54:56 +00008388 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner27f53452006-03-01 05:50:56 +00008389 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson9f944592009-08-11 20:47:22 +00008390 N->getOperand(1).getValueType() == MVT::i32 &&
8391 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008392 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00008393 if (Val.getValueType() == MVT::f32) {
8394 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008395 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008396 }
Owen Anderson9f944592009-08-11 20:47:22 +00008397 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greiff304a7a2008-08-28 21:40:38 +00008398 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008399
Hal Finkel60c75102013-04-01 15:37:53 +00008400 SDValue Ops[] = {
8401 N->getOperand(0), Val, N->getOperand(2),
8402 DAG.getValueType(N->getOperand(1).getValueType())
8403 };
8404
8405 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
Craig Topper206fcd42014-04-26 19:29:41 +00008406 DAG.getVTList(MVT::Other), Ops,
Hal Finkel60c75102013-04-01 15:37:53 +00008407 cast<StoreSDNode>(N)->getMemoryVT(),
8408 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greiff304a7a2008-08-28 21:40:38 +00008409 DCI.AddToWorklist(Val.getNode());
Chris Lattner27f53452006-03-01 05:50:56 +00008410 return Val;
8411 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008412
Chris Lattnera7976d32006-07-10 20:56:58 +00008413 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman28328db2009-09-25 00:57:30 +00008414 if (cast<StoreSDNode>(N)->isUnindexed() &&
8415 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00008416 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson9f944592009-08-11 20:47:22 +00008417 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkel31d29562013-03-28 19:25:55 +00008418 N->getOperand(1).getValueType() == MVT::i16 ||
8419 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008420 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008421 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008422 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008423 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson9f944592009-08-11 20:47:22 +00008424 if (BSwapOp.getValueType() == MVT::i16)
8425 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnera7976d32006-07-10 20:56:58 +00008426
Dan Gohman48b185d2009-09-25 20:36:54 +00008427 SDValue Ops[] = {
8428 N->getOperand(0), BSwapOp, N->getOperand(2),
8429 DAG.getValueType(N->getOperand(1).getValueType())
8430 };
8431 return
8432 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00008433 Ops, cast<StoreSDNode>(N)->getMemoryVT(),
Dan Gohman48b185d2009-09-25 20:36:54 +00008434 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008435 }
8436 break;
Hal Finkelcf2e9082013-05-24 23:00:14 +00008437 case ISD::LOAD: {
8438 LoadSDNode *LD = cast<LoadSDNode>(N);
8439 EVT VT = LD->getValueType(0);
8440 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
8441 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
8442 if (ISD::isNON_EXTLoad(N) && VT.isVector() &&
8443 TM.getSubtarget<PPCSubtarget>().hasAltivec() &&
Hal Finkel40c34782013-09-15 22:09:58 +00008444 (VT == MVT::v16i8 || VT == MVT::v8i16 ||
8445 VT == MVT::v4i32 || VT == MVT::v4f32) &&
Hal Finkelcf2e9082013-05-24 23:00:14 +00008446 LD->getAlignment() < ABIAlignment) {
8447 // This is a type-legal unaligned Altivec load.
8448 SDValue Chain = LD->getChain();
8449 SDValue Ptr = LD->getBasePtr();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008450 bool isLittleEndian = Subtarget.isLittleEndian();
Hal Finkelcf2e9082013-05-24 23:00:14 +00008451
8452 // This implements the loading of unaligned vectors as described in
8453 // the venerable Apple Velocity Engine overview. Specifically:
8454 // https://developer.apple.com/hardwaredrivers/ve/alignment.html
8455 // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html
8456 //
8457 // The general idea is to expand a sequence of one or more unaligned
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008458 // loads into an alignment-based permutation-control instruction (lvsl
8459 // or lvsr), a series of regular vector loads (which always truncate
8460 // their input address to an aligned address), and a series of
8461 // permutations. The results of these permutations are the requested
8462 // loaded values. The trick is that the last "extra" load is not taken
8463 // from the address you might suspect (sizeof(vector) bytes after the
8464 // last requested load), but rather sizeof(vector) - 1 bytes after the
8465 // last requested vector. The point of this is to avoid a page fault if
8466 // the base address happened to be aligned. This works because if the
8467 // base address is aligned, then adding less than a full vector length
8468 // will cause the last vector in the sequence to be (re)loaded.
8469 // Otherwise, the next vector will be fetched as you might suspect was
8470 // necessary.
Hal Finkelcf2e9082013-05-24 23:00:14 +00008471
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008472 // We might be able to reuse the permutation generation from
Hal Finkelcf2e9082013-05-24 23:00:14 +00008473 // a different base address offset from this one by an aligned amount.
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008474 // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this
8475 // optimization later.
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008476 Intrinsic::ID Intr = (isLittleEndian ?
8477 Intrinsic::ppc_altivec_lvsr :
8478 Intrinsic::ppc_altivec_lvsl);
8479 SDValue PermCntl = BuildIntrinsicOp(Intr, Ptr, DAG, dl, MVT::v16i8);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008480
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008481 // Create the new MMO for the new base load. It is like the original MMO,
8482 // but represents an area in memory almost twice the vector size centered
8483 // on the original address. If the address is unaligned, we might start
8484 // reading up to (sizeof(vector)-1) bytes below the address of the
8485 // original unaligned load.
Hal Finkelcf2e9082013-05-24 23:00:14 +00008486 MachineFunction &MF = DAG.getMachineFunction();
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008487 MachineMemOperand *BaseMMO =
8488 MF.getMachineMemOperand(LD->getMemOperand(),
8489 -LD->getMemoryVT().getStoreSize()+1,
8490 2*LD->getMemoryVT().getStoreSize()-1);
8491
8492 // Create the new base load.
8493 SDValue LDXIntID = DAG.getTargetConstant(Intrinsic::ppc_altivec_lvx,
8494 getPointerTy());
8495 SDValue BaseLoadOps[] = { Chain, LDXIntID, Ptr };
8496 SDValue BaseLoad =
8497 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
8498 DAG.getVTList(MVT::v4i32, MVT::Other),
8499 BaseLoadOps, MVT::v4i32, BaseMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008500
8501 // Note that the value of IncOffset (which is provided to the next
8502 // load's pointer info offset value, and thus used to calculate the
8503 // alignment), and the value of IncValue (which is actually used to
8504 // increment the pointer value) are different! This is because we
8505 // require the next load to appear to be aligned, even though it
8506 // is actually offset from the base pointer by a lesser amount.
8507 int IncOffset = VT.getSizeInBits() / 8;
Hal Finkel7d8a6912013-05-26 18:08:30 +00008508 int IncValue = IncOffset;
8509
8510 // Walk (both up and down) the chain looking for another load at the real
8511 // (aligned) offset (the alignment of the other load does not matter in
8512 // this case). If found, then do not use the offset reduction trick, as
8513 // that will prevent the loads from being later combined (as they would
8514 // otherwise be duplicates).
8515 if (!findConsecutiveLoad(LD, DAG))
8516 --IncValue;
8517
Hal Finkelcf2e9082013-05-24 23:00:14 +00008518 SDValue Increment = DAG.getConstant(IncValue, getPointerTy());
8519 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
8520
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008521 MachineMemOperand *ExtraMMO =
8522 MF.getMachineMemOperand(LD->getMemOperand(),
8523 1, 2*LD->getMemoryVT().getStoreSize()-1);
8524 SDValue ExtraLoadOps[] = { Chain, LDXIntID, Ptr };
Hal Finkelcf2e9082013-05-24 23:00:14 +00008525 SDValue ExtraLoad =
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008526 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, dl,
8527 DAG.getVTList(MVT::v4i32, MVT::Other),
8528 ExtraLoadOps, MVT::v4i32, ExtraMMO);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008529
8530 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8531 BaseLoad.getValue(1), ExtraLoad.getValue(1));
8532
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008533 // Because vperm has a big-endian bias, we must reverse the order
8534 // of the input vectors and complement the permute control vector
8535 // when generating little endian code. We have already handled the
8536 // latter by using lvsr instead of lvsl, so just reverse BaseLoad
8537 // and ExtraLoad here.
8538 SDValue Perm;
8539 if (isLittleEndian)
8540 Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
8541 ExtraLoad, BaseLoad, PermCntl, DAG, dl);
8542 else
8543 Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm,
8544 BaseLoad, ExtraLoad, PermCntl, DAG, dl);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008545
8546 if (VT != MVT::v4i32)
8547 Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm);
8548
Hal Finkelb6d0d6b2014-08-01 05:20:41 +00008549 // The output of the permutation is our loaded result, the TokenFactor is
8550 // our new chain.
8551 DCI.CombineTo(N, Perm, TF);
Hal Finkelcf2e9082013-05-24 23:00:14 +00008552 return SDValue(N, 0);
8553 }
8554 }
8555 break;
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008556 case ISD::INTRINSIC_WO_CHAIN: {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008557 bool isLittleEndian = Subtarget.isLittleEndian();
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008558 Intrinsic::ID Intr = (isLittleEndian ?
8559 Intrinsic::ppc_altivec_lvsr :
8560 Intrinsic::ppc_altivec_lvsl);
8561 if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() == Intr &&
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008562 N->getOperand(1)->getOpcode() == ISD::ADD) {
8563 SDValue Add = N->getOperand(1);
8564
8565 if (DAG.MaskedValueIsZero(Add->getOperand(1),
8566 APInt::getAllOnesValue(4 /* 16 byte alignment */).zext(
8567 Add.getValueType().getScalarType().getSizeInBits()))) {
8568 SDNode *BasePtr = Add->getOperand(0).getNode();
8569 for (SDNode::use_iterator UI = BasePtr->use_begin(),
8570 UE = BasePtr->use_end(); UI != UE; ++UI) {
8571 if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8572 cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() ==
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008573 Intr) {
8574 // We've found another LVSL/LVSR, and this address is an aligned
Hal Finkelbc2ee4c2013-05-25 04:05:05 +00008575 // multiple of that one. The results will be the same, so use the
8576 // one we've just found instead.
8577
8578 return SDValue(*UI, 0);
8579 }
8580 }
8581 }
8582 }
Bill Schmidt6b5a7df2014-06-09 22:00:52 +00008583 }
Hal Finkelc3cfbf82013-09-13 20:09:02 +00008584
8585 break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008586 case ISD::BSWAP:
8587 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008588 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnera7976d32006-07-10 20:56:58 +00008589 N->getOperand(0).hasOneUse() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008590 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
8591 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel22e41c42013-03-28 20:23:46 +00008592 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkel31d29562013-03-28 19:25:55 +00008593 N->getValueType(0) == MVT::i64))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008594 SDValue Load = N->getOperand(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00008595 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnera7976d32006-07-10 20:56:58 +00008596 // Create the byte-swapping load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008597 SDValue Ops[] = {
Evan Chenge71fe34d2006-10-09 20:57:25 +00008598 LD->getChain(), // Chain
8599 LD->getBasePtr(), // Ptr
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008600 DAG.getValueType(N->getValueType(0)) // VT
8601 };
Dan Gohman48b185d2009-09-25 20:36:54 +00008602 SDValue BSLoad =
8603 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkel31d29562013-03-28 19:25:55 +00008604 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
8605 MVT::i64 : MVT::i32, MVT::Other),
Craig Topper206fcd42014-04-26 19:29:41 +00008606 Ops, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnera7976d32006-07-10 20:56:58 +00008607
Scott Michelcf0da6c2009-02-17 22:15:04 +00008608 // If this is an i16 load, insert the truncate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008609 SDValue ResVal = BSLoad;
Owen Anderson9f944592009-08-11 20:47:22 +00008610 if (N->getValueType(0) == MVT::i16)
8611 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008612
Chris Lattnera7976d32006-07-10 20:56:58 +00008613 // First, combine the bswap away. This makes the value produced by the
8614 // load dead.
8615 DCI.CombineTo(N, ResVal);
8616
8617 // Next, combine the load away, we give it a bogus result value but a real
8618 // chain result. The result value is dead because the bswap is dead.
Gabor Greiff304a7a2008-08-28 21:40:38 +00008619 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00008620
Chris Lattnera7976d32006-07-10 20:56:58 +00008621 // Return N so it doesn't get rechecked!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008622 return SDValue(N, 0);
Chris Lattnera7976d32006-07-10 20:56:58 +00008623 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008624
Chris Lattner27f53452006-03-01 05:50:56 +00008625 break;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008626 case PPCISD::VCMP: {
8627 // If a VCMPo node already exists with exactly the same operands as this
8628 // node, use its result instead of this node (VCMPo computes both a CR6 and
8629 // a normal output).
8630 //
8631 if (!N->getOperand(0).hasOneUse() &&
8632 !N->getOperand(1).hasOneUse() &&
8633 !N->getOperand(2).hasOneUse()) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00008634
Chris Lattnerd4058a52006-03-31 06:02:07 +00008635 // Scan all of the users of the LHS, looking for VCMPo's that match.
Craig Topper062a2ba2014-04-25 05:30:21 +00008636 SDNode *VCMPoNode = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008637
Gabor Greiff304a7a2008-08-28 21:40:38 +00008638 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattnerd4058a52006-03-31 06:02:07 +00008639 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
8640 UI != E; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008641 if (UI->getOpcode() == PPCISD::VCMPo &&
8642 UI->getOperand(1) == N->getOperand(1) &&
8643 UI->getOperand(2) == N->getOperand(2) &&
8644 UI->getOperand(0) == N->getOperand(0)) {
8645 VCMPoNode = *UI;
Chris Lattnerd4058a52006-03-31 06:02:07 +00008646 break;
8647 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008648
Chris Lattner518834c2006-04-18 18:28:22 +00008649 // If there is no VCMPo node, or if the flag value has a single use, don't
8650 // transform this.
8651 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
8652 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008653
8654 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner518834c2006-04-18 18:28:22 +00008655 // chain, this transformation is more complex. Note that multiple things
8656 // could use the value result, which we should ignore.
Craig Topper062a2ba2014-04-25 05:30:21 +00008657 SDNode *FlagUser = nullptr;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008658 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Craig Topper062a2ba2014-04-25 05:30:21 +00008659 FlagUser == nullptr; ++UI) {
Chris Lattner518834c2006-04-18 18:28:22 +00008660 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman91e5dcb2008-07-27 20:43:25 +00008661 SDNode *User = *UI;
Chris Lattner518834c2006-04-18 18:28:22 +00008662 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008663 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner518834c2006-04-18 18:28:22 +00008664 FlagUser = User;
8665 break;
8666 }
8667 }
8668 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008669
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008670 // If the user is a MFOCRF instruction, we know this is safe.
8671 // Otherwise we give up for right now.
8672 if (FlagUser->getOpcode() == PPCISD::MFOCRF)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008673 return SDValue(VCMPoNode, 0);
Chris Lattnerd4058a52006-03-31 06:02:07 +00008674 }
8675 break;
8676 }
Hal Finkel940ab932014-02-28 00:27:01 +00008677 case ISD::BRCOND: {
8678 SDValue Cond = N->getOperand(1);
8679 SDValue Target = N->getOperand(2);
8680
8681 if (Cond.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8682 cast<ConstantSDNode>(Cond.getOperand(1))->getZExtValue() ==
8683 Intrinsic::ppc_is_decremented_ctr_nonzero) {
8684
8685 // We now need to make the intrinsic dead (it cannot be instruction
8686 // selected).
8687 DAG.ReplaceAllUsesOfValueWith(Cond.getValue(1), Cond.getOperand(0));
8688 assert(Cond.getNode()->hasOneUse() &&
8689 "Counter decrement has more than one use");
8690
8691 return DAG.getNode(PPCISD::BDNZ, dl, MVT::Other,
8692 N->getOperand(0), Target);
8693 }
8694 }
8695 break;
Chris Lattner9754d142006-04-18 17:59:36 +00008696 case ISD::BR_CC: {
8697 // If this is a branch on an altivec predicate comparison, lower this so
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00008698 // that we don't have to do a MFOCRF: instead, branch directly on CR6. This
Chris Lattner9754d142006-04-18 17:59:36 +00008699 // lowering is done pre-legalize, because the legalizer lowers the predicate
8700 // compare down to code that is difficult to reassemble.
8701 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008702 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkel25c19922013-05-15 21:37:41 +00008703
8704 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
8705 // value. If so, pass-through the AND to get to the intrinsic.
8706 if (LHS.getOpcode() == ISD::AND &&
8707 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8708 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
8709 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8710 isa<ConstantSDNode>(LHS.getOperand(1)) &&
8711 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
8712 isZero())
8713 LHS = LHS.getOperand(0);
8714
8715 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
8716 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
8717 Intrinsic::ppc_is_decremented_ctr_nonzero &&
8718 isa<ConstantSDNode>(RHS)) {
8719 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
8720 "Counter decrement comparison is not EQ or NE");
8721
8722 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
8723 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
8724 (CC == ISD::SETNE && !Val);
8725
8726 // We now need to make the intrinsic dead (it cannot be instruction
8727 // selected).
8728 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
8729 assert(LHS.getNode()->hasOneUse() &&
8730 "Counter decrement has more than one use");
8731
8732 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
8733 N->getOperand(0), N->getOperand(4));
8734 }
8735
Chris Lattner9754d142006-04-18 17:59:36 +00008736 int CompareOpc;
8737 bool isDot;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008738
Chris Lattner9754d142006-04-18 17:59:36 +00008739 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8740 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
8741 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
8742 assert(isDot && "Can't compare against a vector result!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00008743
Chris Lattner9754d142006-04-18 17:59:36 +00008744 // If this is a comparison against something other than 0/1, then we know
8745 // that the condition is never/always true.
Dan Gohmaneffb8942008-09-12 16:56:44 +00008746 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner9754d142006-04-18 17:59:36 +00008747 if (Val != 0 && Val != 1) {
8748 if (CC == ISD::SETEQ) // Cond never true, remove branch.
8749 return N->getOperand(0);
8750 // Always !=, turn it into an unconditional branch.
Owen Anderson9f944592009-08-11 20:47:22 +00008751 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner9754d142006-04-18 17:59:36 +00008752 N->getOperand(0), N->getOperand(4));
8753 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008754
Chris Lattner9754d142006-04-18 17:59:36 +00008755 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008756
Chris Lattner9754d142006-04-18 17:59:36 +00008757 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008758 SDValue Ops[] = {
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008759 LHS.getOperand(2), // LHS of compare
8760 LHS.getOperand(3), // RHS of compare
Owen Anderson9f944592009-08-11 20:47:22 +00008761 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattnerd66f14e2006-08-11 17:18:05 +00008762 };
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00008763 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Craig Topper48d114b2014-04-26 18:35:24 +00008764 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops);
Scott Michelcf0da6c2009-02-17 22:15:04 +00008765
Chris Lattner9754d142006-04-18 17:59:36 +00008766 // Unpack the result based on how the target uses it.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008767 PPC::Predicate CompOpc;
Dan Gohmaneffb8942008-09-12 16:56:44 +00008768 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner9754d142006-04-18 17:59:36 +00008769 default: // Can't happen, don't crash on invalid number though.
8770 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008771 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner9754d142006-04-18 17:59:36 +00008772 break;
8773 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008774 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner9754d142006-04-18 17:59:36 +00008775 break;
8776 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008777 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner9754d142006-04-18 17:59:36 +00008778 break;
8779 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattner8c6a41e2006-11-17 22:10:59 +00008780 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner9754d142006-04-18 17:59:36 +00008781 break;
8782 }
8783
Owen Anderson9f944592009-08-11 20:47:22 +00008784 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
8785 DAG.getConstant(CompOpc, MVT::i32),
8786 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner9754d142006-04-18 17:59:36 +00008787 N->getOperand(4), CompNode.getValue(1));
8788 }
8789 break;
8790 }
Chris Lattnerf4184352006-03-01 04:57:39 +00008791 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008792
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008793 return SDValue();
Chris Lattnerf4184352006-03-01 04:57:39 +00008794}
8795
Chris Lattner4211ca92006-04-14 06:01:58 +00008796//===----------------------------------------------------------------------===//
8797// Inline Assembly Support
8798//===----------------------------------------------------------------------===//
8799
Jay Foada0653a32014-05-14 21:14:37 +00008800void PPCTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
8801 APInt &KnownZero,
8802 APInt &KnownOne,
8803 const SelectionDAG &DAG,
8804 unsigned Depth) const {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00008805 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerc5287c02006-04-02 06:26:07 +00008806 switch (Op.getOpcode()) {
8807 default: break;
Chris Lattnera7976d32006-07-10 20:56:58 +00008808 case PPCISD::LBRX: {
8809 // lhbrx is known to have the top bits cleared out.
Dan Gohmana5fc0352009-09-27 23:17:47 +00008810 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnera7976d32006-07-10 20:56:58 +00008811 KnownZero = 0xFFFF0000;
8812 break;
8813 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008814 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmaneffb8942008-09-12 16:56:44 +00008815 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerc5287c02006-04-02 06:26:07 +00008816 default: break;
8817 case Intrinsic::ppc_altivec_vcmpbfp_p:
8818 case Intrinsic::ppc_altivec_vcmpeqfp_p:
8819 case Intrinsic::ppc_altivec_vcmpequb_p:
8820 case Intrinsic::ppc_altivec_vcmpequh_p:
8821 case Intrinsic::ppc_altivec_vcmpequw_p:
8822 case Intrinsic::ppc_altivec_vcmpgefp_p:
8823 case Intrinsic::ppc_altivec_vcmpgtfp_p:
8824 case Intrinsic::ppc_altivec_vcmpgtsb_p:
8825 case Intrinsic::ppc_altivec_vcmpgtsh_p:
8826 case Intrinsic::ppc_altivec_vcmpgtsw_p:
8827 case Intrinsic::ppc_altivec_vcmpgtub_p:
8828 case Intrinsic::ppc_altivec_vcmpgtuh_p:
8829 case Intrinsic::ppc_altivec_vcmpgtuw_p:
8830 KnownZero = ~1U; // All bits but the low one are known to be zero.
8831 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008832 }
Chris Lattnerc5287c02006-04-02 06:26:07 +00008833 }
8834 }
8835}
8836
8837
Chris Lattnerd6855142007-03-25 02:14:49 +00008838/// getConstraintType - Given a constraint, return the type of
Chris Lattner203b2f12006-02-07 20:16:30 +00008839/// constraint it is for this target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00008840PPCTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00008841PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
8842 if (Constraint.size() == 1) {
8843 switch (Constraint[0]) {
8844 default: break;
8845 case 'b':
8846 case 'r':
8847 case 'f':
8848 case 'v':
8849 case 'y':
8850 return C_RegisterClass;
Hal Finkel4f24c622012-11-05 18:18:42 +00008851 case 'Z':
8852 // FIXME: While Z does indicate a memory constraint, it specifically
8853 // indicates an r+r address (used in conjunction with the 'y' modifier
8854 // in the replacement string). Currently, we're forcing the base
8855 // register to be r0 in the asm printer (which is interpreted as zero)
8856 // and forming the complete address in the second register. This is
8857 // suboptimal.
8858 return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +00008859 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008860 } else if (Constraint == "wc") { // individual CR bits.
8861 return C_RegisterClass;
Hal Finkel27774d92014-03-13 07:58:58 +00008862 } else if (Constraint == "wa" || Constraint == "wd" ||
8863 Constraint == "wf" || Constraint == "ws") {
8864 return C_RegisterClass; // VSX registers.
Chris Lattnerd6855142007-03-25 02:14:49 +00008865 }
8866 return TargetLowering::getConstraintType(Constraint);
Chris Lattner203b2f12006-02-07 20:16:30 +00008867}
8868
John Thompsone8360b72010-10-29 17:29:13 +00008869/// Examine constraint type and operand type and determine a weight value.
8870/// This object must already have been set up with the operand type
8871/// and the current alternative constraint selected.
8872TargetLowering::ConstraintWeight
8873PPCTargetLowering::getSingleConstraintMatchWeight(
8874 AsmOperandInfo &info, const char *constraint) const {
8875 ConstraintWeight weight = CW_Invalid;
8876 Value *CallOperandVal = info.CallOperandVal;
8877 // If we don't have a value, we can't do a match,
8878 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00008879 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00008880 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00008881 Type *type = CallOperandVal->getType();
Hal Finkel6aca2372014-03-02 18:23:39 +00008882
John Thompsone8360b72010-10-29 17:29:13 +00008883 // Look at the constraint type.
Hal Finkel6aca2372014-03-02 18:23:39 +00008884 if (StringRef(constraint) == "wc" && type->isIntegerTy(1))
8885 return CW_Register; // an individual CR bit.
Hal Finkel27774d92014-03-13 07:58:58 +00008886 else if ((StringRef(constraint) == "wa" ||
8887 StringRef(constraint) == "wd" ||
8888 StringRef(constraint) == "wf") &&
8889 type->isVectorTy())
8890 return CW_Register;
8891 else if (StringRef(constraint) == "ws" && type->isDoubleTy())
8892 return CW_Register;
Hal Finkel6aca2372014-03-02 18:23:39 +00008893
John Thompsone8360b72010-10-29 17:29:13 +00008894 switch (*constraint) {
8895 default:
8896 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8897 break;
8898 case 'b':
8899 if (type->isIntegerTy())
8900 weight = CW_Register;
8901 break;
8902 case 'f':
8903 if (type->isFloatTy())
8904 weight = CW_Register;
8905 break;
8906 case 'd':
8907 if (type->isDoubleTy())
8908 weight = CW_Register;
8909 break;
8910 case 'v':
8911 if (type->isVectorTy())
8912 weight = CW_Register;
8913 break;
8914 case 'y':
8915 weight = CW_Register;
8916 break;
Hal Finkel4f24c622012-11-05 18:18:42 +00008917 case 'Z':
8918 weight = CW_Memory;
8919 break;
John Thompsone8360b72010-10-29 17:29:13 +00008920 }
8921 return weight;
8922}
8923
Scott Michelcf0da6c2009-02-17 22:15:04 +00008924std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner584a11a2006-11-02 01:44:04 +00008925PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00008926 MVT VT) const {
Chris Lattner01513612006-01-31 19:20:21 +00008927 if (Constraint.size() == 1) {
Chris Lattner584a11a2006-11-02 01:44:04 +00008928 // GCC RS6000 Constraint Letters
8929 switch (Constraint[0]) {
8930 case 'b': // R1-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008931 if (VT == MVT::i64 && Subtarget.isPPC64())
Hal Finkel638a9fa2013-03-19 18:51:05 +00008932 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
8933 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008934 case 'r': // R0-R31
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008935 if (VT == MVT::i64 && Subtarget.isPPC64())
Craig Topperabadc662012-04-20 06:31:50 +00008936 return std::make_pair(0U, &PPC::G8RCRegClass);
8937 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008938 case 'f':
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008939 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperabadc662012-04-20 06:31:50 +00008940 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand0de4a1e2012-10-29 17:49:34 +00008941 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperabadc662012-04-20 06:31:50 +00008942 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008943 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00008944 case 'v':
Craig Topperabadc662012-04-20 06:31:50 +00008945 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner584a11a2006-11-02 01:44:04 +00008946 case 'y': // crrc
Craig Topperabadc662012-04-20 06:31:50 +00008947 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008948 }
Hal Finkel6aca2372014-03-02 18:23:39 +00008949 } else if (Constraint == "wc") { // an individual CR bit.
8950 return std::make_pair(0U, &PPC::CRBITRCRegClass);
Hal Finkel27774d92014-03-13 07:58:58 +00008951 } else if (Constraint == "wa" || Constraint == "wd" ||
Hal Finkel19be5062014-03-29 05:29:01 +00008952 Constraint == "wf") {
Hal Finkel27774d92014-03-13 07:58:58 +00008953 return std::make_pair(0U, &PPC::VSRCRegClass);
Hal Finkel19be5062014-03-29 05:29:01 +00008954 } else if (Constraint == "ws") {
8955 return std::make_pair(0U, &PPC::VSFRCRegClass);
Chris Lattner01513612006-01-31 19:20:21 +00008956 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00008957
Hal Finkelb176acb2013-08-03 12:25:10 +00008958 std::pair<unsigned, const TargetRegisterClass*> R =
8959 TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8960
8961 // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers
8962 // (which we call X[0-9]+). If a 64-bit value has been requested, and a
8963 // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent
8964 // register.
8965 // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use
8966 // the AsmName field from *RegisterInfo.td, then this would not be necessary.
Eric Christopherb1aaebe2014-06-12 22:38:18 +00008967 if (R.first && VT == MVT::i64 && Subtarget.isPPC64() &&
Hal Finkelb176acb2013-08-03 12:25:10 +00008968 PPC::GPRCRegClass.contains(R.first)) {
8969 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
8970 return std::make_pair(TRI->getMatchingSuperReg(R.first,
Hal Finkelb3ca00d2013-08-14 20:05:04 +00008971 PPC::sub_32, &PPC::G8RCRegClass),
Hal Finkelb176acb2013-08-03 12:25:10 +00008972 &PPC::G8RCRegClass);
8973 }
8974
8975 return R;
Chris Lattner01513612006-01-31 19:20:21 +00008976}
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008977
Chris Lattner584a11a2006-11-02 01:44:04 +00008978
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00008979/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesence97d552010-06-25 21:55:36 +00008980/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher0713a9d2011-06-08 23:55:35 +00008981void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00008982 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008983 std::vector<SDValue>&Ops,
Chris Lattner724539c2008-04-26 23:02:14 +00008984 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00008985 SDValue Result;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008986
Eric Christopherde9399b2011-06-02 23:16:42 +00008987 // Only support length 1 constraints.
8988 if (Constraint.length() > 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +00008989
Eric Christopherde9399b2011-06-02 23:16:42 +00008990 char Letter = Constraint[0];
Chris Lattner15a6c4c2006-02-07 00:47:13 +00008991 switch (Letter) {
8992 default: break;
8993 case 'I':
8994 case 'J':
8995 case 'K':
8996 case 'L':
8997 case 'M':
8998 case 'N':
8999 case 'O':
9000 case 'P': {
Chris Lattner0b7472d2007-05-15 01:31:05 +00009001 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00009002 if (!CST) return; // Must be an immediate to match.
Dan Gohmaneffb8942008-09-12 16:56:44 +00009003 unsigned Value = CST->getZExtValue();
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009004 switch (Letter) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009005 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009006 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00009007 if ((short)Value == (int)Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00009008 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00009009 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009010 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
9011 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner0b7472d2007-05-15 01:31:05 +00009012 if ((short)Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00009013 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00009014 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009015 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00009016 if ((Value >> 16) == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00009017 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00009018 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009019 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner0b7472d2007-05-15 01:31:05 +00009020 if (Value > 31)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00009021 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00009022 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009023 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner0b7472d2007-05-15 01:31:05 +00009024 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00009025 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00009026 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009027 case 'O': // "O" is the constant zero.
Chris Lattner0b7472d2007-05-15 01:31:05 +00009028 if (Value == 0)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00009029 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00009030 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009031 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner0b7472d2007-05-15 01:31:05 +00009032 if ((short)-Value == (int)-Value)
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00009033 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattner8c6949e2006-10-31 19:40:43 +00009034 break;
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009035 }
9036 break;
9037 }
9038 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009039
Gabor Greiff304a7a2008-08-28 21:40:38 +00009040 if (Result.getNode()) {
Chris Lattnerd8c9cb92007-08-25 00:47:38 +00009041 Ops.push_back(Result);
9042 return;
9043 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009044
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009045 // Handle standard constraint letters.
Eric Christopherde9399b2011-06-02 23:16:42 +00009046 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner15a6c4c2006-02-07 00:47:13 +00009047}
Evan Cheng2dd2c652006-03-13 23:20:37 +00009048
Chris Lattner1eb94d92007-03-30 23:15:24 +00009049// isLegalAddressingMode - Return true if the addressing mode represented
9050// by AM is legal for this target, for a load/store of the specified type.
Scott Michelcf0da6c2009-02-17 22:15:04 +00009051bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00009052 Type *Ty) const {
Chris Lattner1eb94d92007-03-30 23:15:24 +00009053 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelcf0da6c2009-02-17 22:15:04 +00009054
Chris Lattner1eb94d92007-03-30 23:15:24 +00009055 // PPC allows a sign-extended 16-bit immediate field.
9056 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
9057 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009058
Chris Lattner1eb94d92007-03-30 23:15:24 +00009059 // No global is ever allowed as a base.
9060 if (AM.BaseGV)
9061 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009062
9063 // PPC only support r+r,
Chris Lattner1eb94d92007-03-30 23:15:24 +00009064 switch (AM.Scale) {
9065 case 0: // "r+i" or just "i", depending on HasBaseReg.
9066 break;
9067 case 1:
9068 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
9069 return false;
9070 // Otherwise we have r+r or r+i.
9071 break;
9072 case 2:
9073 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
9074 return false;
9075 // Allow 2*r as r+r.
9076 break;
Chris Lattner19ccd622007-04-09 22:10:05 +00009077 default:
9078 // No other scales are supported.
9079 return false;
Chris Lattner1eb94d92007-03-30 23:15:24 +00009080 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009081
Chris Lattner1eb94d92007-03-30 23:15:24 +00009082 return true;
9083}
9084
Dan Gohman21cea8a2010-04-17 15:26:15 +00009085SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
9086 SelectionDAG &DAG) const {
Evan Cheng168ced92010-05-22 01:47:14 +00009087 MachineFunction &MF = DAG.getMachineFunction();
9088 MachineFrameInfo *MFI = MF.getFrameInfo();
9089 MFI->setReturnAddressIsTaken(true);
9090
Bill Wendling908bf812014-01-06 00:43:20 +00009091 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00009092 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00009093
Andrew Trickef9de2a2013-05-25 02:42:55 +00009094 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009095 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattnerf6a81562007-12-08 06:59:59 +00009096
Dale Johannesen81bfca72010-05-03 22:59:34 +00009097 // Make sure the function does not optimize away the store of the RA to
9098 // the stack.
Chris Lattnerf6a81562007-12-08 06:59:59 +00009099 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen81bfca72010-05-03 22:59:34 +00009100 FuncInfo->setLRStoreRequired();
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009101 bool isPPC64 = Subtarget.isPPC64();
9102 bool isDarwinABI = Subtarget.isDarwinABI();
Dale Johannesen81bfca72010-05-03 22:59:34 +00009103
9104 if (Depth > 0) {
9105 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9106 SDValue Offset =
Wesley Peck527da1b2010-11-23 03:31:01 +00009107
Anton Korobeynikov2f931282011-01-10 12:39:04 +00009108 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen81bfca72010-05-03 22:59:34 +00009109 isPPC64? MVT::i64 : MVT::i32);
9110 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
9111 DAG.getNode(ISD::ADD, dl, getPointerTy(),
9112 FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009113 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009114 }
Chris Lattnerf6a81562007-12-08 06:59:59 +00009115
Chris Lattnerf6a81562007-12-08 06:59:59 +00009116 // Just load the return address off the stack.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009117 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009118 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009119 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerf6a81562007-12-08 06:59:59 +00009120}
9121
Dan Gohman21cea8a2010-04-17 15:26:15 +00009122SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
9123 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009124 SDLoc dl(Op);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009125 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00009126
Owen Anderson53aa7a92009-08-10 22:56:29 +00009127 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson9f944592009-08-11 20:47:22 +00009128 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009129
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00009130 MachineFunction &MF = DAG.getMachineFunction();
9131 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen81bfca72010-05-03 22:59:34 +00009132 MFI->setFrameAddressIsTaken(true);
Hal Finkelaa03c032013-03-21 19:03:19 +00009133
9134 // Naked functions never have a frame pointer, and so we use r1. For all
9135 // other functions, this decision must be delayed until during PEI.
9136 unsigned FrameReg;
9137 if (MF.getFunction()->getAttributes().hasAttribute(
9138 AttributeSet::FunctionIndex, Attribute::Naked))
9139 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
9140 else
9141 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
9142
Dale Johannesen81bfca72010-05-03 22:59:34 +00009143 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
9144 PtrVT);
9145 while (Depth--)
9146 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009147 FrameAddr, MachinePointerInfo(), false, false,
9148 false, 0);
Dale Johannesen81bfca72010-05-03 22:59:34 +00009149 return FrameAddr;
Nicolas Geoffray75ab9792007-03-01 13:11:38 +00009150}
Dan Gohmanc14e5222008-10-21 03:41:46 +00009151
Hal Finkel0d8db462014-05-11 19:29:11 +00009152// FIXME? Maybe this could be a TableGen attribute on some registers and
9153// this table could be generated automatically from RegInfo.
9154unsigned PPCTargetLowering::getRegisterByName(const char* RegName,
9155 EVT VT) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009156 bool isPPC64 = Subtarget.isPPC64();
9157 bool isDarwinABI = Subtarget.isDarwinABI();
Hal Finkel0d8db462014-05-11 19:29:11 +00009158
9159 if ((isPPC64 && VT != MVT::i64 && VT != MVT::i32) ||
9160 (!isPPC64 && VT != MVT::i32))
9161 report_fatal_error("Invalid register global variable type");
9162
9163 bool is64Bit = isPPC64 && VT == MVT::i64;
9164 unsigned Reg = StringSwitch<unsigned>(RegName)
9165 .Case("r1", is64Bit ? PPC::X1 : PPC::R1)
9166 .Case("r2", isDarwinABI ? 0 : (is64Bit ? PPC::X2 : PPC::R2))
9167 .Case("r13", (!isPPC64 && isDarwinABI) ? 0 :
9168 (is64Bit ? PPC::X13 : PPC::R13))
9169 .Default(0);
9170
9171 if (Reg)
9172 return Reg;
9173 report_fatal_error("Invalid register name global variable");
9174}
9175
Dan Gohmanc14e5222008-10-21 03:41:46 +00009176bool
9177PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9178 // The PowerPC target isn't yet aware of offsets.
9179 return false;
9180}
Tilmann Schellerb93960d2009-07-03 06:45:56 +00009181
Evan Chengd9929f02010-04-01 20:10:42 +00009182/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng61399372010-04-02 19:36:14 +00009183/// and store operations as a result of memset, memcpy, and memmove
9184/// lowering. If DstAlign is zero that means it's safe to destination
9185/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
9186/// means there isn't a need to check it against alignment requirement,
Evan Cheng962711e2012-12-12 02:34:41 +00009187/// probably because the source does not need to be loaded. If 'IsMemset' is
9188/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
9189/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
9190/// source is constant so it does not need to be loaded.
Dan Gohman148c69a2010-04-16 20:11:05 +00009191/// It returns EVT::Other if the type should be determined using generic
9192/// target-independent logic.
Evan Cheng43cd9e32010-04-01 06:04:33 +00009193EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
9194 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009195 bool IsMemset, bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00009196 bool MemcpyStrSrc,
Dan Gohman148c69a2010-04-16 20:11:05 +00009197 MachineFunction &MF) const {
Eric Christopherd90a8742014-06-12 22:38:20 +00009198 if (Subtarget.isPPC64()) {
Owen Anderson9f944592009-08-11 20:47:22 +00009199 return MVT::i64;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00009200 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00009201 return MVT::i32;
Tilmann Schellerb93960d2009-07-03 06:45:56 +00009202 }
9203}
Hal Finkel88ed4e32012-04-01 19:23:08 +00009204
Hal Finkel34974ed2014-04-12 21:52:38 +00009205/// \brief Returns true if it is beneficial to convert a load of a constant
9206/// to just the constant itself.
9207bool PPCTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
9208 Type *Ty) const {
9209 assert(Ty->isIntegerTy());
9210
9211 unsigned BitSize = Ty->getPrimitiveSizeInBits();
9212 if (BitSize == 0 || BitSize > 64)
9213 return false;
9214 return true;
9215}
9216
9217bool PPCTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
9218 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9219 return false;
9220 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9221 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
9222 return NumBits1 == 64 && NumBits2 == 32;
9223}
9224
9225bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
9226 if (!VT1.isInteger() || !VT2.isInteger())
9227 return false;
9228 unsigned NumBits1 = VT1.getSizeInBits();
9229 unsigned NumBits2 = VT2.getSizeInBits();
9230 return NumBits1 == 64 && NumBits2 == 32;
9231}
9232
9233bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
9234 return isInt<16>(Imm) || isUInt<16>(Imm);
9235}
9236
9237bool PPCTargetLowering::isLegalAddImmediate(int64_t Imm) const {
9238 return isInt<16>(Imm) || isUInt<16>(Imm);
9239}
9240
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009241bool PPCTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
9242 unsigned,
9243 unsigned,
9244 bool *Fast) const {
Hal Finkel8d7fbc92013-03-15 15:27:13 +00009245 if (DisablePPCUnaligned)
9246 return false;
9247
9248 // PowerPC supports unaligned memory access for simple non-vector types.
9249 // Although accessing unaligned addresses is not as efficient as accessing
9250 // aligned addresses, it is generally more efficient than manual expansion,
9251 // and generally only traps for software emulation when crossing page
9252 // boundaries.
9253
9254 if (!VT.isSimple())
9255 return false;
9256
Hal Finkel6e28e6a2014-03-26 19:39:09 +00009257 if (VT.getSimpleVT().isVector()) {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009258 if (Subtarget.hasVSX()) {
Hal Finkel6e28e6a2014-03-26 19:39:09 +00009259 if (VT != MVT::v2f64 && VT != MVT::v2i64)
9260 return false;
9261 } else {
9262 return false;
9263 }
9264 }
Hal Finkel8d7fbc92013-03-15 15:27:13 +00009265
9266 if (VT == MVT::ppcf128)
9267 return false;
9268
9269 if (Fast)
9270 *Fast = true;
9271
9272 return true;
9273}
9274
Stephen Lin73de7bf2013-07-09 18:16:56 +00009275bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
9276 VT = VT.getScalarType();
9277
Hal Finkel0a479ae2012-06-22 00:49:52 +00009278 if (!VT.isSimple())
9279 return false;
9280
9281 switch (VT.getSimpleVT().SimpleTy) {
9282 case MVT::f32:
9283 case MVT::f64:
Hal Finkel0a479ae2012-06-22 00:49:52 +00009284 return true;
9285 default:
9286 break;
9287 }
9288
9289 return false;
9290}
9291
Hal Finkelb4240ca2014-03-31 17:48:16 +00009292bool
9293PPCTargetLowering::shouldExpandBuildVectorWithShuffles(
9294 EVT VT , unsigned DefinedValues) const {
9295 if (VT == MVT::v2i64)
9296 return false;
9297
9298 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
9299}
9300
Hal Finkel88ed4e32012-04-01 19:23:08 +00009301Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Eric Christopherb1aaebe2014-06-12 22:38:18 +00009302 if (DisableILPPref || Subtarget.enableMachineScheduler())
Hal Finkel4e9f1a82012-06-10 19:32:29 +00009303 return TargetLowering::getSchedulingPreference(N);
Hal Finkel88ed4e32012-04-01 19:23:08 +00009304
Hal Finkel4e9f1a82012-06-10 19:32:29 +00009305 return Sched::ILP;
Hal Finkel88ed4e32012-04-01 19:23:08 +00009306}
9307
Bill Schmidt0cf702f2013-07-30 00:50:39 +00009308// Create a fast isel object.
9309FastISel *
9310PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo,
9311 const TargetLibraryInfo *LibInfo) const {
9312 return PPC::createFastISel(FuncInfo, LibInfo);
9313}